【问题标题】:Python3 multipartmime email (text, email, and attachment)Python3 多部分电子邮件(文本、电子邮件和附件)
【发布时间】:2013-07-13 00:22:51
【问题描述】:

我正在用 Python 创建一些电子邮件,我想要 HTML、文本和附件。我的代码正在“工作”,尽管 Outlook 将其输出显示为 HTML 或文本,而将其他“部分”(电子邮件或 txt)显示为附件。我想拥有电子邮件和文本版本以及文件附件的稳健性。

是否存在基本限制或我犯了错误?

#!/usr/bin/env python3
import smtplib,email,email.encoders,email.mime.text,email.mime.base
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "me@me.com"
you = "you@you.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('mixed')
msg['Subject'] = "msg"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi\nThis is text-only"
html = """\
<html> This is email</html>
"""

part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
#attach an excel file:
fp = open('excelfile.xlsx', 'rb')
file1=email.mime.base.MIMEBase('application','vnd.ms-excel')
file1.set_payload(fp.read())
fp.close()
email.encoders.encode_base64(file1)
file1.add_header('Content-Disposition','attachment;filename=anExcelFile.xlsx')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part2)
msg.attach(part1)
msg.attach(file1)

composed = msg.as_string()

fp = open('msgtest.eml', 'w')
fp.write(composed)
fp.close()

【问题讨论】:

    标签: python smtp


    【解决方案1】:

    我发现这实际上已经得到了回答。奇怪的是搜索功能如何不如“相关”框有效。

    Sending Multipart html emails which contain embedded images

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-03
      • 2021-02-06
      • 2012-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-06-30
      • 1970-01-01
      • 2016-11-30
      相关资源
      最近更新 更多