我正在努力通过Python发送excel文件附件的电子邮件。我现在面临的问题是python更改excel文件格式为'。Aaf格式或其他未知格式。当python将excel文件发送到特定的电子邮件地址时,就会发生这种情况,我猜这是内部网邮件地址。其他个人电邮地址(例如:Gmail)没有问题,没有改变文件格式。假设内网的邮件地址除了接收excel文件外,还接收格式怪异的pdf文件。为了解决这个问题,我夜以继日地寻找线索,可就是找不到任何有关的建议。任何帮助吗?下面是我的代码:
注)附件=文件路径+文件名
def send_mail(mail_list, cc_list, subject, text, attachment):
msg = EmailMessage()
msg["From"] = MY_ID
msg["To"] = ",".join(mail_list)
msg["Cc"] = ",".join(cc_list)
msg["Subject"] = subject
msg.set_content(text)
if attachment:
filename = Path(attachment).name
with open(filename, "rb") as f:
msg.add_attachment(f.read(), maintype="application", subtype="xlsx", filename=filename)
msg.add_header('Content-Disposition', 'attachment; filename='+filename)
with SMTP_SSL("smtp.gmail.com", 465) as smtp:
smtp.login(MY_ID, MY_PW)
smtp.send_message(msg)
一个人留下了评论,似乎被删除了,这就解决了问题。解决方案是:替换我写的代码
msg.add_attachment(f.read(), maintype="application", subtype="xlsx", filename=filename)
msg.add_attachment(f.read(), maintype="application", subtype="vnd.openxmlformats-officedocument.spreadsheetml.sheet", filename=filename)