【发布时间】:2020-08-22 05:48:43
【问题描述】:
我正在使用以下模块使用 python 发送电子邮件:
import tkinter as tk
from tkinter import filedialog
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import email, smtplib, ssl
我想发送多个附件,但由于某种原因它们没有附加?程序没有返回错误。
这是程序的一部分,它将文件路径的元组附加到电子邮件中。为什么这不起作用,我怎样才能使它起作用?
for file in filelist:
with open(file, "rb") as attachment:
attachment_part = MIMEBase("application", "octet-stream")
attachment_part.set_payload(attachment.read())
encoders.encode_base64(attachment_part)
attachment_part.add_header(
"Content-Disposition",
f"attachment; filename = {filename}",
)
message.attach(attachment_part)
# Create Server Connection
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(config.email_sender, config.email_password)
server.sendmail(
sender_email, reciever_email, message.as_string()
)
很高兴提供更多细节...
编辑:
我尝试使用其他问题的内容,但没有收到任何附件。我也没有收到任何错误...
编辑 2:
for file in filelist:
attachment_part = MIMEBase("application", "octet-stream")
attachment_part.set_payload(open(file, "rb").read())
encoders.encode_base64(attachment_part)
attachment_part.add_header("Content-Disposition", "attachment; filename='%s'" % os.path.basename(file))
message.attach(attachment_part)
# Create Server Connection
with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
server.login(config.email_sender, config.email_password)
server.sendmail(
sender_email, reciever_email, message.as_string()
)
【问题讨论】:
-
这能回答你的问题吗? Gmail Python multiple attachments
-
没有。它不附加文件,也不给出错误。
标签: python python-3.x ssl email-attachments smtplib