0

I'm having a problem with this error every time I try to send a string to email.

UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 49: ordinal not in range(128)

This is the string I'm trying to send using smtplib:

Headline: The ‘New’ iPhone Actually Isn’t New You might have the impression that the tech world in general is at a bit of a standstill. Companies push new products every year, but tout iterative features rather than revolutionary changes

I tried using .encode('utf-8') and sends the email but in this format:

b"Headline: The \xe2\x80\x98New\xe2\x80\x99 iPhone Actually Isn\xe2\x80\x99t New\n\nBrief: ['You might have the impression that the tech world in general is at a bit of a standstill', ' Companies push new products every year, but tout iterative features rather than revolutionary changes']"
data = response.json()["articles"]
    messsage = data[:3]
    three = [f"Headline: {i['title']}\nBrief: {i['description'].split('.')[:2]}" for i in messsage]
    headline = [f"Headline: {i['title']}" for i in messsage]
    brief = [i['description'].split('.')[:2] for i in messsage]

    ha = []
    for i in range(3):
        joined = ".".join(brief[i])
        ha.append(joined)

    for i in range(3):

        with smtplib.SMTP("smtp.gmail.com", 587) as connection:
            connection.starttls()
            connection.login(user=my_email, password=my_passwd)
            connection.sendmail(
                from_addr=my_email,
                to_addrs=my_email,
                msg=f"Subject: Stock Update\n\n{headline[i]}\n{ha[i]}"
            )
Traceback (most recent call last):
  File "C:\Users\Acer\PycharmProjects\100_Days_Of_Python\36\dummy", line 111, in <module>
    get_news()
  File "C:\Users\Acer\PycharmProjects\100_Days_Of_Python\36\dummy", line 54, in get_news
    connection.sendmail(
  File "C:\Users\Acer\AppData\Local\Programs\Python\Python310\lib\smtplib.py", line 875, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u2018' in position 39: ordinal not in range(128)
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Can we please get a [mcve]? – Robert Apr 07 '23 at 13:52
  • 1
    please post full stacktrace – drum Apr 07 '23 at 14:07
  • Your message contains at least one character that isn't in ASCII. Encoding it as UTF-8 is correct and the representation of a UTF-8 encoded bytestring is as you indicate. Are you saying that *the e-mail you receive* has the `b'....'` representation? It should not. – kindall Apr 07 '23 at 14:15
  • Related: https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 – Caridorc Apr 07 '23 at 14:16
  • @kindall yes ive searced here and tried encoding it as UTF-8. The code works but once the email is recieved its not formatted the way i expects it to be – Kustaff Alexandrite Apr 07 '23 at 14:27
  • I don't see you setting a content encoding on the mail you're sending. That could be your problem. Since you're sending a UTF-8 body you need to indicate that in the message headers. – kindall Apr 07 '23 at 14:56

0 Answers0