• smtp发送html报告与日志附件图片png


    1.非ssl发送:

    授权码机制,开启smtp,获取授权码以qq邮箱为例:

     

     附件展示:

    #!/usr/bin/python3
    import os
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    from email.mime.image import MIMEImage
    
    def main():
    
        sender='1932390299@qq.com'
        receiverList=['1932390299@qq.com']
        user='1932390299@qq.com'
        emailPwd='gyuagsqxsnonhbhd' #用需开通授权码
        smtpServer='smtp.qq.com'
        commonPort=25
        emailTitle='Hello,World!'
        htmlPath=r'F:/eclipse/readme/readme_eclipse.html'
        attachPathList=[r'C:\Users\Administrator\PycharmProjects\Supro\src\logs\Info\20190330.log',r'C:\Users\Administrator\PycharmProjects\Supro\src\logs\Info\testpc.png']
        emailChanel(sender,receiverList,user,emailPwd,smtpServer,commonPort,emailTitle,htmlPath,attachPathList)
    
    
    def emailChanel(sender,receiverList,user,emailPwd,smtpServer,commonPort,emailTitle,htmlPath=None,attachPathList=None):
        multiPart=MIMEMultipart()
        multiPart['From']=sender
        multiPart['To']=','.join(receiverList)
        subject=emailTitle
        multiPart['Subject']=Header(subject,"utf-8")
        if os.path.isfile(htmlPath):
            if os.path.exists(htmlPath):
                pass
            else:
                raise IOError("htmlPath not exist")
        else:
            raise IOError("html path is not file..")
        emailBody=MIMEText(_text=open(htmlPath,'rb').read(),_subtype='html',_charset="utf-8")
        multiPart.attach(emailBody)
        if isinstance(attachPathList,list):
                for attachPath in attachPathList:
                    if os.path.exists(attachPath):
                        pass
                    else:
                        raise  IOError("attachPath not exist")
        else:
            raise TypeError("expected type is list,but get {}".format(type(attachPathList).__name__))
        for attachPath in attachPathList:
            if os.path.splitext(attachPath)[-1]==".log":
                attach=MIMEText(open(attachPath, 'rb').read(), 'base64', 'utf-8')
                attach["Content-Type"] = 'application/octet-stream'
                attach["Content-Disposition"] = 'attachment; filename="dailyLog.log"' # filename not strict
                multiPart.attach(attach)
            if os.path.splitext(attachPath)[-1]==".png":
                fp = open(attachPath, 'rb')
                msgImage = MIMEImage(fp.read(),_subtype='octet-stream')
                fp.close()
                msgImage.add_header('Content-Disposition', 'attachment', filename="attach.png")
                multiPart.attach(msgImage)
        smtp=smtplib.SMTP(timeout=30)
        try:
            smtp.connect(host=smtpServer,port=commonPort)
            smtp.login(user,emailPwd)
            smtp.sendmail(sender,receiverList,multiPart.as_string())
        except smtplib.SMTPException as e:
            print("send fail",e)
        else:
            print("success")
        finally:
            try:
                smtp.quit()
            except smtplib.SMTPException:
                print("quit fail")
            else:
                print("quit success")
    if __name__ == '__main__':
        main()
    

      

  • 相关阅读:
    (转)使用Nios II 9.1 SP1 SBTE的Flash Programmer的几点注意事项
    [转]linux mysql 更改MySQL数据库目录位置
    2009年第二天被小偷光顾
    [转]几乎没人能逃过的定向思维,我做过了,几乎全对,最后想的是苹果、鼻子跟鸭子
    QQ群里一段推理(恶搞)
    [转]Visio虚线复制到word中变为实线问题的解决办法
    [转]Vmware中提供的与网络通讯的三种网络模式的讲解
    [转]Word 2007书籍排版完全手册
    好好造句
    地产忽悠大全
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10629267.html
Copyright © 2020-2023  润新知