How to send an email with non-ascii characters in Python

Lad python at hope.cz
Sat Feb 25 06:17:19 EST 2006


Finally I have the working version,It looks like this
#############
from email.Message import Message
from email.MIMEText import MIMEText
from email.Header import Header
import smtplib
msg = Message()
Body='Rídících Márinka a Školák Kája
Marík'.decode('utf8').encode('windows-1250')# I use the text written
in my editor with utf-8 coding, so first I decode and then encode to
windows-1250
msg = MIMEText(Body,'plain', 'windows-1250')#add body to email and with
 proper coding
h = Header('Ceské knihy - made by Czech
Republic'.decode('utf8').encode('windows-1250'), 'windows-1250')#Also
subject should be a proper coding with
msg['Subject'] = h# add the header to the message
s = smtplib.SMTP() # and send
s.connect()
s.sendmail('e..ort at h.pe.cz', 'ex..ort at h.pe.cz', msg.as_string())
s.close()
#############
Hope it may be useful to others




More information about the Python-list mailing list