使用Python和smtplib发送带有附加文件中特殊字符的电子邮件时出现问题

2024-09-27 21:33:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个自动发送电子邮件的脚本,我遇到了一个小问题

附件文件有一些特殊字符,如(ç, á, ã...), 但这是用户的输入,我无法更改

电子邮件传递,但附件文件没有名称和扩展名

你能帮我分析一下代码,看看我能做些什么吗

我知道这和编码有关,但我的技术知识还不足以弄清楚我能做什么

`# attach the body with the msg instance 
msg.attach(MIMEText(body, 'plain'))
#get all the attachments
for anexo in anexos:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(anexo, 'rb').read())
encoders.encode_base64(part)
filename = anexo.split('/')
filename = filename[-1]
print(filename)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % anexo)
msg.attach(part)

`    

就像我说的,电子邮件已经发送了,但是附件中没有名字。如果名称中没有特殊字符,则可以正常工作

但问题是我需要发送这些字符


Tags: 文件the代码用户脚本名称附件电子邮件

热门问题