Reputation: 31
How can I send special character like registered symbol in a email's 'from' email string? E.g. Test® <[email protected]>
. Do I require to set some headers?
Upvotes: 3
Views: 560
Reputation: 55913
In Python 3.6 or later, you can do it like this using the new EmailMessage/Policy API.
>>> from email.message import EmailMessage
>>> from email.headerregistry import Address
>>> em = EmailMessage()
>>> from_ = Address(display_name="Test®", username="test", domain="hello.com")
>>> em['from'] = from_
>>> em['to'] = Address('JohnD', 'John.Doe', 'example.com')
>>> em.set_content('Hello world')
>>> print(em)
to: JohnD <[email protected]>
from: Test® <[email protected]>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Hello world
The header value is displayed with no Content Transfer Encoding applied when str
is called on the email instance; however the CTE is applied in the instance itself, as can be seen by calling EmailMessage.as_string
:
>>> print(em.as_string())
to: JohnD <[email protected]>
from: =?utf-8?q?Test=C2=AE?= <[email protected]>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Hello world
Upvotes: 1
Reputation: 799230
email.header
is used to handle unicodes used in headers.
Upvotes: 0