如何在电子邮件的“发件人”字符串中发送特殊字符,例如注册符号?例如:Test® <test@hello.com>
。我需要设置一些标头吗?
如何在电子邮件的“发件人”字符串中发送特殊字符,例如注册符号?例如:Test® <test@hello.com>
。我需要设置一些标头吗?
>>> 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 <John.Doe@example.com>
from: Test® <test@hello.com>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Hello world
str
时,标题值将显示为未应用任何内容传输编码;然而,在实例本身中应用了CTE,可以通过调用EmailMessage.as_string
来查看。>>> print(em.as_string())
to: JohnD <John.Doe@example.com>
from: =?utf-8?q?Test=C2=AE?= <test@hello.com>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
Hello world