Python Forum
Issue with text encoding ans smtplib.SMTP.sendmail()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue with text encoding ans smtplib.SMTP.sendmail()
#1
Hi and thanks for reading.

I have a prepared email that I want to send. This text is in a unicode(utf8) text file and displays correctly.
If I call sendmail(sender,receiver,msg) with msg being msg = file.read() I get

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

I dont understand why it does Encode to Unicode in the first place, as the text is already in Unicode and all strings in python 3 are as well (by default), aren't they? If I use (sender,receiver,msg.encode('utf-8')) the email is sent, but non-ascii-chars are not shown correctly. I understand WHAT is happening: The UFT-8 Bytes are interpreted as ISO 8859-something, but I don't understand WHY this is happening and how I can do that correctly. That is porbably something I need to configure in smtplib, but what?

Please help. Thank you.
Reply
#2
Issue solved: I read a textfile that had proper windows newlines (CRLF) and in python those were read as LF only. So I use file.read.replace("\x0A","\x0D\x0A").replace("\x0D\\x0D\\x0A","\x0D\x0A") - in case someone needs it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 678 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  Encoding Issue in eyed3 giddyhead 2 1,790 Jul-22-2022, 02:03 AM
Last Post: giddyhead
  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
  [UnicodeEncodeError from smtplib] yoohooos 0 3,346 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  send email smtp rwahdan 0 1,755 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  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
  SMTP problem MajK 6 3,086 May-09-2020, 07:47 AM
Last Post: MajK
  Sending an email with attachment without using SMTP? PythonNPC 5 3,116 May-05-2020, 07:58 AM
Last Post: PythonNPC

Forum Jump:

User Panel Messages

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