login community faq

Ok, so I'm following the sample code at the Python docs for sending a simple basic email. I even created a textfile with a message in it. Anyway, I'm doing it in the Python interpreter via ssh to test it out & make sure I know what I'm doing, but when I press enter after typing the s.send_message(msg) line, I get this surprising TypeError:

import smtplib
from email.mime.text import MIMEText
textfile = 'txt4mail.txt'
fp = open(textfile,'rb')
msg = MIMEText(fp.read())
fp.close()
msg['Subject'] = "Sent using Python 3"
msg['From'] = 'xxxxxxx@zamphatta.com'
msg['To'] = 'xxxxxxxxxxxx@gmail.com'
s = smtplib.SMTP('smtp.webfaction.com')
s.send_message(msg)

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python3.2/smtplib.py", line 812, in send_message g.flatten(msg_copy, linesep='rn') File "/usr/local/lib/python3.2/email/generator.py", line 91, in flatten self._write(msg) File "/usr/local/lib/python3.2/email/generator.py", line 137, in _write self._dispatch(msg) File "/usr/local/lib/python3.2/email/generator.py", line 163, in _dispatch meth(msg) File "/usr/local/lib/python3.2/email/generator.py", line 385, in _handle_text if _has_surrogates(msg._payload): TypeError: can't use a string pattern on a bytes-like object

I come from a PHP background & have only been learning Python for a few months, so I'm not sure what's causing the error in such a basic thing. Shouldn't the example code "just work"? So, what's going on here that I need to fix? Is there a server setting I might be unaware of? Is it possible that something I put in the textfile is causing it? I tried changing stuff in it but it didn't seem to have any affect. I'm really confused.

The textfile I created in vi, is simply this, nothing special --

Yo dude!
  I am sending this via Py3k. Test link: http://google.com, enjoy!
   - Yourself

Any ideas?

asked Sep 13 '12 at 20:46

Zamphatta's gravatar image

Zamphatta
33

edited Sep 13 '12 at 20:48


I can't comment on why this is the case, but the documentation is wrong as explained in this stack overflow article, You can't open the file as 'rb' as this makes it a byte stream, and not a string it has to be read as 'rt'.

1
fp = open(textfile,'rt')

answered Sep 13 '12 at 23:16

johns's gravatar image

johns ♦♦
340427

Thanks! I never would've guessed the documentation had a mistake in it. I'm surprised it's still there. Now I get a smtplib.SMTPRecipientsRefused error, but I think I can figure that out. I'm guessing I have to log into the SMTP server using my email address before sending or else it appears like spam.

(Sep 14 '12 at 08:54) Zamphatta Zamphatta's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×229
×22
×3

Asked: Sep 13 '12 at 20:46

Seen: 375 times

Last updated: Sep 14 '12 at 08:54

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited