Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27091 > unrolled thread
| Started by | Helmut Jarausch <jarausch@igpm.rwth-aachen.de> |
|---|---|
| First post | 2012-08-15 12:16 +0000 |
| Last post | 2012-08-16 14:38 +1000 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
email with a non-ascii charset in Python3 ? Helmut Jarausch <jarausch@igpm.rwth-aachen.de> - 2012-08-15 12:16 +0000
Re: email with a non-ascii charset in Python3 ? Christian Heimes <lists@cheimes.de> - 2012-08-15 14:48 +0200
Re: email with a non-ascii charset in Python3 ? Helmut Jarausch <jarausch@skynet.be> - 2012-08-15 14:04 +0000
Re: email with a non-ascii charset in Python3 ? MRAB <python@mrabarnett.plus.com> - 2012-08-15 17:57 +0100
Re: email with a non-ascii charset in Python3 ? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-15 23:54 +0000
Re: email with a non-ascii charset in Python3 ? Ben Finney <ben+python@benfinney.id.au> - 2012-08-16 14:38 +1000
| From | Helmut Jarausch <jarausch@igpm.rwth-aachen.de> |
|---|---|
| Date | 2012-08-15 12:16 +0000 |
| Subject | email with a non-ascii charset in Python3 ? |
| Message-ID | <a91i9cFlktU4@mid.dfncis.de> |
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'] = "Email_Tester@numa-sv.igpm.rwth-aachen.de"
msg['To'] = "jarausch@igpm.rwth-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)
[toc] | [next] | [standalone]
| From | Christian Heimes <lists@cheimes.de> |
|---|---|
| Date | 2012-08-15 14:48 +0200 |
| Message-ID | <mailman.3310.1345034937.4697.python-list@python.org> |
| In reply to | #27091 |
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'] = "Email_Tester@numa-sv.igpm.rwth-aachen.de"
> msg['To'] = "jarausch@igpm.rwth-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:
>>> msg = Message()
>>> msg.set_payload("Gedanken über einen Test", "iso-8859-1")
>>> msg.as_string()
'MIME-Version: 1.0\nContent-Type: text/plain;
charset="iso-8859-1"\nContent-Transfer-Encoding:
quoted-printable\n\nGedanken =FCber einen Test'
>>> msg2 = Message()
>>> msg2.set_charset("iso-8859-1")
>>> msg2.set_payload("Gedanken über einen Test", "iso-8859-1")
>>> msg2.as_string()
'MIME-Version: 1.0\nContent-Type: text/plain;
charset="iso-8859-1"\nContent-Transfer-Encoding:
quoted-printable\n\nGedanken über einen Test'
Christian
[toc] | [prev] | [next] | [standalone]
| From | Helmut Jarausch <jarausch@skynet.be> |
|---|---|
| Date | 2012-08-15 14:04 +0000 |
| Message-ID | <502bac83$0$3117$ba620e4c@news.skynet.be> |
| In reply to | #27092 |
On Wed, 15 Aug 2012 14:48:40 +0200, Christian Heimes wrote:
> 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'] = "Email_Tester@numa-sv.igpm.rwth-aachen.de"
>> msg['To'] = "jarausch@igpm.rwth-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:
>
>>>> msg = Message()
>>>> msg.set_payload("Gedanken über einen Test", "iso-8859-1")
>>>> msg.as_string()
> 'MIME-Version: 1.0\nContent-Type: text/plain;
> charset="iso-8859-1"\nContent-Transfer-Encoding:
> quoted-printable\n\nGedanken =FCber einen Test'
>
>>>> msg2 = Message()
>>>> msg2.set_charset("iso-8859-1")
>>>> msg2.set_payload("Gedanken über einen Test", "iso-8859-1")
>>>> msg2.as_string()
> '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.
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-08-15 17:57 +0100 |
| Message-ID | <mailman.3319.1345049868.4697.python-list@python.org> |
| In reply to | #27091 |
On 15/08/2012 13:16, Helmut Jarausch wrote: > 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 -*-
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-08-15 23:54 +0000 |
| Message-ID | <502c36ce$0$29978$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #27107 |
On Wed, 15 Aug 2012 17:57:47 +0100, MRAB wrote: >> #!/usr/bin/python3 >> #_*_ coding: latin1 _*_ >> > Aw well as the other replies, the "coding" line should be: > > #-*- coding: latin1 -*- I don't believe that actually matters to Python. It may matter to Emacs or some other editors, but Python simply matches on this regex: coding[=:]\s*([-\w.]+) http://docs.python.org/py3k/reference/lexical_analysis.html#encoding-declarations -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2012-08-16 14:38 +1000 |
| Message-ID | <87k3wze9j5.fsf@benfinney.id.au> |
| In reply to | #27121 |
Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes: > On Wed, 15 Aug 2012 17:57:47 +0100, MRAB wrote: > > >> #!/usr/bin/python3 > >> #_*_ coding: latin1 _*_ > >> > > Aw well as the other replies, the "coding" line should be: > > > > #-*- coding: latin1 -*- > > I don't believe that actually matters to Python. It may matter to > Emacs or some other editors I think that is sufficient for MRAB's “should”. Especially since Python's specification is designed so that people can write a valid Emacs or Vim editor hint and have it work for Python. -- \ “The best mind-altering drug is truth.” —Jane Wagner, via Lily | `\ Tomlin | _o__) | Ben Finney
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web