Binary file email attachment problem

后端 未结 3 2034
别跟我提以往
别跟我提以往 2021-01-05 06:11

Using Python 3.1.2 I am having a problem sending binary attachment files (jpeg, pdf, etc.) - MIMEText attachments work fine. The code in question is as follows...



        
3条回答
  •  别那么骄傲
    2021-01-05 06:24

    for file in self.attachments:
       fp = open(file,"rb")    
       part = MIMEApplication( fp.read() )    
       fp.close()    
       encoders.encode_base64(part)
    
       # the miracle
       part.set_payload( part.get_payload().decode('ASCII') )
    
       part.add_header('Content-Disposition', 'attachment; filename="%s"' % file)    
       msg.attach(part)   
    

提交回复
热议问题