【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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM