是什么导致我的 'test.txt' 文件从 Python 发送到我的 gmail 帐户,当我在我的电子邮件收件箱中收到它时却显示为 "Untitled"?
What is causing my 'test.txt' file being sent from Python to my gmail account to come across as "Untitled" when I receive it in my email inbox?
我正在通过 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”。
我正在通过 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”。