如何解决是什么导致我从 Python 发送到我的 gmail 帐户的“test.txt”文件在我的电子邮件收件箱中收到时显示为“无标题”?
我正在通过 python 程序发送一个名为 test.txt
的文件作为下面的附件。它成功发送,但当我在 Gmail 收件箱中收到它时,附件显示 Untitled
而不是 text.txt
。我在下面有我的电子邮件代码块,它看起来是正确的。有什么建议吗?
# Setup the email
mail_content = '''Hello,The attached document contains the results of the file scan.
Please contact the IT Help Desk if you have any questions.
Thank You!!!
'''
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'
message.attach(MIMEText(mail_content,'plain'))
attach_file_name = 'david.txt'
# attach_file_name = file_name
attach_file = open(attach_file_name,'rb') # Open the file as binary mode
# Setup the Payload
payload = MIMEBase('application','octate-stream')
payload.set_payload(attach_file.read())
encoders.encode_base64(payload) # encode the attachment
payload.add_header('Content-Decomposition','attachment',filename=attach_file_name)
message.attach(payload)
# Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com',587) # use gmail with port
session.starttls() # enable security
session.login(sender_address,sender_pass) # login with mail_id and password
text = message.as_string()
session.sendmail(sender_address,receiver_address,text)
session.quit()
解决方法
因为拼写很重要。它不是“octate-stream”,而是“octet-stream”,也不是“Content-Decomposition”,而是“Content-Disposition”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。