yagmail icon indicating copy to clipboard operation
yagmail copied to clipboard

UnicodeEncodeError

Open bepetersn opened this issue 5 years ago • 0 comments

Getting this error when trying to send some non-ascii content with yagmail. Stack trace:

  File "/Users/brianp/Code/pivi/app.py", line 149, in send_email
    mail.send(email_address, template.module.SUBJECT, contents)
  File "/Users/brianp/.local/share/virtualenvs/pivi-T-u2lKU4/lib/python3.7/site-packages/yagmail/sender.py", line 154, in send
    return self._attempt_send(recipients, msg_string)
  File "/Users/brianp/.local/share/virtualenvs/pivi-T-u2lKU4/lib/python3.7/site-packages/yagmail/sender.py", line 160, in _attempt_send
    result = self.smtp.sendmail(self.user, recipients, msg_string)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 855, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 836: ordinal not in range(128)

Content:

<html>\n <body>\n <h3 style = "font-size: 3em;font-weight: 700;color: dodgerblue; margin: 0; border-bottom: solid 1px gainsboro; width: 350px;">Flashcards</h3>\n <table><tr>\n <td style = "font-weight: 700; margin-right: 30px;font-size: 1.5em; border-spacing: 30px; color: dodgerblue;">qué</td><td style = "padding-left: 40px;font-size: 1.5em; font-weight: 100;">that, than, who, whom</td>\n </tr></table>\n </body>\n</html>

Final content of my email, on debugging:

'Content-Type: multipart/mixed; boundary="===============7115580951676798068=="\nMIME-Version: 1.0\nDate: Mon, 15 Jul 2019 21:43:36 -0000\nSubject: Your Flashcards\nFrom: "[email protected]" <[email protected]>\nTo: [email protected]\n\n--===============7115580951676798068==\nContent-Type: multipart/alternative; boundary="===============1668278927287579622=="\nMIME-Version: 1.0\n\n--===============1668278927287579622==\nContent-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\n\n<html><br> <body><br> <h3 style = "font-size: 3em;font-weight: 700;color: dodgerblue; margin: 0; border-bottom: solid 1px gainsboro; width: 350px;">Flashcards</h3><br> <table><tr><br> <td style = "font-weight: 700; margin-right: 30px;font-size: 1.5em; border-spacing: 30px; color: dodgerblue;">qué</td><td style = "padding-left: 40px;font-size: 1.5em; font-weight: 100;">that, than, who, whom</td><br> </tr></table><br> </body><br></html>\n--===============1668278927287579622==\nContent-Type: multipart/related; boundary="===============0802201098011733724=="\nMIME-Version: 1.0\n\n--===============0802201098011733724==\nContent-Type: text/html; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: 8bit\n\n<div><html><br> <body><br> <h3 style = "font-size: 3em;font-weight: 700;color: dodgerblue; margin: 0; border-bottom: solid 1px gainsboro; width: 350px;">Flashcards</h3><br> <table><tr><br> <td style = "font-weight: 700; margin-right: 30px;font-size: 1.5em; border-spacing: 30px; color: dodgerblue;">qué</td><td style = "padding-left: 40px;font-size: 1.5em; font-weight: 100;">that, than, who, whom</td><br> </tr></table><br> </body><br></html></div>\n--===============0802201098011733724==--\n\n--===============1668278927287579622==--\n\n--===============7115580951676798068==--\n'

It superficially looks like smtplib is messing up from sleuthing around the stack trace. I probably don't understand how the encoding supposed to work though, because it looks like they just up and call .encode('ascii') right at the beginning of their sendmail method. I don't understand why I see other examples of smtplib usage working similarly. I do see that yagmail is passing a 'utf-8' encoding to MimeText and classes like that in prepare_message.

I also don't see a lot of other complaints about this here (not since 2017) so it's probably my environment. Python 3.7.3. yagmail==0.11.220. MacOS Sierra, but also possibly happening in an Ubuntu Linux server environment.

I haven't been able to reproduce the issue yet with my contents, when using yagmail on its own--only in the context of my application being run (definitely environmental)... Could use a suggestion on what to look into.

bepetersn avatar Jul 15 '19 21:07 bepetersn