Python电子邮件模块:表单头“从”与一些unicode名称+电子邮件 [英] Python email module: form header "From" with some unicode name + email
问题描述
我在Python电子邮件模块的帮助下生成电子邮件。
这里是几行代码,它演示了我的问题:
I'm generating email with the help of Python email module.
Here are few lines of code, which demonstrates my question:
msg = email.MIMEMultipart.MIMEMultipart('alternative')
msg['From'] = "somemail@somedomain.com"
msg.as_string()
Out[7]: 'Content-Type: multipart/alternative;\n boundary="===============9006870443159801881=="\nMIME-Version: 1.0\nFrom: somemail@somedomain.com\n\n--===============9006870443159801881==\n\n--===============9006870443159801881==--'
你可以看到,一切都可以在这里,从字段包含电子邮件蚂蚁它很酷。但是如果我要在电子邮件之前添加一些名字呢?特别是unicode一:
As you can see, everything is okay here, From field contains email ant it is cool. But what if I want to add some name before email? Especially unicode one:
In [8]: u.get_full_name()
Out[8]: u'\u0414\u0438\u043c\u0430 \u0426\u0443\u043a\u0430\u043d\u043e\u0432'
In [9]: msg = email.MIMEMultipart.MIMEMultipart('alternative')
In [10]: msg['From'] = "%s <%s>" % (u.get_full_name(), "email@at.com")
In [11]: msg.as_string()
Out[11]: 'Content-Type: multipart/alternative;\n boundary="===============5792069034892928634=="\nMIME-Version: 1.0\nFrom: =?utf-8?b?0JTQuNC80LAg0KbRg9C60LDQvdC+0LIgPGVtYWlsQGF0LmNvbT4=?=\n\n--===============5792069034892928634==\n\n--===============5792069034892928634==--'
这里可以看到,所有的字符串(名称,电子邮件)在base64中编码(甚至是相当合乎逻辑的,MIMEMultipart将如何知道该字符串包含unicode和非Unicode代码)
所以我的问题是:我该怎么告诉电子邮件模块我漂亮的从标题,如:
From:=?UTF-8?B?0JLQmtC + 0L3RgtCw0LrRgtC1?=< admin@notify.vk.com> ?
Here you can see, that all the string (name, email) was encoded in base64 (and it is even quite logical, how MIMEMultipart will know that string contains unicode and non-unicode parts).
So, my question is: how do I have to tell email module to make me pretty "From" header like:
From: =?UTF-8?B?0JLQmtC+0L3RgtCw0LrRgtC1?= <admin@notify.vk.com> ?
此外,我学到了一点RFC2822(http://www.faqs.org/rfcs/rfc2822.html,p .3.6.2)。它告诉:
Also, I've learned a little RFC2822 (http://www.faqs.org/rfcs/rfc2822.html , p.3.6.2). It tells:
发件人字段指示
消息的源的邮箱。 From:字段指定消息的作者
,即为消息的写入负责的个人或系统的
的邮箱。 发件人:字段指定负责实际传输
消息的代理的
邮箱。例如,如果秘书要发送另一个人的
的邮件,则秘书的邮箱将出现在
Sender:字段中,实际作者的邮箱将以
来自:字段。如果消息的发起者可以通过单个邮箱指示
,并且作者和发送者是相同的,则
Sender:字段不应该被使用。否则,两个字段SHOULD
出现。
The originator fields indicate the mailbox(es) of the source of the message. The "From:" field specifies the author(s) of the message, that is, the mailbox(es) of the person(s) or system(s) responsible for the writing of the message. The "Sender:" field specifies the mailbox of the agent responsible for the actual transmission of the message. For example, if a secretary were to send a message for another person, the mailbox of the secretary would appear in the "Sender:" field and the mailbox of the actual author would appear in the "From:" field. If the originator of the message can be indicated by a single mailbox and the author and transmitter are identical, the "Sender:" field SHOULD NOT be used. Otherwise, both fields SHOULD appear.
这是否意味着我应该组合这两个标题? (来自和发件人)。我有点困惑,因为我注意到我的Gmail中有很多电子邮件(通过显示原始),其中从字段名称和电子邮件中提供。
Does it mean that I should combine these two headers? (From and Sender). I'm a bit confused, because I noticed a lot of emails in my gmail (looking through "Show original") where in From field name and email are presented.
感谢您的帮助。
推荐答案
部分单独使用 email.header.Header
:
from email.MIMEMultipart import MIMEMultipart
from email.header import Header
from email.utils import formataddr
author = formataddr((str(Header(u'Alał', 'utf-8')), "somemail@somedomain.com"))
msg = MIMEMultipart('alternative')
msg['From'] = author
print msg
我希望这将有所帮助。
这篇关于Python电子邮件模块:表单头“从”与一些unicode名称+电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!