【问题标题】:Python: What is the correct MIME type for attaching html file to emailPython:将html文件附加到电子邮件的正确MIME类型是什么
【发布时间】:2016-02-19 10:32:49
【问题描述】:

我有一个脚本可以发送包含 html 内容的电子邮件。按预期工作... 我无法通过电子邮件发送附件。

附件是保存在脚本活动目录中的html文件...“test.html”

如何将 html 文件附加到电子邮件中?我已经从我发现的与此相关的各种其他帖子中尝试了 sn-ps,但每个帖子都返回了相同的输出“没有这样的文件或目录”。

代码如下:

import smtplib
import os
import email.encoders
import email.mime.text
import email.mime.base
import mimetools
import base64



from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

    # me == Outgoing email address
    # you == Recipient's email address
me = "secret"
you = "secret"

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

    # Create the body of the message (a plain-text and an HTML version).
html = """\
    <html>
    <head></head>
    <body>test</p>
</body>
</html>"""


    # Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
filename = "C:/CHRIS/ServerStatus/Oceaneering_Server_Status.html"
f = file(filename)
attachment = MIMEText(f.read(), _subtype='html')
attachment.add_header('Content-Disposition', 'attachment', filename=filename)

    # 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(part1)
msg.attach(part2)
msg.attach(attachment)

    # Send the message via local SMTP server.
mail = smtplib.SMTP('smtp.gmail.com', 587)

mail.ehlo()

    # mail.login(username, password) to Outgoing email account
mail.login('secret', 'secret')
mail.sendmail(me, you, msg.as_string())
mail.quit()

我已经更新了我的代码,希望让这个问题回到主题......在 Dirk 和这个链接的帮助下,我取得了一些进展: Attach a txt file in Python smtplib...

我现在已经能够实际发送附件,但附件仍以文本类型文件的形式出现,并且无法像原始 html 文件那样打开。

所以改写我的问题...更改此代码的 MIME 类型以正确地将 .html 文件附加到基于 html 的电子邮件的纠正措施是什么?

我的py脚本和需要发送的html文件的相对路径和目录如下: C:\CHRIS\ServerStatus\

这是我收到的带有代码的输出:

这是 html 文档在电子邮件脚本之外的外观(它应该看起来的样子):

【问题讨论】:

  • 这段代码在 Linux 上适用于我,带有一些随机的 html 文件,在 gmail 中接收邮件,所以恐怕我没有看到你的问题......它可能与字符集有关、文件编码或您正在使用的邮件客户端。官方文档:docs.python.org/2/library/email-examples.html#id2 注意:你看过安装信封并使用下面的代码吗?这应该处理细节、跨平台问题等。
  • 感谢 dirk,我使用过信封模块,但无法正常工作。我最终将这段代码切换为在没有 ssl 的 smtp 25 上运行并在我们拥有的服务器上运行,但出于测试目的,我使用 gmail 587 构建了它。在这个范围之外,整个代码还有更多内容,我有一种感觉,这需要我对我使用字符集和编码进行过很多更改,但仍然没有运气。我用图像更新了原始帖子以反映 html 文档。
  • Dirk,当我使用信封模块复制您的代码时,我收到“切片对象没有属性较低”的错误..?
  • “切片...”错误可能与您为路径或电子邮件地址或其他内容传递的字符串有关。堆栈跟踪会有所帮助...
  • 但是,看看截图,真正的罪魁祸首很可能不是 Python 代码,而是你理解 html 文件的工作原理......从你显示的内容来看,html 文件看起来不错,除了它缺少样式(可能还有功能)。我敢打赌,这是因为原始文件中引用的某些 css 和/或 javascript 文件不可用于电子邮件副本。您可以通过在 html 中包含任何所需的标记/样式内联来解决此问题,但您需要更改文件:matthewjamestaylor.com/blog/…

标签: python html email smtp


【解决方案1】:

我会鼓励你使用库而不是处理 -rather unpythonic- 内置邮件模块,例如强烈推荐的信封:

https://tomekwojcik.github.io/envelopes/index.html

安装:

pip install envelopes

Python 代码:

import os
from envelopes import Envelope

filename = "C:/CHRIS/ServerStatus/Oceaneering_Server_Status.html"

envelope = Envelope(
    from_addr=(me),
    to_addr=(you),
    subject=u'Test',
    text_body=u'Plain text version',
    html_body=html
)
envelope.add_attachment(filename)

envelope.send('smtp.gmail.com', login='secret', password='secret', tls=True)

【讨论】:

  • 对不起,关于那个.. html 文件的目录是“C:\CHRIS\ServerStatus\Server_Status.html”.. 生成电子邮件的 python 脚本的目录是“C:\CHRIS \ServerStatus\ServerStatus.py".. 我可以发送电子邮件,但我无法将 .html 文件物理附加到电子邮件中。
  • 我添加了这些行并制作了附件...但是,附件似乎是以文本格式而不是原始 html 文件发送的? filename = "C:/CHRIS/ServerStatusServer_Status.html" f = file(filename) attachment = MIMEText(f.read()) attachment.add_header('Content-Disposition', 'attachment', filename=filename) msg​​.attach(附件)
  • MIMEText(fp.read(), _subtype='html') 可能是您正在寻找的答案,但我必须同意这会使问题有点偏离主题,除非它被改写。首先是因为这个答案实际上不太适合原始问题,其次是因为它不是对 SO 的有用补充(有问题的人不会找到这个问题,找到问题的人不太可能有你的问题:- ))
  • 谢谢,我已经改写了问题并发布了。希望这又回到了话题?但是,您推荐的更改并未输出我正在寻找的内容.. :(
猜你喜欢
  • 2011-01-13
  • 2013-09-09
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2015-05-28
  • 2017-11-07
  • 2012-12-26
相关资源
最近更新 更多