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.










share|improve this question



























    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.










    share|improve this question

























      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 20:40

























      asked Oct 27 at 18:21









      Martin Urbanec

      2818




      2818



























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          Barbados

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3