电子邮件中的修剪附件

Trimmed attachment in e-mail

我通过 python 功能发送电子邮件附件。一切都很好,除了东西,我的附件被修剪了。修剪了大约 200 根琴弦,我不知道它们在哪里松动。我在调试器中检查了我的函数,发现 encoders.encode_base64(part) part.set_payload 之前的大小与 HDD 上的文件大小相同,但结果我收到了经过修剪的附件。

发送邮件功能如下:

def mail_sender(recipients, sender, z_name, z_count=0):
    for recipient in recipients:
        msg = MIMEMultipart()
        sender = '%s' % sender
        subject = "report on %s" % (time.strftime("%d/%m/%Y"))
        body = "Good morning, enjoy todays report.\n\nTotal: %d" % z_count

        msg['From'] = sender
        msg['To'] = recipient
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = subject
        msg.attach(MIMEText(body, 'plain'))

        part = MIMEBase('application', "base64")
        part.set_payload(open("result.txt", "rb").read())
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="result.txt"')
        msg.attach(part)

        s = smtplib.SMTP('localhost')
        s.sendmail(sender, recipient, msg.as_string())

我找到了为什么 attach 被修剪了。我忘记在执行发送邮件功能之前关闭文件处理程序。