如何在python中签名和发送Mimultipart消息?

时间:2018-06-26 20:57:16

标签: python python-2.7 python-2.x m2crypto smime

通常,在python中以这种方式发送带有附件的消息。

import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate

def send_mail(send_from, send_to, subject, text, files=None,
              server="127.0.0.1"):
    assert isinstance(send_to, list)

    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach(MIMEText(msg, 'html'))

    for f in files or []:
        with open(f, "rb") as fil:
            part = MIMEApplication(
                fil.read(),
                Name=basename(f)
            )
        # After the file is closed
        part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
        msg.attach(part)

    smtp = smtplib.SMTP(server)
    smtp.sendmail(send_from, send_to, msg.as_string()) # The problem is here
    smtp.close()

但是,由于要用ꜱᴍɪᴍᴇ签名的部分只是正文和附件,而不是全部元数据,因此我无法执行该操作。

在这种情况下,我需要拆分消息和元数据。但是,我仍然需要使用email.mime.multipart和email.mime.application``来构建附件元数据(同时排除其他元数据)

那么如何在仍然使用ʜᴛᴍʟ格式和附件的情况下创建带有附件的ʜᴛᴍʟ电子邮件?

0 个答案:

没有答案