发送邮件

上图是如何获取QQ邮箱的 授权码。

 

邮件和生成报告文件不能放入文件夹,它们跟文件夹是同级,放在外面就好。

 

使用 yagmail 模块发送邮件更加简单,四行代码
以下是官方文档: https://github.com/kootenpv/yagmail

使用前先要安装 yagmail

pip install yagmail -i https://pypi.douban.com/simple

例子:简单发送邮件

# -*- coding:utf-8 -*-
import yagmail
# 链接邮箱服务器
yag = yagmail.SMTP( user="157540957@qq.com", password="授权码", host='smtp.qq.com')
"""
user:      发送的邮箱
password: 授权码
"""

# 邮箱正文
contents = ['测试发送邮件']

# 发送邮件
yag.send(to = '3437871062@qq.com', subject='subject', contents = contents, attachments="")
"""
to : 接收者
subject : 邮件主题
contents: 正文
attachments: 附件  #附件如果带,不能为空   例如:"D:\\code\\0906\\api_test009\\report\\report.html"
#如不带删除或者填写
attachments = None
""" yag.close() print("邮件发送成功") 

-------------------------------------------------------------------------------------------------------------------------

发送邮件给多个人

邮件发送给多个人,将接受的邮箱放在列表中即可


</br>
</br>

# 发送邮件
yag.send(to = ['3437871062@qq.com','2222@qq.com', '333@qq.com'], subject='subject', contents = contents, attachments="")


------------------------------------------------------------------------------------------------------------

发送邮件带附件

# -*- coding:utf-8 -*-
import yagmail

yag = yagmail.SMTP( user="157540957@qq.com",
                    password="kayzilfyziulbhbb1",
                    host='smtp.qq.com')
"""
user:      发送的邮箱
password: 授权码
"""
# 邮箱正文
contents = ['测试发送邮件']
# 附件
attachments = "D:\\code\\0906\\api_test009\\report\\report.html"
# 发送邮件
try:
    yag.send(to = '3437871062@qq.com',
             subject='subject',
             contents = contents,
             attachments=attachments)

except Exception as e :
    print("Error: 抱歉!发送邮件失败。", e)
"""
to : 接收者
subject : 邮件主题
contents: 正文
attachments: 附件
"""

yag.close()

-----------------------------------------------------------------------------------------------

封装


# -*- coding:utf-8 -*-
import yagmail


def send(user, password, receiver):
    yag = yagmail.SMTP( user=user,
                        password=password,
                        host='smtp.qq.com')
    """
    user:      发送的邮箱
    password: 授权码
    """
    # 邮箱正文
    contents = ['测试发送邮件']
    # 附件
    attachments = "D:\\code\\0906\\api_test009\\report\\report.html"
    # 发送邮件
    try:
        yag.send(to=receiver,
                 subject='subject',
                 contents = contents,
                 attachments=attachments)

    except Exception as e :
        print("Error: 抱歉!发送邮件失败。", e)
    """
    to : 接收者
    subject : 邮件主题
    contents: 正文
    attachments: 附件
    """

    yag.close()


if __name__ == '__main__':
    send("157540957@qq.com", "kayzilfyziulbhbb1", "3437871062@qq.com")

 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

单个发送举例!!!!
# -*- coding:utf-8 -*-
import yagmail
# 链接邮箱服务器
yag = yagmail.SMTP( user="2455267861@qq.com", password="wlvcxapkhzmdecge", host='smtp.qq.com')
"""
user: 发送的邮箱
password: 授权码
"""
# 邮箱正文
contents = ['测试发送邮件']
attachments = "E:\\learn\\ApiTest\\report\\report.html"

# 发送邮件
yag.send(to = '2312624058@qq.com', subject='测试', contents = contents, attachments=attachments)
"""
to : 接收者
subject : 邮件主题
contents: 正文
attachments: 附件
"""
yag.close()
print("邮件发送成功")

封装用例,发邮件。举例!!!
# -*- coding:utf-8 -*-
import unittest
import yagmail #封装用例发送邮件,第一步
from commen import HTMLTestRunner
import os
current_path = os.getcwd() # 获取当前路径
caseDir = os.path.join(current_path, 'case') # 用例路径

# 获取报告的路径
reportDir = os.path.join(current_path, 'report')
reportHtmlDir = os.path.join(reportDir, 'report.html')

def allCase():
# caseDir = "E:\\learn\\ApiTest\\case" #这里是用例的文件路径要转义所以再加一个\
discover = unittest.defaultTestLoader.discover(caseDir,
pattern="test*.py")
print(discover)
return discover

user = "2455267861@qq.com" #以下语法固定词往下,第二步
password = "wlvcxapkhzmdecge"
receiver = '2312624058@qq.com'
def send_mail(user, password, receiver):
yag = yagmail.SMTP( user=user,
password=password,
host='smtp.qq.com')
"""
user: 发送的邮箱
password: 授权码
"""
# 邮箱正文
contents = ['测试发送邮件']
# 附件
attachments = "E:\\learn\\ApiTest\\report\\report.html"
# 发送邮件
try:
yag.send(to= receiver,
subject='测试',
contents = contents,
attachments=attachments)

except Exception as e :
print("Error: 抱歉!发送邮件失败。", e)
"""
to : 接收者
subject : 邮件主题
contents: 正文
attachments: 附件
"""

yag.close()
#语法到这还有一步

if __name__ == '__main__':
# reportDir = "E:\\learn\\ApiTest\\report\\report.html" # 定义报告的路径
fb = open(reportHtmlDir, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fb,
title="教育系统自动化测试报告",
description="用例执行情况")

runner.run(allCase())
fb.close()

# 发邮件
send_mail("2455267861@qq.com", "wlvcxapkhzmdecge", "2312624058@qq.com")
#第三步,结束
posted @ 2021-12-24 15:01  权释  阅读(501)  评论(0)    收藏  举报