0
import smtplib

server = smtplib.SMTP( "smtp.gmail.com", 587 )
server.starttls()
server.login( 'myEmail@gmail.com', 'myPassword' )

file = 'C:\\Users\\PC1\\Desktop\\myFile.txt'
f = open(file, "r")
filecontent = (f.read())
server.sendmail( 'example@gmail.com', 'example@gmail.com', filecontent )

When I run this code, i get an error like this;

Traceback (most recent call last):
  File "C:\Users\PC1\Desktop\pyos\mail.py", line 10, in <module>
    server.sendmail( 'example@gmail.com', 'example@gmail.com', filecontent )
  File "C:\Users\PC1\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 854, in sendmail
    msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u0131' in position 31: ordinal not in range(128)

How can I overcome this? I don't want a character problem in the file I send.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
M. West
  • 9
  • 2
  • you are trying to read in a file with characters with an encoding that you need to accommodate. This has been answered [before](https://stackoverflow.com/questions/11086752/read-a-text-file-with-non-ascii-characters-in-an-unknown-encoding) – Shawn Mehan May 28 '17 at 23:25
  • Sorry, that's not specific at all. – M. West May 29 '17 at 14:59

1 Answers1

0

Fixed the problem changing:

  filecontent = (f.read())

to:

filecontent = (f.read().encode("utf-8"))
M. West
  • 9
  • 2