修剪电子邮件中的附件

时间:2017-05-21 17:24:39

标签: python-2.7 email smtp base64 attachment

我通过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())

1 个答案:

答案 0 :(得分:0)

我发现为什么要修剪附件。我忘了在执行发送邮件功能之前关闭文件处理程序。

相关问题