如何使用Python脚本逐个向邮箱发送EML文件?

3 投票
1 回答
4181 浏览
提问于 2025-04-15 22:55

我想写一个简单的Python脚本,通过指定的SMTP服务器,把从Outlook导出的EML文件发送到一组指定的邮箱。我知道怎么发送普通的邮件,但发送EML文件作为邮件我不知道怎么做,也在谷歌上找不到相关信息。有没有人能帮我一下?这个EML文件其实是HTML格式的,还包含了嵌入的图片。如果有其他的建议也欢迎提供。

1 个回答

3

在这个内容中,我们要基于email模块的例子,来尝试使用带有HTML内容的MIME附件。如果EML格式只是HTML,这个方法应该可以用。

这个例子展示了如何构建一条带有(HTML)附件的消息:

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
  <head></head>
  <body>
    <p>Hi!<br>
       How are you?<br>
       Here is the <a href="http://www.python.org">link</a> you wanted.
    </p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# 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)
#...

撰写回答