Page 1 of 1

sending advanced .txt file with smtplib

Posted: Wed Nov 12, 2014 10:04 pm
by ground_
hello,

I wrote a script to automatically send a .txt file to my e-mail address. This was working fine. But when I add an "•" to the text file, it throws me an error.

this is the error:

Code: Select all

UnicodeEncodeError: 'ascii' codec can't encode character '\u2022' in position 584: ordinal not in range(128)
and this is my script:

Code: Select all

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import time

to = "****@***.com"
server= "*****"
port = "***"
user= "****@***.com"
pasw= "**********"

msg=MIMEMultipart()
msg["From"]=user
msg["To"]= to
msg["Subject"]="Daily logging file."
body="Date: %s\n\nYour daily log is attached."%(time.strftime("%m/%d/%Y"))
log = open("log.txt","r")
attachment= MIMEText(log.read())
attachment.add_header("Content-Disposition","attachment",filename="log.txt")
msg.attach(MIMEText(body,"plain"))
msg.attach(attachment)

smtpserver= smtplib.SMTP(server,port)
smtpserver.starttls()
smtpserver.login(user,pasw)
smtpserver.ehlo()
smtpserver.sendmail(user,to,msg.as_string())
smtpserver.quit()
does anybody know how to solve this?

Re: sending advanced .txt file with smtplib

Posted: Wed Nov 12, 2014 10:42 pm
by B.Goode
For a full understanding and solution I think you will need to read the documentation for the module you are using - https://docs.python.org/2/library/email.mime.html

My hunch is that you have specified that the message body has a type of "plain", which I take to mean 7-bit ascii, and clearly your " dot " character is not in that range.

Solutions might include not putting that character into a 'text' body part, or changing the definition of the body part to accept the extended character set.