繁体   English   中英

Python电子邮件附件未解码

[英]Python email attachment is not decoding

我正在尝试从文本字符串而不是外部文件发送电子邮件附件,但是附件在接收端没有解码。 我使用内置的字符串编码功能在base64中对附件进行了编码。 我看到了许多附加外部文件的示例,但是我没有看到将字符串作为附件发送的示例。

为什么附件在接收端没有解码? 我对附件的编码不正确吗?

attachment = MIMEText("This is a test".encode('base64', 'strict'))
attachment.add_header('Content-Disposition', 'attachment', 'test.txt')           
msg.attach(attachment)

如果确实需要使用base64 ,则应显式设置编码:

attachment = MIMEText("This is a test".encode('base64', 'strict'))
attachment.add_header('Content-Disposition', 'attachment', filename='test.txt')           
attachment.replace_header('content-transfer-encoding', 'base64')
msg.attach(attachment)

如果您真的不需要base64 ,请让该库为您决定:

attachment = MIMEText("This is a test")
attachment.add_header('Content-Disposition', 'attachment', filename='test.txt')           
msg.attach(attachment)

或者,使用email.encoders.encode_base64

attachment = MIMEText("This is a test")
email.encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment', filename='test.txt')           
msg.attach(attachment)

您可以参考http://docs.python.org/2/library/email.mime.html?highlight=mimetext#email.mime.text.MIMEText并使用

email.mime.text.MIMEText(_text[, _subtype[, _charset]])

_subtype默认plain和_CHARSET明确说明您的编码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM