zoukankan      html  css  js  c++  java
  • Python自动化必备发送邮件报告脚本详解

    #!/usr/bin/python3
    # -*- coding:UTF-8 -*-
    import smtplib
    #smtplib库主要用来连接第三方smtp库,用来发邮件
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    #email库主要用来定义邮件的格式,发送人,接收人,邮件标题,邮件正文


    smtp_server = 'smtp.163.com'
    smtp_user = '185xxxxx@163.com'
    smtp_pwd = 'xxxxxxx'
    #user和pwd是用来连接smtp.163.com,不是邮箱的密码。需要在邮箱里设置一下。
    sender = smtp_user
    #发送人
    receivers = ["xxxxxx", "xxxxxxx"]
    #接收邮件的邮箱列表

    msg = MIMEMultipart()
    #创建MIMEMultipart()实例,用于构建附件

    msg['From'] = Header('张xx<18519xxxxx>', 'utf-8')
    subject = 'Python final Mail'
    msg['Subject'] = Header(subject, 'utf-8')
    #创建邮件的发送人和主题

    with open('mail.html', 'r', encoding='UTF-8') as fp:
    mail_msg = fp.read()
    msg.attach(MIMEText(mail_msg, 'html', 'utf-8'))
    #读取mail.html文件,发送html文本

    att1 = MIMEText(open('mail1.txt', 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="mail1.txt"'
    msg.attach(att1)
    #创建附件1

    att2 = MIMEText(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename="runoob.txt"'
    msg.attach(att2)
    #创建附件2

    sm = smtplib.SMTP()
    sm.connect(smtp_server, 25)
    sm.login(smtp_user, smtp_pwd)
    #连接smtp服务器,并登陆

    try:
    for rec in receivers:
    msg['To'] = rec
    sm.sendmail(sender, rec, msg.as_string())
    #发送邮件
    print("Send mail to {0} succeed".format(rec))
    except smtplib.SMTPException:
    print("Send mail to {0} fail".format(rec))
    sm.quit()
    #关闭连接
  • 相关阅读:
    Action返回类型
    低成本FPGA中实现动态相位调整
    SERDES高速系统(二)
    SERDES高速系统(一)
    Avalon总线概述
    FPGA热设计
    功耗的挑战
    特性阻抗介绍
    低阻抗电源分配系统
    非理想回路信号衰减
  • 原文地址:https://www.cnblogs.com/william126/p/8892941.html
Copyright © 2011-2022 走看看