WebFaction
Community site: login 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 13 Sep '12, 20:46

Zamphatta
315
accept rate: 0%

edited 13 Sep '12, 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'.

fp = open(textfile,'rt')
permanent link

answered 13 Sep '12, 23:16

johns
5.4k312
accept rate: 23%

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.

(14 Sep '12, 08:54) Zamphatta
Your answer
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

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×42
×3

question asked: 13 Sep '12, 20:46

question was seen: 3,031 times

last updated: 14 Sep '12, 08:54

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM