背景:由于项目需要对每日数据进行比对监控,所以需要把比对结果发送给各负责人,本文记录下Python发送邮件的几个步骤。
需要模块:smtplib
pip install smtplib
闲言少叙上代码
# -*- coding: UTF-8 -*-
from email.header import Header
from email.mime.text import MIMEText
import smtplib
#发送邮箱
sender='xxxxx@qq.com'
password='xxxxxxxxxx'
#接受邮箱
receiver='xxxxxxxxx@qq.com'
smtpserver='smtp.qq.com'
subject='邮件发送测试'
msg=MIMEText('测试内容请忽略','plain','utf-8')
msg['Subject']=Header(subject,'utf-8')
msg['To']=receiver
msg['From']=sender
try:
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(sender,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
print ("邮件发送成功")
except smtplib.SMTPException as e:
print (e)
需要注意的是l