1. 程式人生 > >【python】發送郵件,含附件

【python】發送郵件,含附件

msg 分號 mat gin 內容 nbsp span level mtp

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)

【python】發送郵件,含附件