python发送邮件

def send_mail(smtp_dict, report):
    """用于将测试报告发送到邮箱
    :param
    smtp_dict = {
        "smtp_server": "发送邮件的smtp ex:smtp.126.com",
        "send_user": "发送邮件的邮箱 ex:am1122@126.com",
        "send_pwd": "发送邮件的邮箱密码 ex:mima",
        "sender": "发件人邮箱用于显示收到邮件中的发件人 ex:am1122@126.com",
        "receiver": "收件人邮箱 ex:zhangmin@hidtest.cn",多个收件人可以写成list
        "subject": "邮件主题 ex:自动化测试报告"
    }    
    """

    # 获取测试报告的内容
    file = open(report, "rb")
    mail_body = file.read()
    file.close()
    # 组装邮件内容
    msg = MIMEText(mail_body, 'html', 'utf-8')
    msg['Subject'] = Header(smtp_dict["subject"], 'utf-8')
    msg['From'] = smtp_dict["send_user"]
    # 发送邮件
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtp_dict["smtp_server"])
        smtp.login(smtp_dict["send_user"], smtp_dict["send_pwd"])
        smtp.sendmail(smtp_dict["sender"], smtp_dict["receiver"], msg.as_string())
    except smtplib.SMTPException as se:
        print("邮件发送失败!!")
        print(se)

 

posted @ 2018-03-22 18:09  来呀快活吧  阅读(198)  评论(0编辑  收藏  举报
cs