|
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)
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? |
|
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'.
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. |