0

I'm trying to send an email with the text of an txt file, the text is just a couple rows of text. The output I get when I run these is 'ascii' codec can't encode character '\xe9' in position 87: ordinal not in range(128)

bodyFloat = ("")
for differentNews in fileOpen:
    bodyFloat = bodyFloat + differentNews
    bodyFloat = bodyFloat + "\n"

sender = "sender@gmail.com"
reciver = ["reciver@gmail.com"]
password = "password"
body = str(bodyFloat)
print(body)
subject = "The daily news"


with smtplib.SMTP('smtp.gmail.com', 587) as mail:

    mail.ehlo()
    mail.starttls()
    mail.ehlo()
    mail.login(sender, password)

    message = f'Subject: {subject}\n\n{body}'
    mail.sendmail(sender, reciver,message)

MIGUEL GP
  • 63
  • 5
  • 1
    Similar https://stackoverflow.com/a/54853576/5320906 – snakecharmerb Jan 30 '20 at 14:36
  • The fundamental problem is that SMTP requires UTF-8 to be properly tagged and encapsulated in a MIME structure, not just copied verbatim. I added a duplicate which shows how to do it properly. In brief, use the `email` library to compose a valid MIME message. – tripleee Jan 30 '20 at 14:51
  • Similar to https://stackoverflow.com/questions/5910104/how-to-send-utf-8-e-mail/74584053#74584053 – Hans Daigle Nov 26 '22 at 17:00

0 Answers0