开发者

Python : sending mail through gmail issue

I am trying to send an email (through gmail) using python script that someone once wrote on this site, but I'm getting an error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 2: invalid continuation byte

the script:


import smtplib
from email.mime.text import MIMEText
#mail setup
FROMMAIL = "xxx@gmail.com"
LOGIN    = FROMMAIL
PASSWORD = "yyy"
SUBJECT  = "test su开发者_C百科bject"
TOMAIL  = "xxx@gmail.com"

msg = MIMEText('testcontent')
msg['Subject'] = 'test'
msg['From'] = FROMMAIL
msg['To'] = TOMAIL
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMMAIL, [TOMAIL], msg.as_string())
server.quit()

The stacktrace:


Traceback (most recent call last):
  File "C:\Users\xxx\Desktop\test.py", line 11, in 
    server = smtplib.SMTP('smtp.gmail.com', 587)
  File "C:\Program Files\Python31\lib\smtplib.py", line 248, in __init__
    fqdn = socket.getfqdn()
  File "C:\Program Files\Python31\lib\socket.py", line 290, in getfqdn
    name = gethostname()
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe8 in position 2: invalid continuation byte

I am using python v3.1.3. How to resolve this?

Thank you.


Use the 'email' module of Python in order to generate proper formatted emails. Dealing yourself with encoding issues on the application level while composing emails directly through Python is not the way to go.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜