1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| _user = "发送人邮箱地址" smtpserver='邮箱SMTP地址' _from = '发送人姓名<发送人邮箱地址>' _pwd = "邮箱密码"
_to = '收件人名字<收件人邮箱地址>'
'''我们编写了一个函数_format_addr()来格式化一个邮件地址。 注意不能简单地传入name <addr@example.com>, 因为如果包含中文,需要通过Header对象进行编码。---廖雪峰''' def _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), addr))
msg = MIMEMultipart() msg["Subject"] = "邮件名字" msg["From"] =_format_addr( _from) msg["To"] = _to
def addimg(src, imgid): fp = open(src, 'rb') msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header('Content-ID', imgid) return msgImage
part = MIMEText('<font size="18px" color=red><b><center>ArcGIS知乎站内搜索关键词统计(详情见附件)</center></b></font>'+'<br/><b><center>数据图表获取生成时间:'+str(datetime.datetime.now())+'</center></b><img width="1300px" src="cid:pic">',"html","utf-8" ) msg.attach(part)
msg.attach(addimg(savename,"pic"))
part = MIMEApplication(open('C://Users//Esri//Desktop//statistics//爬取的数据源.xlsx', 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '',"爬取的数据源.xlsx")) msg.attach(part)
part = MIMEApplication(open('C://Users//Esri//Desktop//statistics//每天搜索次数统计.txt', 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '',"每天搜索次数统计.xlsx")) msg.attach(part)
part = MIMEApplication(open('C://Users//Esri//Desktop//statistics//每月搜索次数统计.xlsx', 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '',"每月搜索次数统计.xlsx")) msg.attach(part)
part = MIMEApplication(open('C://Users//Esri//Desktop//statistics//关键词top100.xlsx', 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '',"关键词top100.xlsx")) msg.attach(part)
part = MIMEApplication(open(savename, 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '',"Top20统计结果直方图.png")) msg.attach(part)
part = MIMEApplication(open(savets, 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '',"每天统计结果折点图.png")) msg.attach(part)
part = MIMEApplication(open(savemonth, 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '',"每月统计结果折点图.png")) msg.attach(part)
s = smtplib.SMTP(smtpserver,25) s.login(_user, _pwd) s.sendmail(_user, _to.split(','), msg.as_string()) s.close()
|