########################如果想要在邮件中添加附件,加入以下代码 #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则出现下载保存对话框,保存的默认文件名使用filename指定 #由于QQ邮箱使用gb18030页面编码,为保证中文文件名不出现乱码,对文件名进行编码转换 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,一开始执行这个py文件的时候老是报编码错误的问题,后来将decode("utf-8")改成了decode("gbk")邮件就能正常发送了并且能正常在浏览器中打开附件内容,我想知道如何才能企确认decode("gbk")是否是正确的,这个decode("gbk")是不是就是系统语言编码? #MIMEMultipart对象附加MIMEText附件内容 msg.attach(attach1) msg.attach(attach2) msg.attach(attach3) msg.attach(attach4)
相关分类