Python:通过 python 发送邮件创建未知附件
Python: sending mail via python creates unknown attachment
我正在尝试通过 python 发送邮件 + 附件(.pdf 文件)。
邮件已发送,但附件变为 未知附件 而不是 .pdf 文件
我的代码如下所示:
import smtplib
import os
import ssl
import email
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
port = 465
smtp_server = "smtp.gmail.com"
subject = "An example of txt.file"
sender = "..."
receiver = "..."
password = "..."
message = MIMEMultipart()
message["From"] = sender
message["To"] = receiver
message["Subject"] = subject
filename = '318.pdf'
attachment = open(filename, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
part.add_header('Content Disposition', 'attachment', filename=filename)
encoders.encode_base64(part)
message.attach(part)
message.attach(part)
body = "This is an example of how to send an email with an .pdf-attachment."
message.attach(MIMEText(body, 'plain'))
text = message.as_string()
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(sender, password)
server.sendmail(sender, receiver, text)
print('Sent')
它有什么问题或者我必须做些什么不同的事情?
我尝试了不同的文件类型,.pdf 文件在 python 文件目录中,...
你写了
part.add_header('Content Disposition', 'attachment', filename=filename)
但是 'filename' argument-name 必须作为字符串提供,而且您还漏掉了 'Content-Disposition' 中的连字符。尝试
part.add_header('Content-Disposition', 'attachment; filename=' + filename)
这应该可以解决您的问题。只是指出,您在第 20 行和第 22 行附加了两次 'part'。
我认为您可能已经在关注 this 文章。但如果没有,我想你会发现它很有用。
我正在尝试通过 python 发送邮件 + 附件(.pdf 文件)。 邮件已发送,但附件变为 未知附件 而不是 .pdf 文件
我的代码如下所示:
import smtplib
import os
import ssl
import email
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
port = 465
smtp_server = "smtp.gmail.com"
subject = "An example of txt.file"
sender = "..."
receiver = "..."
password = "..."
message = MIMEMultipart()
message["From"] = sender
message["To"] = receiver
message["Subject"] = subject
filename = '318.pdf'
attachment = open(filename, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
part.add_header('Content Disposition', 'attachment', filename=filename)
encoders.encode_base64(part)
message.attach(part)
message.attach(part)
body = "This is an example of how to send an email with an .pdf-attachment."
message.attach(MIMEText(body, 'plain'))
text = message.as_string()
context = ssl.create_default_context()
with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login(sender, password)
server.sendmail(sender, receiver, text)
print('Sent')
它有什么问题或者我必须做些什么不同的事情?
我尝试了不同的文件类型,.pdf 文件在 python 文件目录中,...
你写了
part.add_header('Content Disposition', 'attachment', filename=filename)
但是 'filename' argument-name 必须作为字符串提供,而且您还漏掉了 'Content-Disposition' 中的连字符。尝试
part.add_header('Content-Disposition', 'attachment; filename=' + filename)
这应该可以解决您的问题。只是指出,您在第 20 行和第 22 行附加了两次 'part'。
我认为您可能已经在关注 this 文章。但如果没有,我想你会发现它很有用。