自动发送邮件,比如可以在测试程序完成之后自动给自己发个邮件,主要是方便管理。
1 import smtplib, mimetypes
2 from email.mime.text import MIMEText
3 from email.mime.multipart import MIMEMultipart
4 from email.mime.image import MIMEImage
5
6 # mail config
7 fromAddress = ""
8 fromPassword = ""
9 toAddressList = ("") # seperate by ,
10
11 # mail cotent
12 emailTitle = '2this is email title'
13 emailContent = ("Dear XXX:\n"
14 " this is email cotent.\n"
15 " Best Regards.\n"
16 " "
17 )
18 emailAttachedList=["a.oas"]
19
20 # program
21 msg = MIMEMultipart()
22 msg['From'] = fromAddress
23 msg['To'] = toAddressList
24 msg['Subject'] = emailTitle
25 txt = MIMEText(emailContent)
26 msg.attach(txt)
27 for fileName in emailAttachedList:
28 ctype, encoding = mimetypes.guess_type(fileName)
29 if ctype is None or encoding is not None:
30 ctype = 'application/octet-stream'
31 maintype, subtype = ctype.split('/', 1)
32 att1 = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, ⇉
33 att1.add_header('Content-Disposition', 'attachment', filename = f⇉
34 msg.attach(att1)
35 smtp = smtplib.SMTP()
36 smtp.connect('smtp.exmail.qq.com:25')
37 smtp.login(fromAddress, fromPassword)
38 smtp.sendmail(fromAddress, toAddressList.split(","), msg.as_string())
39 smtp.quit()
40 print 'email send success'