SendEmail.py 4.86 KB
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import smtplib  # 用于建立smtp连接
from email.mime.multipart import MIMEMultipart  # 混合MIME格式,支持上传附件
from email.header import Header  # 用于使用中文邮件主题
from email.mime.text import MIMEText  # 邮件需要专门的MIME格式
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from commons.Logging import Logger
log=Logger()

def send_email_text():
    # 1. 编写邮件内容(Email邮件需要专门的MIME格式)
    msg = MIMEText('this is a test email', 'plain', 'utf-8')  # plain指普通文本格式邮件内容
    
    # 2. 组装Email头(发件人,收件人,主题)
    msg['From'] = '328351418@qq.com'  # 发件人
    msg['To'] = '328351418@qq.com'  # 收件人
    msg['Subject'] = 'Api Test Report'  # 邮件主题
    
    # 3. 连接smtp服务器并发送邮件
    smtp = smtplib.SMTP_SSL('smtp.qq.com')  # smtp服务器地址 使用SSL模式
    smtp.login('328351418@qq.com', 'apebatdwkggkbiee')  # 用户名和密码
    smtp.sendmail("328351418@qq.com", "328351418@qq.com", msg.as_string())
    smtp.quit()


def send_email(send_file,send_to=["lixi@diligrp.com","328351418@qq.com"]):
    
    log_path=os.path.dirname(os.path.dirname(__file__))
    log_path=log_path+"/report/test.log"
    msg = MIMEMultipart()  # 混合MIME格式
    msg['From'] = '328351418@qq.com'  # 发件人
    msg['To'] = '175930106@qq.com'  # 收件人
    msg['Subject'] = Header('接口测试报告', 'utf-8')  # 中文邮件主题,指定utf-8编码
    
    text = MIMEText('this is a test email', 'plain', 'utf-8')
    msg.attach(MIMEText(open(send_file,'rb' ).read(), 'html', 'utf-8'))  # 邮件正文添加html格式内容(会丢失css格式)

    att1 = MIMEText(open(send_file, 'rb').read(), 'base64', 'utf-8')  # 二进制格式打开需要添加的附件
    att1["Content-Type"] = 'application/octet-stream'
    att1["Content-Disposition"] = 'attachment; filename="report.html"'  # filename为邮件中附件显示的名字
    
#     att2 = MIMEText(open("../Case_Report/test.log", 'rb').read(), 'base64', 'utf-8')  # 二进制格式打开需要添加的附件
    att2 = MIMEText(open(log_path, 'rb').read(), 'base64', 'utf-8')  # 二进制格式打开需要添加的附件
    att2["Content-Type"] = 'application/octet-stream'
    att2["Content-Disposition"] = 'attachment; filename="test.log"'  # filename为邮件中附件显示的名字
    msg.attach(text)
    msg.attach(att1)
    msg.attach(att2)

    #一下发送日志不会在test.log上,因为提前msg.attach了
    log.info("发送邮件")
    try:
        smtp = smtplib.SMTP_SSL('smtp.qq.com')  # smtp服务器地址 使用SSL模式
        re=smtp.login('328351418@qq.com', 'wveicahctcizbghh')  # 用户名和密码
        smtp.sendmail("328351418@qq.com", send_to, msg.as_string())
        print(re)
#         smtp.sendmail("328351418@qq.com", "328351418@qq.com", msg.as_string())  # 发送给另一个邮箱
#         logging.info("邮件发送完成!")
    except Exception as e:
        log.error(str(e))
        print(e)
    finally:
        smtp.quit()
        log.info("邮件发送完毕")


# 
# f='E:\\EclipseWorkspace\\\WorksapceDemo\\Request-demo\\src\\report\\2019-11-28 15_39_01_result.html'
# send_email(send_file=f)
# fromaddr = '328351418@qq.com'
# password = 'apebatdwkggkbiee'
# toaddrs = ['328351418@qq.com', '328351418@qq.com']
#  
# content = 'hello, this is email content.'
# textApart = MIMEText(content)
#  
# imageFile = 'E:\\EclipseWorkspace\\nong12test\\Request-fresh\\src\\Case_Report\\report.html'
# imageApart = MIMEApplication(open(imageFile, 'rb').read())
# imageApart.add_header('Content-Disposition', 'attachment', filename='report.html')
#  
# #     pdfFile = '算法设计与分析基础第3版PDF.pdf'
# #     pdfApart = MIMEApplication(open(pdfFile, 'rb').read())
# #     pdfApart.add_header('Content-Disposition', 'attachment', filename=pdfFile)
# # 
# # 
# #     zipFile = '算法设计与分析基础第3版PDF.zip'
# #     zipApart = MIMEApplication(open(zipFile, 'rb').read())
# #     zipApart.add_header('Content-Disposition', 'attachment', filename=zipFile)
#  
# m = MIMEMultipart()
# m.attach(textApart)
# m.attach(imageApart)
# #     m.attach(pdfApart)
# #     m.attach(zipApart)
# m['Subject'] = 'TEST REPORT'
# m['From'] = '328351418@qq.com'
# m['to'] = '175930106@qq.com' 
#  
# try:
#     server = smtplib.SMTP('smtp.qq.com')
#     server.login(fromaddr,password)
#     server.sendmail(fromaddr, toaddrs, m.as_string())
#     print('success')
#     server.quit()
# except smtplib.SMTPException as e:
#     print('error:',e) #打印错误
#  
#  
# # file='E:\\EclipseWorkspace\\nong12test\\Request-fresh\\src\\Case_Report\\2019-01-28 17_54_18_result.html'
# # 
# # email(file)