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

L

Lad

Can anyone give an example how to send email with non-ascii characters(
both in subject and body).
I would like to use windows-1250 code page
Thank you
L.B.
 
S

Sybren Stuvel

Lad enlightened us with:
Can anyone give an example how to send email with non-ascii
characters( both in subject and body). I would like to use
windows-1250 code page

I'd use Latin-1 or UTF-8, since they are both cross-platform instead
of windows-specific...

Sybren
 
S

Sybren Stuvel

Lad enlightened us with:
and can give me an example of Python code that can send such
email??

Not really, but I'm sure this will help you. In fact, my last name has
an umlaut on the 'u'. This is the From header in my emails, encoded in
Latin-1:

From: Sybren =?iso-8859-1?Q?St=FCvel?= <[email protected]>

You must ensure that there are no spaces in the specially encoded
part. So if I'd encode my first name too, it would be:

=?iso-8859-1?Q?Sybren?= =?iso-8859-1?Q?St=FCvel?=

Sybren
 
L

Lad

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-mail address removed)', '(e-mail address removed)', msg.as_string())
s.close()
#############
Hope it may be useful to others
 
S

Sybren Stuvel

Lad enlightened us with:
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

Why would you do that? What's the advantage of windows-1250?

Sybren
 
K

Kent Johnson

Gabriel said:
what does a string became when it's decoded?

I mean, it must be encoded in something, right?

Unicode, for encodings like latin-1 or utf-8. A few special cases like
str.decode('string_escape') yield byte strings again.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top