将一些文件附加到 MIME 多部分电子邮件

Posted

技术标签:

【中文标题】将一些文件附加到 MIME 多部分电子邮件【英文标题】:Attaching some files to MIME multipart email 【发布时间】:2019-04-06 16:48:24 【问题描述】:

如何使用 Python3 ? 我想将一些附件作为“可下载内容”发送到我的 html 邮件(带有纯文本后备)。目前还没有找到任何东西...

编辑:经过几次尝试,我才成功发送文件。感谢您的提示@tripleee。但不幸的是,我的 HTML 现在以纯文本形式发送...

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import formatdate
from os.path import basename
import smtplib

login = "*********"
password = "*********"
server = "smail.*********:25"
files = ['Anhang/file.png']

# Create the root message and fill in the from, to, and subject headers
msg = MIMEMultipart('related')
msg['From'] = "*********"
msg['To'] = "*********"
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "*********"
msg['Reply-To'] = "*********"
msg.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')

with open('*********.txt', 'r') as plainTXT:
    plain = plainTXT.read()
plainTXT.close()
msgAlternative.attach(MIMEText(plain))

with open('*********.html', 'r') as plainHTML:
    html = plainHTML.read()
plainHTML.close()
msgAlternative.attach(MIMEText(html))

msg.attach(msgAlternative)

# Image
for f in files or []:
    with open(f, "rb") as fp:
        part = MIMEImage(
            fp.read(),
            Name=basename(f)
        )
    # closing file
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
    msg.attach(part)


# create server
server = smtplib.SMTP(server)
server.starttls()

# Login Credentials for sending the mail
server.login(login, password)

# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())

server.quit()

print("successfully sent email to %s:" % (msg['To']));

【问题讨论】:

这样的问题很多。您的似乎不包含任何实际附加任何图像的尝试,因此很难看到您需要什么帮助。基本上,创建一个multipart/related 并在其中放置multipart/alternative 和二进制文件为image/png 或其他。如果您真的不希望收件人的邮件客户端尝试呈现它们,您可能需要输入Content-Disposition: attachment 也许这可以帮助一个开始? ***.com/questions/48562935/… 更改了我原来的帖子。现在唯一要做的就是修复纯 html 的东西。谢谢! 【参考方案1】:

我只需要使用MIMEText(html, 'html') 作为我的 HTML 的附加部分。 工作代码:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.utils import formatdate
from os.path import basename
import smtplib

login = "YourLogin"
password = "YourPassword"
server = "SMTP-Server:Port"
files = ['files']

# Create the root message and fill in the from, to, and subject headers
msg = MIMEMultipart('related')
msg['From'] = "FromEmail"
msg['To'] = "ToEmail"
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "EmailSubject"
msg['Reply-To'] = "EmailReply"
msg.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')

with open('YourPlaintext.txt', 'r') as plainTXT:
    plain = plainTXT.read()
plainTXT.close()
msgAlternative.attach(MIMEText(plain, 'plain'))

with open('YourHTML.html', 'r') as plainHTML:
    html = plainHTML.read()
plainHTML.close()
msgAlternative.attach(MIMEText(html, 'html'))

msg.attach(msgAlternative)

# Image
for f in files or []:
    with open(f, "rb") as fp:
        part = MIMEImage(
            fp.read(),
            Name=basename(f)
        )
    # closing file
    part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
    msg.attach(part)


# create server
server = smtplib.SMTP(server)
server.starttls()

# Login Credentials for sending the mail
server.login(login, password)

# send the message via the server.
server.sendmail(msg['From'], msg['To'], msg.as_string())

server.quit()

print("successfully sent email to %s:" % (msg['To']));

感谢@tripleee

【讨论】:

以上是关于将一些文件附加到 MIME 多部分电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

我可以将 S/MIME 作为多部分/混合消息的一部分吗?

在 iOS 上为亚马逊 SES 创建多部分/混合 MIME

如何将 msg 中的附件附加到 Mime 以在 Python 中作为电子邮件发送?

使用 MIMEApplication 将 s3 文件附加到 smtplib lib 电子邮件

在 Golang 中附加文件并通过 SMTP 发送时没有正文部分的电子邮件

电子邮件:MIME,用附件多部分发送?