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

Attaching some files to MIME multipart email

如何使用 Python3 将一些文件附加到 MIME 多部分电子邮件? 我想将一些附件作为 "downloadable content" 发送到我的 HTML-mail(带有纯文本回退)。到目前为止找不到任何东西...

编辑:经过几次尝试,我成功地发送了我的文件。感谢提示 @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']));

我只需要使用 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