簡體   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