【问题标题】:Changing Attachment Header MIME python更改附件标题 MIME python
【发布时间】:2020-03-12 13:54:13
【问题描述】:

我试图将我附加的 pdf 的标题从默认的“noname”更改为其他一些标题。不幸的是,我的代码的以下“add_header”部分不会更改实际的标题名称。是否有任何替代方案不会损坏或更改附件的内容?

fromaddr = 'XXXXX@gmail.com'  
toaddrs  = 'XXXXX@gmail.com'

username = 'XXXX'  
password = 'XXXXX'
msg = MIMEMultipart()
msg['Subject']='Spam'

msg.preamble = 'SDFSFSDF'


file=open(r'XXXXXX.pdf','rb').read()

msg.attach(MIMEApplication(file,'pdf'))

msg.add_header('XXXXXX', 'file', filename = 'XXXXXX.pdf')

server = smtplib.SMTP('smtp.gmail.com', 587)  
server.ehlo()
server.starttls()
server.login(username, password)  
server.sendmail(fromaddr, toaddrs, msg.as_string())  
time.sleep(2)

编辑

这是附加的文件标题:

【问题讨论】:

    标签: python python-3.x email mime


    【解决方案1】:

    你想要这样的东西:

    msg.add_header("Content-Disposition", 'attachment; filename="%s"' % 'XXXXX.pdf')
    

    这将在消息中生成此标头

    'Content-Disposition: attachment; filename="XXXXX.pdf"'
    

    替换原来的add_header调用
    msg.add_header("Content-Disposition", 'attachment; filename="%s"' % 'hello.pdf')
    

    在 smtp 调试服务器中生成此输出*

    b'Content-Type: multipart/mixed; boundary="===============3303386632460914267=="'                                                   
    b'MIME-Version: 1.0'                                                                                                                
    b'Subject: Spam'                                                                                                                    
    b'Content-Disposition: attachment; filename="hello.pdf"'                                                                            
    b'X-Peer: ::1'                                                                                                                      
    b''                                                                                                                                 
    b'SDFSFSDF'                                                                                                                         
    b'--===============3303386632460914267=='                                                                                           
    b'Content-Type: application/pdf'                                                                                                    
    b'MIME-Version: 1.0'                                                                                                                
    b'Content-Transfer-Encoding: base64'                                                                                                
    b''
    b'LotsOfBase64'                        
    

    *python -m smtpd -n -c DebuggingServer localhost:1025

    【讨论】:

    • 你得到什么标题?
    • "noname" 是我得到的标题
    • 我已经添加了我得到的输出。也许你可以edit你的问题来显示生成的确切标题?
    • 我在发送电子邮件时添加了标题的输出。如您所见,它仍然显示“无名”。也许它与更新 MIME 版本有关?
    猜你喜欢
    • 2016-02-20
    • 2012-11-02
    • 2014-04-07
    • 2013-10-06
    • 2021-08-19
    • 1970-01-01
    • 2011-12-20
    • 2015-05-28
    • 2019-03-04
    相关资源
    最近更新 更多