首页 > 技术文章 > Python-发送邮件

dmtz 2019-06-18 17:17 原文

# -*- coding: utf-8 -*-

'''
@author: Administrator
'''

import os
import smtplib
from smtplib import SMTP
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from readConfig import *




PATH = lambda p: os.path.abspath(
        os.path.join(os.path.dirname(__file__), p)
    )


def send_mail(file_new):
    
    conf = Readconfig()
    # 发送邮箱服务器
    smtpserver=conf.getemailValue('smtpserver')
    sender = conf.getemailValue('sender')
    to_addr_in = conf.getemailValue('receiver')
    to_addr = to_addr_in
    to_addrs = to_addr.split(',')
    # 发送邮箱用户信息
    username = conf.getemailValue('username')
    password = conf.getemailValue('password')
    f = open(file_new)
    #读取测试报告正文
#     mail_body=u"中转场装卸模块自动化测试报告,请查收!"
    mail_body = f.read()
    f.close()

    #通过  模块构造的带附件的邮件如图
    msg = MIMEMultipart()
    #编写html类型的邮件正文,MIMEtext()用于定义邮件正文
    #发送正文
    text = MIMEText(mail_body,'html','utf-8')
    text['Subject'] = Header('自动化测试报告', 'utf-8')
    msg.attach(text)
    #发送附件
    #Header()用于定义邮件标题
    msg['Subject'] = Header('丰驰app自动化测试报告', 'utf-8')
    msg_file = MIMEText(mail_body, 'html', 'utf-8')
    msg_file['Content-Type'] = 'application/octet-stream'
    msg_file["Content-Disposition"] = 'attachment; filename="Ui_Test_Report.html"'
    msg.attach(msg_file)


    msg['from'] = sender  # 发送邮件的人
#     msg['to'] = receiver
    msg['to'] = ",".join(to_addrs)
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username, password)  # 登录的用户名和密码
    smtp.sendmail(msg['from'], to_addrs, msg.as_string())  # 发送邮件
    smtp.quit()
    print('sendmail success')

def getfile(dir_fiile):
    allFileNum = 0 
    baseUrl=dir_fiile
    list = os.listdir(baseUrl)    
    filelist = [] 
    for i in range(0, len(list)):
        #遍历 
        path = os.path.join(baseUrl,list[i])
        allFileNum = allFileNum + 1
        if os.path.isfile(path):  
            filelist.append(list[i])        
        else:
            pass
    return filelist[-2]
# dir_fiile="../report/"
# file=dir_fiile+getfile(dir_fiile)   
# send_mail(file)

if __name__ == '__main__':
    file="report.html"
    send_mail(file)
    
readConfig 的代码:
#coding=utf-8

import os
import configparser
import codecs

# 获取配置文件所在路径
# prjDir =os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+"\\lib"
# configfile_path = os.path.join(prjDir, "config.ini").replace('\\','/')

class Readconfig:
    def __init__(self):
        f = open("config.ini")
        data = f.read()

        self.conf = configparser.ConfigParser()
        self.conf.read("config.ini")

    def getConfigValue(self,name):
        value = self.conf.get('config',name)
        return value

    def getcmdValue(self,name):
        value = self.conf.get('cmd',name)
        return value

    def getemailValue(self,name):
        value = self.conf.get('email',name)
        return value
    
if __name__ == '__main__':
    pass
    # conf=Readconfig()
    # print conf.getcmdValue("viewPhone")

config.ini文件内容如下:

[config]
deviceName = Android
platformName=Android
automationName=Appium
appPackage=com.sf.bulktransit
appActivity=com.sf.bulktransit.view.act.MainActivity

[cmd]
openAppium=node /Applications/Appium.app/Contents/Resources/node_modules/appium/bin/appium.js
stopAppium=pkill node
startServer=adb statr-server
closeServer=adb kill-server
checkPhone=adb get-state
viewPhone=adb devices
viewAndroid=adb shell grep ro.build.version.release /system/build.prop

[email]
smtpserver=hqmail.qq.com
username=11111
password=123456
sender=111@qq.com
receiver=111@qq.com,222@qq.com

 

推荐阅读