I'm writing a simple smtp-sender with authentification. Here's my code
SMTPserver, sender, destination = 'smtp.googlemail.com', 'user@gmail.com', ['reciever@gmail.com']
USERNAME, PASSWORD = "user", "password"
# typical values for text_subtype are plain, html, xml
text_subtype = 'plain'
content="""
Hello, world!
"""
subject="Message Subject"
from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
from email.MIMEText import MIMEText
try:
msg = MIMEText(content, text_subtype)
msg['Subject']= subject
msg['From'] = sender # some SMTP servers will do this automatically, not all
conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.login(USERNAME, PASSWORD)
try:
conn.sendmail(sender, destination, msg.as_string())
finally:
conn.close()
except Exception, exc:
sys.exit( "mail failed; %s" % str(exc) ) # give a error message
It works perfect, untill I try to send non-ascii symbols (russian cyrillic). How should i define a charset in a message to make it show in a proper way? Thanks in advance!
UPD. I've changed my code:
text_subtype = 'text'
content="<p>Текст письма</p>"
msg = MIMEText(content, text_subtype)
msg['From']=sender # some SMTP servers will do this automatically, not all
msg['MIME-Version']="1.0"
msg['Subject']="=?UTF-8?Q?Тема письма?="
msg['Content-Type'] = "text/html; charset=utf-8"
msg['Content-Transfer-Encoding'] = "quoted-printable"
…
conn.sendmail(sender, destination, str(msg))
So, first time I spectify text_subtype = 'text', and then in header I place a msg['Content-Type'] = "text/html; charset=utf-8" string. Is it correct?
UPDATE Finally, I've solved my message problem You should write smth like msg = MIMEText(content.encode('utf-8'), 'plain', 'UTF-8')
This should give you your proper encoding for both text and headers regardless of whether your text contains non-ASCII characters or not. It also means you won't automatically use base64 encoding unnecessarily.
You should encode your message-text with UTF-8
More here: http://radix.twistedmatrix.com/2010/07/how-to-send-good-unicode-email-with.html
You may have to use SMTP header to achieve sending different charsets, try adding this -
msg['Content-Type'] = "text/html; charset=us-ascii"
( Change the charset according to your need )