2012年9月3日星期一

How to send a email with an image by using Python

just see the code:

#!/usr/bin/python
import smtplib
import sys
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
if __name__ == '__main__':

attach_image = sys.argv[1]
me = 'from email address' to = 'to email address' subject = 'See the Image'
username = given_your_username_when_login password = given_your_password_when_login host = given_your_host_name_like_'smtp.163.com' port = given_your_host_port_like_25
msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = me msg['To'] = to msg.preamble = subject
fp = open(attach_image,'rb') img = MIMEImage(fp.read()) fp.close()
msg.attach(img)
s = smtplib.SMTP(host,port) s.login(username,password) s.sendmail(me,to,msg.as_string()) s.quit()

1 条评论: