Thunderbird "size unknown" 附件与 python 一起发送
Thunderbird "size unknown" attachment when sent with python
当我使用下面的python代码发送带附件的邮件时,虽然可以下载附件,但附件旁边会显示大小未知,如果我转发该邮件,附件不在转发电子邮件,这似乎是 Thunderbird(60.5.2(32 位))特有的问题,但由于我们公司完全使用 TB,因此我需要修复 python 代码以使其兼容结核病
我通过比较在 TB 中发送的电子邮件附件的来源来使用 TB 中 "view source" 来源的原始字符串,但无济于事。
PS: 在 mozilla 中找到相关的 bug tracker 以供参考: 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
当我使用下面的python代码发送带附件的邮件时,虽然可以下载附件,但附件旁边会显示大小未知,如果我转发该邮件,附件不在转发电子邮件,这似乎是 Thunderbird(60.5.2(32 位))特有的问题,但由于我们公司完全使用 TB,因此我需要修复 python 代码以使其兼容结核病
我通过比较在 TB 中发送的电子邮件附件的来源来使用 TB 中 "view source" 来源的原始字符串,但无济于事。
PS: 在 mozilla 中找到相关的 bug tracker 以供参考: 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