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)