PI_newbee
Posts: 172
Joined: Mon Aug 26, 2013 7:41 pm

Typeerror: coercing to unicode....-> send mail with picture

Wed Nov 30, 2016 10:00 am

Hi !
I'm using this code to send mail. When i add part for attaching the picture i get error written in subject.
I dont quite understand what this message means. Is there something wrong with picture type (jpg) ?

Code: Select all

import subprocess
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
# sender
to = 'mymail@mail.com'
gmail_user = 'user@gmail.com'
gmail_password = '12345'
smtpserver = smtplib.SMTP('smtp.gmail.com', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(gmail_user, gmail_password)
today = datetime.date.today()

#add picture
fp = open(file, 'picture.jpg')
img = MIMEImage(fp.read())
fp.close()
msg.attach(img)

# reciever
arg='ip route list'
p=subprocess.Popen(arg,shell=True,stdout=subprocess.PIPE)
data = p.communicate()
split_data = data[0].split()
ipaddr = split_data[split_data.index('src')+1]
my_ip = 'IP : %s' %  ipaddr
msg = MIMEText(my_ip)
msg['Subject'] = 'Test message %s' % today.strftime('%b %d %Y')
msg['From'] = 'tester'
msg['To'] = to
smtpserver.sendmail(gmail_user, [to], msg.as_string())
smtpserver.quit()
btw. full error message : Typeerror: coercing to unicode: need string or buffer, type found

Thank you in advance for your answer !
PIN

Return to “General discussion”