我们正在尝试发送带有附件的电子邮件,但由于某种原因,使用 Outlook 的人无法看到附件。
如果他们将电子邮件转发给使用 gmail 的人,它会在 gmail 中显示得很好。
如果 gmail 用户将电子邮件转发给 Outlook 用户,它会显示在 Outlook 中(可能是因为 gmail 重建了邮件)。
这是我们用来发送电子邮件的代码:
def send_email(headers={}, attachments=[], body={}):
ADDRESS_HEADERS = set(['from', 'to', 'cc', 'bcc', 'reply-to'])
msg = MIMEMultipart('alternative')
msg.preamble = "You need a MIME-aware email client to read this email.\n"
def add_headers():
def encode_address(v):
(name, address) = parseaddr(v)
name = str(Header(unicode(name), 'utf-8'))
address = address.encode('ascii')
return formataddr((name, address))
for key, value in headers.iteritems():
if not isinstance(value, list):
value = [value]
if key.lower() in ADDRESS_HEADERS:
value = map(encode_address, value)
msg[key.title()] = u';'.join(value)
def set_body():
msg.attach(MIMEText(body.get('text', ''), 'plain', _charset='utf-8'))
if 'html' in body:
msg.attach(MIMEText(body['html'], 'html', _charset='utf-8'))
def attach_file(attachment):
maintype, subtype = attachment['mimetype'].split("/", 1)
part = MIMEBase(maintype, subtype)
filename = attachment['filename']
name = attachment.get('name', os.path.basename(filename))
with open(filename, 'rb') as f:
part.set_payload(f.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=name)
msg.attach(part)
add_headers()
map(attach_file, attachments)
set_body()
composed = msg.as_string()
p = subprocess.Popen("sendmail -t", shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate(composed)
if p.returncode != 0:
raise IOError(u'{}\n\n{}'.format(stdout, stderr).strip())
由于电子邮件实现的碎片化,很难找到任何相关信息。
我们附加的文件是 mime 类型的 excel 文件application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
我正在尝试查找有关正在使用哪个 Outlook 版本的更多信息。