1

I'm trying to send an email and have a small problem with the sender address in the header part of the message contents.

Basically, this is what I want to have in the headers:

From: "Real Name" <test@example.com>

And this is what I do (Python 3.5):

message = email.mime.text.MIMEText(body)
message.add_header('From', from_hdr)

It works as expected for ASCII, but not for UTF-8.

Given a non-ASCII name like "Strángé Nämé" <test@example.com> the input gets encoded by add_header():

If the value contains non-ASCII characters ..... it is automatically encoded in RFC 2231 format using a CHARSET of utf-8 and a LANGUAGE of None.

The problem is that the whole string - not only the real name - is encoded. The result looks like a single string: =?utf-8?.....?= and the original format is lost. The mail server adds the apparently missing domain to it and the damage is done.

The sender is then shown (decoded by the recipient's email program) as: "\"Strángé Nämé\" <test@example.com>"@server.example.com

I have tried to split the address using email.utils.parseaddr() and encode only the real name part with email.utils.encode_rfc2231() but this did not help.

I'm avoiding UTF-8 in the Form: field for now, don't know how to handle it properly. Could you please help?

VPfB
  • 14,927
  • 6
  • 41
  • 75

1 Answers1

6

The following worked for me (in python2 as well as python3):

# -*- coding: utf-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.header import Header

msg = MIMEText("Hi! This is Mr. Robot!")

you = 'nehalxxxxx@somedomain.com'
me = Header('Strángé Nämé', 'utf-8')
me.append('<test@example.com>', 'ascii')

msg['Subject'] = 'Hi There'
msg['From'] = me
msg['To'] = you 

s = smtplib.SMTP('localhost')
s.sendmail(me.__str__(), [you], msg.as_string())
s.quit()

The trick is to use the Header class and the append() method

The message then looks like this (msg.as_string()):

Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: Hi There
From: =?utf-8?b?U3Ryw6FuZ8OpIE7DpG3DqQ==?= <test@example.com>
To: nehalxxxxx@somedomain.com
Nehal J Wani
  • 16,071
  • 3
  • 64
  • 89
  • Yes, the trick works for me too. The `Header` class documentation says that mixing string with different encodings is fine. Just wondering where is the separating space between the real name and the email address coming from. Anyway, the Form field is now OK. Thank you. – VPfB Jan 28 '17 at 17:06
  • Thanks, I was surprised such a thing didn't work out of the box but this solved my problem. – Shautieh May 15 '19 at 01:24