[Python-Dev] headers api for email package (original) (raw)
Chris Withers chris at simplistix.co.uk
Sat Apr 11 14:39:40 CEST 2009
- Previous message: [Python-Dev] email header encoding
- Next message: [Python-Dev] headers api for email package
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Barry Warsaw wrote:
>>> message['Subject']
The raw bytes or the decoded unicode?
A header object.
Okay, so you've picked one. Now how do you spell the other way?
str(message['Subject']) bytes(message['Subject'])
Now, setting headers. Sometimes you have some unicode thing and sometimes you have some bytes. You need to end up with bytes in the ASCII range and you'd like to leave the header value unencoded if so. But in both cases, you might have bytes or characters outside that range, so you need an explicit encoding, defaulting to utf-8 probably.
>>> Message.setheader('Subject', 'Some text', encoding='utf-8') >>> Message.setheader('Subject', b'Some bytes')
Where you just want "a damned valid email and stop making my life hard!":
Message['Subject']='Some text'
Where you care about what encoding is used:
Message['Subject']=Header('Some text',encoding='utf-8')
If you have bytes, for whatever reason:
Message['Subject']=b'some bytes'.decode('utf-8')
...because only you know what encoding those bytes use!
One of those maps to
>>> message['Subject'] = ???
...should only accept text or a Header object.
Chris
-- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
- Previous message: [Python-Dev] email header encoding
- Next message: [Python-Dev] headers api for email package
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]