zoukankan      html  css  js  c++  java
  • smtp ssl模式邮件发送与附件添加

    #!/usr/bin/python3
    import os
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    
    
    def main():
    
        sender='1932390299@qq.com'
        receiverList=['1932390299@qq.com','1148750911@qq.com']
        user='1932390299@qq.com'
        emailPwd='gyuagsqxsnonhbhd' #授权码开启
        smtpServer='smtp.qq.com'
        commonPort=465
        emailTitle='Hello,World!'
        htmlPath=r'F:/eclipse/readme/readme_eclipse.html'
        attachPathList=[r'C:\Users\Administrator\PycharmProjects\Supro\src\logs\Info\20190328.log']
        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:
            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)
        
        # ssl 协议安全发送
        smtp=smtplib.SMTP_SSL(host=smtpServer,port=commonPort)
        try:
        
            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()
    

      

  • 相关阅读:
    26_为什么我的删除刷新没有办法删除动态添加的数据呢?
    075_not in (null):这代表是什么意思呢?
    074_form表单中的value值
    025_回调函数没有遍历出数据
    073_模糊查询
    072_jsp追加与刷新数据
    071_为什么要catch return.setCode()?
    070_jstl中的三目表达式
    069_都是查询语句时得事物?
    打印周报模板
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/10629342.html
Copyright © 2011-2022 走看看