smtp - python中,使用MIMEApplication封装邮件附件,Outlook为什么无法收到邮件附件?
高洛峰
高洛峰 2017-04-17 14:59:47
0
2
635

在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)
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
阿神

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.)

巴扎黑

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. . .

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!