Home  >  Q&A  >  body text

smtp - python中,使用MIMEApplication封装邮件附件,Outlook为什么无法收到邮件附件?

在python中使用MIMEAppliction封装附件,outlook收到此邮件时无附件。

使用MIMEBase封装附件,outlook可以收到附件。

其他客户端、网页端均正常显示,就想搞明白为什么,以后要如何取舍,在网上看到的办法都是使用MIMEApplication,对不明确的附件类型的附件进行统一包装。

两段代码如下,请教为什么?

file_mail = MIMEApplication(open(fileurl,'rb').read())  
file_mail.add_header('Content-Disposition', 'attachment',filename=filename.decode('utf-8').encode('gb2312'))  
msg.attach(file_mail)


part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data 
part.set_payload(open(file, 'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(file)) 
msg.attach(part)
高洛峰高洛峰2714 days ago587

reply all(2)I'll reply

  • 阿神

    阿神2017-04-17 15:01:47

    Never used MIMEApplication. There doesn't seem to be any difference.

    Can you provide code that reproduces the problem? (I have no way of verifying this, and there's something missing in your code, so the constructed email may or may not have the problem you're experiencing.)

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:01:47

    Oou, I'm so happy that someone else has encountered the same problem as me~=w=It's fate~I really rarely go to segmentfault. . If I hadn't encountered another pitfall today, I would like to share it. . .
    I was depressed when I encountered this problem. I couldn’t receive the attachment using Airmail. I carefully studied the content of the original email and found it
    You can take a look at RFC 1341 for this problem. When the header specifies Content-Type: multipart/alternative, if the email client feels that it cannot display the corresponding type of attachment, such as Content-Type: text/x-whatever, it will It will choose not to display. The solution is to specify Content-Type: multipart/mixed or specify other headers, such as this one from the original poster

    pythonclass MIMEApplication(MIMENonMultipart):
        """Class for generating application/* MIME documents."""
    
        def __init__(self, _data, _subtype='octet-stream',
                     _encoder=encoders.encode_base64, **_params):
    

    It specifies _subtype='octet-stream'
    _subtype is not specified by default in MIMEBase

    I raised an issue about this issue to the Python envolop library, but why don’t you offend me. . In my project, I can only inherit and override the to_mime_message function of this library. . .

    reply
    0
  • Cancelreply