python发送邮件,,# 使用163邮箱发


# 使用163邮箱发送邮件# 1.先导入smtplib库用来发送邮件,导入MIMEText库用来做纯文本的邮件模板import smtplibfrom email.mime.text import MIMEText# 发送带附件的需要导入另外一个模块MIMEMultipartfrom email.mime.multipart import MIMEMultipart
------------------------------------------------------------------------------------------# 2.定义附件路径file_path = r‘D:\Python_Script\TetsInterface\reports\1576631030.html‘# 打开文件with open(file_path,‘rb‘)as fp: file_data = fp.read()# ----------------------------------准备跟发件相关参数---------------------------------------smtp_server = ‘smtp.163.com‘ # 发件服务器prot = 0 # 端口sender_user = ‘[email protected]‘ # 发件人账号pwd = ‘*******‘ # 发件人密码receivers = [‘[email protected]‘,‘[email protected]‘] # 需要多个收件人# 4. --------------------------------编辑邮件内容-------------------------------------------subject = ‘这是接口自动化测试报告‘ # 邮件主题msg = MIMEMultipart()msg[‘from‘] = sender_user # 定义发件人msg[‘to‘] = ‘;‘.join(receivers) # 定义收件人,多个收件人不可直接接收列表,需要通过分号隔开msg[‘subject‘] = subject # 邮件主题body = MIMEText(file_data,‘html‘,‘utf-8‘) # 邮件正文msg.attach(body)# ------------------------------------附件--------------------------------------------------accessory = MIMEText(file_data,‘base64‘,‘utf-8‘)accessory["Content-Type"] = "application/octet-stream"accessory["Content-Disposition"] = ‘attachment; filename="test_report.html"‘ #附件重命名msg.attach(accessory)# -----------------------------------准备发送------------------------------------------------smtp = smtplib.SMTP()smtp.connect(smtp_server)smtp.login(sender_user,pwd)              #登录smtp.sendmail(sender_user,receivers,msg.as_string()) # 发送smtp.quit()                          # 退出

python发送邮件

评论关闭