【python】发送邮件,含附件

def send_mail(_user,_pwd,_to):
    # f = open(file_new,'rb')
    # mail_body = f.read()
    # f.close()
    # 读取最新测试报告的内容
    #with open("H:\\AS-automation\as-testcase\Api_01\m66y.zip", "rb") as f:
        #mail_body = f.read()
    msg = MIMEMultipart()
    body = MIMEText("你好", 'HTML', 'utf-8')#邮件内容
    msg['Subject'] = Header("自动化测试报告", 'utf-8')#邮件的标题
    msg['From'] = _user
    msg['To'] = _to
    msg.attach(body)
    #
    #
    #添加附件
    att=MIMEText(open("H:\\AS-automation\\as-testcase\\Api_01\\m66y.zip","rb").read(),"base64","utf-8")#打开附件地址
    att["Content-Type"] = "application/octet-stream"
    att["Content-Disposition"] ='attachment; filename="m66y.zip"'
    msg.attach(att)

    #
    #
    #发送邮件
    s = smtplib.SMTP_SSL("smtp.qq.com")
    # s.set_debuglevel(1)
    s.login(_user,_pwd)  # 登录邮箱的账户和密码
    s.sendmail(_user,_to, msg.as_string())#发送邮件
    
    s.quit()
    print("邮件发送成功")

if __name__=="__main__":
    _user='*****@qq.com'  # 发件地址
    _pwd="服务器授权码"     #服务器授权码
    _to="****@163.com"      # 收件人地址,多人以分号分隔
    send_mail(_user,_pwd,_to)

 

posted @ 2017-12-13 18:24  抓虫使者  阅读(1662)  评论(0编辑  收藏  举报