如果你真的需要使用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)