Sending mails using Python3 smtplib with real name containing unicode characters in From header fails
up vote
0
down vote
favorite
I'm trying to send mails using smtplib in a way that displays to the user real name (like the Gecos field in /etc/passwd). To accomplish that, I use Something <user@example.com>
as value of From: header.
I'm trying to do this:
to = 'test@wikimedia.cz'
from_mail = 'info@wikimedia.cz'
from_name = 'Sender Name'
mailtext = 'Příliš žluťoučký kůň úpěl ďábelské ódy'
s = smtplib.SMTP('smtp-relay.gmail.com')
s.ehlo()
s.starttls()
msg = MIMEText(mailtext, _charset="utf-8")
msg['Subject'] = '(bez předmětu)'
msg['From'] = '%s <%s>' % (from_name, from_mail)
msg['To'] = to
s.sendmail(from_mail, to, msg.as_string())
s.quit()
This works correctly, as you can see on a screenshot of my Gmail. However, if from_mail is "Jméno Odesílatele" (which includes non-ASCII characters), the mail a) gets redirected to Spam folder b) doesn't display "Jméno Odesílatele" as sender's name, see screenshot.
As you can see, mail text can contain non-ASCII characters with no problem. Also, Subject containing non-ASCII characters with no problem. But From containing non-ASCII doesn't work.
- Headers of wrongly displayed mail
- Headers of correctly displayed mail
Update 2018-11-10
Updated the code snippet to include real data, like the mailserver I use.
I tried to use msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8')
, msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8').encode()
and a
instead of msg['From'] = '%s <%s>' % (from_name, from_mail)
, the result is the same.
If anybody's interested, my real code is on GitHub.
python-3.x smtplib
add a comment |
up vote
0
down vote
favorite
I'm trying to send mails using smtplib in a way that displays to the user real name (like the Gecos field in /etc/passwd). To accomplish that, I use Something <user@example.com>
as value of From: header.
I'm trying to do this:
to = 'test@wikimedia.cz'
from_mail = 'info@wikimedia.cz'
from_name = 'Sender Name'
mailtext = 'Příliš žluťoučký kůň úpěl ďábelské ódy'
s = smtplib.SMTP('smtp-relay.gmail.com')
s.ehlo()
s.starttls()
msg = MIMEText(mailtext, _charset="utf-8")
msg['Subject'] = '(bez předmětu)'
msg['From'] = '%s <%s>' % (from_name, from_mail)
msg['To'] = to
s.sendmail(from_mail, to, msg.as_string())
s.quit()
This works correctly, as you can see on a screenshot of my Gmail. However, if from_mail is "Jméno Odesílatele" (which includes non-ASCII characters), the mail a) gets redirected to Spam folder b) doesn't display "Jméno Odesílatele" as sender's name, see screenshot.
As you can see, mail text can contain non-ASCII characters with no problem. Also, Subject containing non-ASCII characters with no problem. But From containing non-ASCII doesn't work.
- Headers of wrongly displayed mail
- Headers of correctly displayed mail
Update 2018-11-10
Updated the code snippet to include real data, like the mailserver I use.
I tried to use msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8')
, msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8').encode()
and a
instead of msg['From'] = '%s <%s>' % (from_name, from_mail)
, the result is the same.
If anybody's interested, my real code is on GitHub.
python-3.x smtplib
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to send mails using smtplib in a way that displays to the user real name (like the Gecos field in /etc/passwd). To accomplish that, I use Something <user@example.com>
as value of From: header.
I'm trying to do this:
to = 'test@wikimedia.cz'
from_mail = 'info@wikimedia.cz'
from_name = 'Sender Name'
mailtext = 'Příliš žluťoučký kůň úpěl ďábelské ódy'
s = smtplib.SMTP('smtp-relay.gmail.com')
s.ehlo()
s.starttls()
msg = MIMEText(mailtext, _charset="utf-8")
msg['Subject'] = '(bez předmětu)'
msg['From'] = '%s <%s>' % (from_name, from_mail)
msg['To'] = to
s.sendmail(from_mail, to, msg.as_string())
s.quit()
This works correctly, as you can see on a screenshot of my Gmail. However, if from_mail is "Jméno Odesílatele" (which includes non-ASCII characters), the mail a) gets redirected to Spam folder b) doesn't display "Jméno Odesílatele" as sender's name, see screenshot.
As you can see, mail text can contain non-ASCII characters with no problem. Also, Subject containing non-ASCII characters with no problem. But From containing non-ASCII doesn't work.
- Headers of wrongly displayed mail
- Headers of correctly displayed mail
Update 2018-11-10
Updated the code snippet to include real data, like the mailserver I use.
I tried to use msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8')
, msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8').encode()
and a
instead of msg['From'] = '%s <%s>' % (from_name, from_mail)
, the result is the same.
If anybody's interested, my real code is on GitHub.
python-3.x smtplib
I'm trying to send mails using smtplib in a way that displays to the user real name (like the Gecos field in /etc/passwd). To accomplish that, I use Something <user@example.com>
as value of From: header.
I'm trying to do this:
to = 'test@wikimedia.cz'
from_mail = 'info@wikimedia.cz'
from_name = 'Sender Name'
mailtext = 'Příliš žluťoučký kůň úpěl ďábelské ódy'
s = smtplib.SMTP('smtp-relay.gmail.com')
s.ehlo()
s.starttls()
msg = MIMEText(mailtext, _charset="utf-8")
msg['Subject'] = '(bez předmětu)'
msg['From'] = '%s <%s>' % (from_name, from_mail)
msg['To'] = to
s.sendmail(from_mail, to, msg.as_string())
s.quit()
This works correctly, as you can see on a screenshot of my Gmail. However, if from_mail is "Jméno Odesílatele" (which includes non-ASCII characters), the mail a) gets redirected to Spam folder b) doesn't display "Jméno Odesílatele" as sender's name, see screenshot.
As you can see, mail text can contain non-ASCII characters with no problem. Also, Subject containing non-ASCII characters with no problem. But From containing non-ASCII doesn't work.
- Headers of wrongly displayed mail
- Headers of correctly displayed mail
Update 2018-11-10
Updated the code snippet to include real data, like the mailserver I use.
I tried to use msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8')
, msg['From'] = Header('%s <%s>' % (from_name, from_mail), 'utf-8').encode()
and a
instead of msg['From'] = '%s <%s>' % (from_name, from_mail)
, the result is the same.
If anybody's interested, my real code is on GitHub.
python-3.x smtplib
python-3.x smtplib
edited Nov 10 at 20:40
asked Oct 27 at 18:21
Martin Urbanec
2818
2818
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53024985%2fsending-mails-using-python3-smtplib-with-real-name-containing-unicode-characters%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown