Thunderbird "size unknown" 与 python 一起发送时的附件



>当我使用下面的python代码发送带有附件的电子邮件时,虽然可以下载附件,但它会在附件旁边显示大小未知,如果我转发该电子邮件,附件不在转发电子邮件中,这似乎是Thunderbird(60.5.2(32位))特有的问题,但由于我们公司完全依靠TB, 我需要修复 python 代码以使其与 TB 兼容。

通过比较在TB中发送的电子邮件附件的来源,我已经使用了TB中"查看源"的原始字符串,该字符串可以工作,但无济于事。

PS:在Mozilla中找到相关的错误跟踪器以供参考:https://bugzilla.mozilla.org/show_bug.cgi?id=548507

import os
import smtplib
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.utils import formataddr
from email.utils import parseaddr
sender = "from@gmail.com"
to_address = ["to@gmail.com"]
subject = "Test subject"
body = "Test body"
title = "title"
attach_files = ['c:\dummy.pdf']
host = 'localhost.com'
user = 'user@mail.com'
password = 'password'
msg_root = MIMEMultipart('related')
addr = '{} <{}>'.format(title, sender)
name, address = parseaddr(addr)
msg_root['From'] = formataddr((
Header(name, 'utf-8').encode(),
address .encode('utf-8') if isinstance(address , unicode) else address))
msg_root['To'] = ','.join(to_address)
msg_root['Subject'] = Header(subject, 'utf-8')
msg_text = MIMEText(body, 'html', 'utf-8')
msg_root.attach(msg_text)
for index, attach_file in enumerate(attach_files, start=1):
with open(attach_file, 'rb') as attach_obj:
attach = MIMEApplication(attach_obj.read(),
_subtype="pdf",
name=os.path.basename(attach_file))
attach.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(attach_file))
msg_root.attach(attach)
connection = smtplib.SMTP_SSL(host=host, timeout=5)
try:
connection.login(user=user, password=password)
connection.sendmail(user, all_address, msg_root.as_string())
finally:
connection.quit()

我可以接受任何答案,只要:转发通过 Python 发送的带有附件的电子邮件时,该电子邮件的附件仍包含在 TB(最新版本 60+)中。

预期结果:附件旁边的文件大小,转发附件电子邮件也将包含附件。

使用'mixed'而不是'related'

msg_root = MIMEMultipart('mixed')

您将在附件旁边看到大小,并且附件将被转发。


TB 60.7.2 (64-bit)/Linux Mint 19.1上进行测试