zoukankan      html  css  js  c++  java
  • python发送邮件

    # 使用163邮箱发送邮件
    # 1.先导入smtplib库用来发送邮件,导入MIMEText库用来做纯文本的邮件模板
    import smtplib
    from email.mime.text import MIMEText
    # 发送带附件的需要导入另外一个模块MIMEMultipart
    from email.mime.multipart import MIMEMultipart
    ------------------------------------------------------------------------------------------
    # 2.定义附件路径 file_path = r'D:Python_ScriptTetsInterface eports1576631030.html' # 打开文件 with open(file_path,'rb')as fp: file_data = fp.read() # ----------------------------------准备跟发件相关参数--------------------------------------- smtp_server = 'smtp.163.com' # 发件服务器 prot = 0 # 端口 sender_user = 'xxxxxxx@163.com' # 发件人账号 pwd = '*******' # 发件人密码 receivers = ['xxxxxxxx@163.com','xxxxxxxx@qq.com'] # 需要多个收件人 # 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()                          # 退出
  • 相关阅读:
    模板jinja2常用方法
    简单实用的日志类CLog (Python版)
    Databus架构分析与初步实践
    AWS API 2.0签名规范
    django学习——通过get_FOO_display 查找模型中的choice值
    Twitter如何在数千台服务器上快速部署代码?
    mysql 添加索引 mysql 如何创建索引
    sql中的like和正则的区别
    python2.7运行出现的Warning: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode
    Python命令模块argparse学习笔记(四)
  • 原文地址:https://www.cnblogs.com/XhyTechnologyShare/p/12076804.html
Copyright © 2011-2022 走看看