zoukankan      html  css  js  c++  java
  • python邮件之附件

     接上文,python发附件

    #!/usr/local/bin/python3.5
    from email.mime.text import MIMEText
    from smtplib import SMTP_SSL
    from email.mime.multipart import MIMEMultipart
    from email.mime.base import MIMEBase
    from email import encoders
    
    msg = MIMEText('hello,send by Python...','plain','utf-8')
    
    mail_info = {
            'From':'xxxxxx@xxx.xxxx.xx',
            'Password':'xxx',
            'To':'xxx@xxx.xxx',
            'Mail_server':'smtp.exmail.qq.com',
            'Mail_text':'''
            <html><body><h1>myblog</h1>  <p>send by <a href="http://www.cnblogs.com/changbo">Myblog</a>...</p>  
            </body></html>
    '''
    }
    if __name__ == '__main__':
            smtp = SMTP_SSL(mail_info['Mail_server'])
    
            smtp.ehlo(mail_info['Mail_server'])
            smtp.login(mail_info['From'],mail_info['Password'])
    
            msg = MIMEMultipart()
            msg['Subject'] = 'hello 白水寺'
            msg['From'] = mail_info['From']
            msg['To'] = mail_info['To']
    
            msg.attach(MIMEText(mail_info['Mail_text'],'html','utf-8'))
            with open('/root/2013.jpg','rb') as f:
                    mime = MIMEBase('image','jpg',filename='2013.jpg')
                    mime.add_header('Content-Disposition', 'attachment', filename='2013.jpg')
                    mime.add_header('Content-ID','<0>')
                    mime.add_header('X-Attachment-Id', '0')
                    mime.set_payload(f.read())
                    encoders.encode_base64(mime)
                    msg.attach(mime)
    
            smtp.sendmail(mail_info['From'],mail_info['To'],msg.as_string())
            smtp.quit()

    注意: mime.add_header('Content-Disposition', 'attachment', filename='2013.jpg')

             mime.add_header('Content-ID','<0>')

        mime.add_header('X-Attachment-Id', '0')

    这三行代码是必要的头信息,不要的话附件后缀会是bin,需要手动更改附件后缀

    如图,未加效果如下,该后缀文件不可直接在browser中预览:

    加上附件相关必要的头信息后,效果如下,可直接在browser中预览:

    预览效果图:

    END!

  • 相关阅读:
    省选测试28
    省选测试27
    省选测试26
    省选测试25
    省选测试24
    省选测试23
    省选测试22
    省选测试21
    关于maven 导入依赖的最终处理问题
    Dubbo 2 之抽取公共接口
  • 原文地址:https://www.cnblogs.com/changbo/p/5372932.html
Copyright © 2011-2022 走看看