Python email module: form header "From" with some unicode name + email
email, python
Solution
You need to encode the name part separately using `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
I hope this will help.
Problem
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==--' ``` 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==--' ``` 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> ?` Also, I've learned a little RFC2822 (http://www.faqs.org/rfcs/rfc2822.html , p.3.6.2). It tells: 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. 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. Thanks for help.