email with a non-ascii charset in Python3 ?

H

Helmut Jarausch

Hi,

I'm sorry to ask such a FAQ but still I couldn't find an answer - neither in the docs nor the web.

What's wrong with the following script?

Many thanks for a hint,
Helmut.

#!/usr/bin/python3
#_*_ coding: latin1 _*_

import smtplib
from email.message import Message
import datetime

msg= Message()
msg.set_charset('latin-1')
msg['Subject'] = "*** Email Test ***"
msg['From'] = "(e-mail address removed)-aachen.de"
msg['To'] = "(e-mail address removed)-aachen.de"
msg['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S %p')

server= smtplib.SMTP("igpm.igpm.rwth-aachen.de")
msg.set_payload("Gedanken über einen Test","iso-8859-1")

## I have tried msg.set_payload("Gedanken über einen Test".encode("iso-8859-1"),"iso-8859-1")
## which fails, as well

server.send_message(msg)
Traceback (most recent call last):
File "./Test_EMail_Py3.py", line 17, in <module>
server.send_message(msg)
File "/usr/lib64/python3.2/smtplib.py", line 812, in send_message
g.flatten(msg_copy, linesep='\r\n')
File "/usr/lib64/python3.2/email/generator.py", line 91, in flatten
self._write(msg)
File "/usr/lib64/python3.2/email/generator.py", line 137, in _write
self._dispatch(msg)
File "/usr/lib64/python3.2/email/generator.py", line 163, in _dispatch
meth(msg)
File "/usr/lib64/python3.2/email/generator.py", line 396, in _handle_text
super(BytesGenerator,self)._handle_text(msg)
File "/usr/lib64/python3.2/email/generator.py", line 201, in _handle_text
self.write(payload)
File "/usr/lib64/python3.2/email/generator.py", line 357, in write
self._fp.write(s.encode('ascii', 'surrogateescape'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 9: ordinal not in range(128)
server.quit()



This is Python 3.2.4 (GIT 20120805)
 
H

Helmut Jarausch

Am 15.08.2012 14:16, schrieb Helmut Jarausch:
Hi,

I'm sorry to ask such a FAQ but still I couldn't find an answer -
neither in the docs nor the web.

What's wrong with the following script?

Many thanks for a hint,
Helmut.

#!/usr/bin/python3 #_*_ coding: latin1 _*_

import smtplib from email.message import Message import datetime

msg= Message()
msg.set_charset('latin-1')
msg['Subject'] = "*** Email Test ***"
msg['From'] = "(e-mail address removed)-aachen.de"
msg['To'] = "(e-mail address removed)-aachen.de"
msg['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S
%p')

server= smtplib.SMTP("igpm.igpm.rwth-aachen.de")
msg.set_payload("Gedanken über einen Test","iso-8859-1")

You mustn't combine set_charset() with set_payload() with a charset.
That results into invalid output:
'MIME-Version: 1.0\nContent-Type: text/plain;
charset="iso-8859-1"\nContent-Transfer-Encoding:
quoted-printable\n\nGedanken =FCber einen Test'
'MIME-Version: 1.0\nContent-Type: text/plain;
charset="iso-8859-1"\nContent-Transfer-Encoding:
quoted-printable\n\nGedanken über einen Test'


Thanks!
Just, one mustn't use
server.send_message(msg.as_string())

But what if msg['From'] contains a non-ASCII character?

I wonder what the usage of msg.set_charset('latin-1') is.

With
msg.set_charset('latin-1')
msg.set_payload("Gedanken über einen Test") # is accepted BUT
server.send_message(msg)

gives
Traceback (most recent call last):
File "Test_EMail_Py3_2.py", line 21, in <module>
server.send_message(msg)
File "/usr/lib64/python3.2/smtplib.py", line 812, in send_message
g.flatten(msg_copy, linesep='\r\n')
File "/usr/lib64/python3.2/email/generator.py", line 91, in flatten
self._write(msg)
File "/usr/lib64/python3.2/email/generator.py", line 137, in _write
self._dispatch(msg)
File "/usr/lib64/python3.2/email/generator.py", line 163, in _dispatch
meth(msg)
File "/usr/lib64/python3.2/email/generator.py", line 396, in
_handle_text
super(BytesGenerator,self)._handle_text(msg)
File "/usr/lib64/python3.2/email/generator.py", line 201, in
_handle_text
self.write(payload)
File "/usr/lib64/python3.2/email/generator.py", line 357, in write
self._fp.write(s.encode('ascii', 'surrogateescape'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in
position 9: ordinal not in range(128)

Helmut.
 
M

MRAB

Hi,

I'm sorry to ask such a FAQ but still I couldn't find an answer - neither in the docs nor the web.

What's wrong with the following script?

Many thanks for a hint,
Helmut.

#!/usr/bin/python3
#_*_ coding: latin1 _*_
Aw well as the other replies, the "coding" line should be:

#-*- coding: latin1 -*-
 

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

Forum statistics

Threads
474,037
Messages
2,570,371
Members
47,013
Latest member
JewellChes

Latest Threads

Top