Python Forum
[UnicodeEncodeError from smtplib]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[UnicodeEncodeError from smtplib]
#1
[UnicodeEncodeError from smtplib] 'ascii' codec can't encode character '\u2019' in position 77: ordinal not in range(128)

I'm trying to send an email where the Subject of the email will be title of articles. That means, this subject line will contain all types of non-ascii characters/symbols. The method I used for sending email is using smtplib.

The following is my email function:

def email(sender, pw, receiver, header, message):
    with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()

        smtp.login(sender, pw)

        subject = header
        body = message
        msg = f'Subject: {subject}\n\n{body}'

        smtp.sendmail(sender, receiver, msg)
And this is how I call my email function:

email_header = articleheader
email(my_email, my_pass, my_email, email_header, mymessage)
However, whenever my header contains any non-ascii characters, it will return the following error:

Error:
Traceback (most recent call last): File "<input>", line 1, in <module> File "C:\Program Files\JetBrains\PyCharm 2020.3.3\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "C:\Program Files\JetBrains\PyCharm 2020.3.3\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "E:/DE Drive/Python Projects/rss_feed.py", line 186, in <module> email(my_email, my_pass, my_email, email_header, mymessage) File "E:/DE Drive/Python Projects/rss_feed.py", line 54, in email smtp.sendmail(sender, receiver, msg) File "C:\Users\nsmus\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 859, in sendmail msg = _fix_eols(msg).encode('ascii') UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 77: ordinal not in range(128)
with different '\.......' code in the list line of error message, depending on the character that causes the error.

From the error message and very little knowledge of mine reading the 'smtplib.py' script on github. It seems like msg = _fix_eols(msg).encode('ascii') this is the line that caused all the problem. I think I can easily fix this if this line of code is part of the script I wrote on my program. But, since, this line is part of smtplib library, I can't just go in there and fix it and seems to be something I am not capable of.

I'm just wondering, how can I make my script so that it can send the full message without missing any information.

Thank you very much!!!!


PS: Worst case: How to refer to "UnicodeEncodeError" from smtplib in try-except loop?
I thought I should be using smtplib.UnicodeEncodeError but that's not right.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 562: ord ctrldan 23 4,615 Apr-24-2023, 03:40 PM
Last Post: ctrldan
  [SOLVED] [Debian] UnicodeEncodeError: 'ascii' codec Winfried 1 988 Nov-16-2022, 11:41 AM
Last Post: Winfried
  UnicodeEncodeError - Dealing with Japanese Characters fioranosnake 2 2,355 Jul-07-2022, 08:43 PM
Last Post: fioranosnake
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfd' in position 14: ordin Armandito 6 2,645 Apr-29-2022, 12:36 PM
Last Post: Armandito
  UnicodeEncodeError caused by print when program runs from Popen SheeppOSU 5 2,850 Jan-13-2022, 08:11 AM
Last Post: SheeppOSU
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,111 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,661 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,680 Aug-03-2020, 08:34 AM
Last Post: buran
  TimeOutError when trying to initiate smtplib.SMTP thecosmos 0 3,296 Jun-19-2020, 05:30 AM
Last Post: thecosmos
  SMTPlib help tpolim008 4 3,735 Apr-27-2020, 11:11 PM
Last Post: tpolim008

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020