Python Code: Sending email with image or text attachment

#Python code for sending email using smtp server for gmail
#gmail security may not allow for mail transfer from unsecured platform.
#Use less security option of gmail account for executing this code
#Connect Raspberry pi to internet using Ethernet/Wi-Fi or through USB #tethering

import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart

f=open(“igdtuw.png”,”rb”)
img=MIMEImage(f.read())
f.close()
msg=MIMEMultipart()
msg.attach(img)
smtpUser=’user_account_id@gmail.com’
smtpPass=’account_password’

toAdd=’other_user_account@domainname.com’

fromAdd = smtpUser
msg[‘Subject’]=’Python code test’
msg[‘From’]=smtpUser
msg[‘To’]=toAdd

header = “To: ” + toAdd + “\n” + “From: ” + fromAdd + “\n”
body= “Testing Python code for email with attachment, here is the image..”
text=MIMEText(body)
msg.attach(text)

print header + “\n” + body

s = smtplib.SMTP(‘smtp.gmail.com’,587)

s.ehlo()
s.starttls()
s.ehlo()

s.login(smtpUser,smtpPass)
s.sendmail(fromAdd,toAdd,msg.as_string())
s.quit()

Tested Output