测试开发

python_email

2016-09-19  本文已影响60人  古佛青灯度流年

发送邮件问题网上案例很多,基本的都差不多,这是我的代码:

# coding=utf-8
__author__ = 'xcma'
import os
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application  import MIMEApplication
from LogMainClass import Log #这是我自定义的log类,如果本地没有可以删掉
from ReadConfig import Read_config#自定义的读取配置文件类
from Utils import ABSpath#自定义获取当前执行文件的:绝对路径
import smtplib
log = Log("Email.py") #还是log类的命名,可以将log相关的全部删掉
def send_mail(send_file_path):   
"""    
:param send_file_path: 测试报告文件路径   
:return:    
"""    
  config_file_path = ABSpath()+"/Src/Conf/Config.ini"    
  category = "email"    
  #读取配置文件中相关信息
  smtpserver = Read_config.return_specific_str(category, "mail_host", config_file_path)    
  smtpuser = Read_config.return_specific_str(category, "mail_user", config_file_path)    
  password = Read_config.return_specific_str(category, "mail_pass", config_file_path)    
  #拼装接收人   --遍历接收人配置项
  mailto = []    
  receive_category = "email_receiver"    
  #返回所有接收人
  receivers = Read_config.return_options(receive_category, config_file_path)    
  for i in receivers:    
    #找到每个接收人对应的邮箱地址    
    receiver = Read_config.return_specific_str(receive_category, i, config_file_path)        
    #将每个人对应的邮箱地址插入mailto列表中
    mailto.append(receiver)    
  msg = MIMEMultipart()    
  #定义发送人    
  msg['From'] = smtpuser   
  #定义接收邮件对象 --群发   
  msg['To'] = ",".join(mailto)    
  #邮件标题    
  msg['Subject'] = Header('自动化测试报告', 'utf-8').encode()    
  msg["Accept-Language"] = "zh-CN"   
  msg["Accept-Charset"] = "ISO-8859-1,utf-8"    
  content = "<h2>!若想查看用例明细,请下载附件查看</h2>"    
  try:       
    sendfile = send_file_path +new_report(send_file_path)        
    file_name = new_report(send_file_path)        
    #将html中内容贴在邮件正文中        
    fp = open(sendfile, 'rb')        
    msg.attach(MIMEText(content+fp.read(), 'html', 'utf-8'))        
    fp.close()        
    # 添加附件        
    fp = open(sendfile, 'rb')        
    part = MIMEApplication(fp.read())        
    fp.close()        
    part.add_header('Content-Disposition', 'attachment', filename=file_name)        
    msg.attach(part)        
    #发送邮件        
    server = smtplib.SMTP_SSL(smtpserver, 465)        
    server.set_debuglevel(1)
    server.login(smtpuser, password)        
    server.sendmail(smtpuser, mailto, msg.as_string())        
    server.quit()    
  except Exception as msg:       
    log.exception(u'邮件发送失败')        
    print msg
def new_report(testreport):    
  '''    将文件按照名字时间顺序排序,输出文件名字   
       :param testreport:测试报告存放路径    
        :return:    
  '''    
   try:       
     lists = os.listdir(testreport)        
     log.debug(u'当前路径中文件列表'+str(lists))        
     lists.sort(key=lambda fn: os.path.getmtime(testreport + '/' + fn))        
     file_new = os.path.join(lists[-1])        
    #返回最新生成的文件名称       
     log.info(u'将要发送的测试报告文件:'+file_new)       
     return file_new    
   except Exception as msg:        
      print msg        
      raise
配置文件.png

总的来说事情坐下来不难,就是小坑比较多

上一篇 下一篇

猜你喜欢

热点阅读