模式示例:
#!/usr/bin/env python # -*- coding: utf-8 -*- import smtplib import string HOST="smtp.gmail.com" SUBJECT="this is a test" TO="[email protected]" FROM="[email protected]" text="let us try to get python mail" BODY=string.join(( "From:%s" % FROM, "To:%s" % TO, "Subject:%s" % SUBJECT, "-------------------------------------------", "-------------------------------------------", text ),"\r\n") server=smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login(FROM,"mypasswd") server.sendmail(FROM,[TO],BODY) server.quit()
示例1:带有html格式的报表邮件
#!/usr/bin/env python # -*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText HOST="smtp.163.com" FROM="[email protected]" TO="[email protected]" SUBJECT="数据报表详情" msg=MIMEText(""" html 内容 """ ) msg['Subject']=SUBJECT msg['From']=FROM msg['To']=TO try: server=smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login(FROM,"password") server.sendmail(FROM,TO,msg.as_string()) server.quit() print "邮件已发送" except Exception,e: print "失败:"+str(e) ------------------------------------------------- #!/usr/bin/env python # -*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart HOST="smtp.qq.com" FROM="[email protected]" TO="[email protected]" SUBJECT="服务器数据报表" def adding(src,imgid): fp=open(src,'rb') msgImage=MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID',imgid) return msgImage msg=MIMEMultipart('related') msgtext=MIMEText(""" """) msg.attach(msgtext) msg.attach(adding("/data/image/test.png","io")) msg.attach(adding("/data/image/good.png","disk")) msg.attach(adding("/data/image/try.png","cpu")) msg.attach(adding("/data/image/mem.png","mem")) msg['Subject']=SUBJECT msg['From']=FROM msg['To']=TO try: server=smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login(FROM,"password") server.sendmail(FROM,TO,msg.as_string()) server.quit() print "邮件已发送" except Exception,e: print "失败:"+str(e) -------------------------------------------------- #!/usr/bin/env python # -*- coding: utf-8 -*- import smtplib from email.mime.text import MIMEText from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart HOST="smtp.qq.com" FROM="[email protected]" TO="[email protected]" SUBJECT="业务月报信息" def adding(src,imgid): fp=open(src,'rb') msgImage=MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID',imgid) return msgImage msg=MIMEMultipart('related') msgtext=MIMEText("<font color=red> 业务报表:<br><img src=\"cid:yuebao\"border=\"1\"></br>详见附件 </font>","html","utf-8") msg.attach(msgtext) msg.attach(adding("/data/image/yuebao.png","yuebao")) attach=MIMEText(open("/data/yuebao.xlsx","rb").read(),"base64","utf-8") attach["Content-Type"]="application/octet-stream" attach["Content-Disposition"]="attachment;filename=\"月报.xlsx\" #attach["Content-Disposition"]="attachment;filename=\"月报.xlsx\"".decode("utf-8").encode("gb18030") msg.attach(attach) msg['Subject']=SUBJECT msg['From']=FROM msg['To']=TO try: server=smtplib.SMTP() server.connect(HOST,"25") server.starttls() server.login(FROM,"password") server.sendmail(FROM,TO,msg.as_string()) server.quit() print "邮件已发送" except Exception,e: print "失败:"+str(e)
注意:采用SMTP协议,那么需要确保自己的发送邮箱地址开启了SMTP服务
点击开启后,生产授权码,上面示例代码中的password 就是授权码,不是自己的密码
原文地址:http://blog.51cto.com/superleedo/2116645
时间: 2024-10-29 03:28:47