########################如果想要在郵件中添加附件,加入以下代碼
#MIMEText對象,附加郵件附件文件
attach1=MIMEText(open("files/excel.xlsx","rb").read(),"base64","utf-8")
attach2=MIMEText(open("files/img01.jpg","rb").read(),"base64","utf-8")
attach3=MIMEText(open("files/text.txt","rb").read(),"base64","utf-8")
attach4=MIMEText(open("files/word.docx","rb").read(),"base64","utf-8")
#問題1,上面代碼中的后面"base64","utf-8")不知道代表什么意思?
#指定文件格式類型
attach1["Content-Type"]="application/octet-stream"
attach2["Content-Type"]="application/octet-stream"
attach3["Content-Type"]="application/octet-stream"
attach4["Content-Type"]="application/octet-stream"
#指定Content-Disposition值為attachment則出現(xiàn)下載保存對話框,保存的默認文件名使用filename指定
#由于QQ郵箱使用gb18030頁面編碼,為保證中文文件名不出現(xiàn)亂碼,對文件名進行編碼轉(zhuǎn)換
attach1["Content-Disposition"]?=?"attachment;filename=\"新建?Microsoft?Excel?工作表.xlsx\"".decode("gbk").encode("gb18030")
attach2["Content-Disposition"]?=?"attachment;filename=\"新建img01.jpg\"".decode("gbk").encode("gb18030")
attach3["Content-Disposition"]?=?"attachment;filename=\"新建文本文檔.txt\"".decode("gbk").encode("gb18030")
attach4["Content-Disposition"]?=?"attachment;filename=\"新建?Microsoft?Word?文檔.docx\"".decode("gbk").encode("gb18030")
#問題2,一開始執(zhí)行這個py文件的時候老是報編碼錯誤的問題,后來將decode("utf-8")改成了decode("gbk")郵件就能正常發(fā)送了并且能正常在瀏覽器中打開附件內(nèi)容,我想知道如何才能企確認decode("gbk")是否是正確的,這個decode("gbk")是不是就是系統(tǒng)語言編碼?
#MIMEMultipart對象附加MIMEText附件內(nèi)容
msg.attach(attach1)
msg.attach(attach2)
msg.attach(attach3)
msg.attach(attach4)
添加回答
舉報
0/150
提交
取消