Attachment Image to send by mail using Python
python, smtp
Solution
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
def SendMail(ImgFileName):
with open(ImgFileName, 'rb') as f:
img_data = f.read()
msg = MIMEMultipart()
msg['Subject'] = 'subject'
msg['From'] = 'e@mail.cc'
msg['To'] = 'e@mail.cc'
text = MIMEText("test")
msg.attach(text)
image = MIMEImage(img_data, name=os.path.basename(ImgFileName))
msg.attach(image)
s = smtplib.SMTP(Server, Port)
s.ehlo()
s.starttls()
s.ehlo()
s.login(UserName, UserPassword)
s.sendmail(From, To, msg.as_string())
s.quit()
Problem
Possible Duplicate: How to send Email Attachments with python i have do some work on sendEmail using Python i get this code ``` import smtplib def SendAnEmail( usr, psw, fromaddr, toaddr): # SMTP server server=smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(usr,psw) # Send msg="text message ....... " server.sendmail(fromaddr, toaddr, msg) server.quit() if __name__ == '__main__': # Fill info... usr='example@sender.ex' psw='password' fromaddr= usr toaddr='example@recevier.ex' SendAnEmail( usr, psw, fromaddr, toaddr) ``` if i need add image (attachment an image) how do that ? anyone have idea ?