2017-05-21 53 views
0

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

回答

0

我發現爲什麼附上了修剪。我忘記關閉文件處理程序,然後再執行我的發送郵件功能。