zoukankan      html  css  js  c++  java
  • 发送邮件——stamplib

    配置文email.ini件信息:

    [email]
    sender=xxxxxxxxxxx
    pwd=xxxxxxxxxxxx
    reciver=xxxxxxxxxxxxx
    python 3.x代码如下:

    import os,configparser,time,requests,hashlib,json
    from email.header import Header
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.mime.application import MIMEApplication
    def filePath(path):
    return os.path.join(os.path.abspath(os.path.dirname(os.path.dirname(__file__))),path)

    def getEmailData():
    #获取配置文件email信息
    cf=configparser.ConfigParser()
    ConfigPath=filePath('config\email.ini')
    cf.read(ConfigPath)
    form_addr=cf.get('email','sender')
    pwd=cf.get('email','pwd')
    to_addr=cf.get('email','reciver')
    return form_addr,pwd,to_addr
    def sendEmail(report_path,report_name):
        #发送邮件
      '''
      report_path:发送的文件路径
      report_name:发送的文件命名
    '''
    from_add,pwd,to_user=getEmailData()
    fp=open(report_path,'rb')
    mail_body=fp.read()
    fp.close()
    msg=MIMEMultipart()
    smtp_server='smtp.exmail.qq.com'
    msg['From']=Header(from_add)
    msg['To']=Header(to_user)
    msg['Subject']=Header(u'私家云接口测试报告','utf-8')
    msg['date']=time.strftime('%Y%m%d%H%M')
    #发送内容
    textpart=MIMEText(mail_body,_subtype='html',_charset='utf-8')
    msg.attach(textpart)
    #以html附件形式发送
    htmlpart=MIMEApplication(open(reportPath(),'rb').read())
    htmlpart.add_header('Content-Disposition','attachment',filename=report_name)
    msg.attach(htmlpart)
    #发送邮件
    try:
    s=smtplib.SMTP(smtp_server,25)
    s.login(from_add,pwd)
    s.sendmail(from_add,to_user.split(','),msg.as_string())
    s.quit()
    log.info(u'邮件发送成功!')
    except smtplib.SMTPRecipientsRefused as err:
    log.error(u'邮件发送失败!原因为:'+err)
    except smtplib.SMTPAuthenticationError as err:
    log.error(u'邮件发送失败!原因为:'+err)
    except smtplib.SMTPException as err:
    log.error(u'邮件发送失败!原因为:'+err)




  • 相关阅读:
    IE6-9中tbody的innerHTML不能赋值bug
    matchesSelector及低版本IE中对该方法的实现
    JavaScript日期组件的实现
    IE6/7/8中parseInt第一个参数为非法八进制字符串且第二个参数不传时返回值为0
    子程序设计原则
    仅IE6中链接A的href为javascript协议时不能在当前页面跳转
    JavaScript获取图片的原始尺寸
    JavaScript判断图片是否加载完成的三种方式
    Mac OS X 快捷键
    IE6-8中Date不支持toISOString方法
  • 原文地址:https://www.cnblogs.com/langhuagungun/p/9028566.html
Copyright © 2011-2022 走看看