python 每周一练
每周五发布 python 需求, 所有需求都来自实际企业. 下周五发布参考答案.
python 测试开发自动化测试数据分析人工智能自学每周一练 - 2018-06
2018-07-06 使用 python3 smtplib 通过网易 126 邮箱发送带附件的邮件.
图片. png
参考代码:
- def send_mail(recipients, sub, content, from_name='比对测试',server="smtp.126.com",
- files=[]):
- EMAIL_SEND_USER = os.environ.get('EMAIL_SEND_USER')
- EMAIL_SEND_PASSPORT = os.environ.get('EMAIL_SEND_PASSPORT')
- msg = MIMEMultipart()
- msg.attach(MIMEText(content, 'plain'))
- msg['Subject'] = sub
- msg['From'] = "{}<{}>".format(from_name, EMAIL_SEND_USER)
- msg['To'] = ",".join(recipients)
- try:
- s = smtplib.SMTP()
- s.connect(server)
- s.login(EMAIL_SEND_USER, EMAIL_SEND_PASSPORT)
- for f in files or []:
- with open(f, "rb") as fil:
- part = MIMEApplication(
- fil.read(),
- Name=os.path.basename(f)
- )
- # After the file is closed
- part['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(f)
- msg.attach(part)
- print("send email to {}".format(recipients))
- s.sendmail(EMAIL_SEND_USER, recipients, msg.as_string())
- s.close()
- return True
- except Exception as e:
- print(str(e))
- return False
- if __name__ == '__main__':
- if send_mail(['xurongzhong@sensetime.com'],"活体比对测试结果", "测试结果",
- files=[r'output.xls']):
- print("发送成功")
参考资料:
最新代码地址 https://github.com/china-testing/python-api-tesing/blob/master/others.py
http://naelshiab.com/tutorial-send-email-python/
https://gist.github.com/dtanham/11326557 要翻墙
http://code.activestate.com/recipes/578807-sending-email-from-a-python-program/
可爱的 python 测试开发库 https://github.com/china-testing/python-api-tesing 请在 github 上点赞, 谢谢!
python 中文库文档汇总
接口自动化性能测试线上培训大纲
来源: http://www.jianshu.com/p/90a64465f66a