如何使用python基于smtp发送邮件

 终于更新啦!!! 兄弟们

之前项目中需要我这边编写自动化脚本并且实现框架搭建以及功能实现,我这边就研究并参考了各位大神的框架搭建方式,其中这位大佬是我见过描述最详细,最细致的了,大家可以关注下:

https://blog.csdn.net/site008/article/details/77622472 

不过我这边文章是整合了大家的智慧以及自身项目的要求去构建的自动发送邮件的功能,在我使用unittest框架执行完自动化测试用例的时候,需要将测试报告发送至指定的人处,所以我就研究了下pywin32,但是没有成功,我这边转头又开始看使用smtp协议,登录163邮箱实现发送邮件的共功能,于是就有了以下的内容:

我的目录级别是这样的,sendemail.py这个文件在case_module下,所以我在构造函数下定义了我的目录等级,并创建和test_case同级目录,并去目录下取最新的html测试报告,以下是我的操作


#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#@Time : 2021/05/26 17:31
#@Author : LiZhe.

import os
import sys
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


class Send_mail():
    def __init__(self):
        # 获取当前目录路径
        self.root_path = sys.path[0]
        # 获取父目录路径
        self.work_path = os.path.abspath(os.path.dirname(self.root_path) + os.path.sep + ".")    # 获取上一级目录的父目录路径
        self.next_path = os.path.abspath(os.path.dirname(self.work_path) + os.path.sep + ".")


    def send_email(self):
        # 创建文件夹 
        test_report = self.next_path + os.sep.join("/test_report".split("/"))
        if not os.path.exists(test_report):
            os.makedirs(test_report)
        # 遍历文件夹内容,取创建日期最新的html测试报告
        lists = os.listdir(test_report)
        lists.sort(key=lambda fn: os.path.getmtime(test_report + "/" + fn),reverse=True)
        file_new = os.path.join(test_report,lists[0])
        filename = os.path.basename(file_new)
        # 使用163邮箱基于smtp发送邮件
        mailserver = "smtp.163.com"
        # 填入自己的邮箱名称
        username = "xxx"
        # 填入邮箱号
        username_send = "xxx@163.com"
        # 在163电脑网页版获取授权码,并使用授权码登录
        mail_psssword = "填入获取到的授权码"
        # 填入发送邮箱的邮箱号
        recivers = ["xxx@163.com"]
        massage = MIMEMultipart()
        # 填入正文内容
        part1 = MIMEText("<h>此附件为APP自动化测试结果,请各位查收!!!</h><p>姓名</p><p>正文内容</p><p>xxx</p>","html", 'utf-8')
        # 写入邮箱内容
        with open(file_new,"r")as h:
            content = h.read()
        part2 =MIMEText(content,"html","utf-8")
        part2["Content-Type"] = "application/octet-stream"
        part2["Content-Disposition"] = "attachment;filename=%s" % filename
        massage["Subject"] = "APP_Test_Report"
        massage["From"] = username_send
        massage["To"] = recivers[0]
        massage.attach(part1)
        massage.attach(part2)
        # 发送邮箱
        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(mailserver,25)
            smtpObj.login(username,mail_psssword)
            smtpObj.sendmail(username_send,recivers,massage.as_string())
            smtpObj.quit()
            print "Send Success"

        except smtplib.SMTPException as e:
            print "Send Error",e

if __name__ == "__main__":
    sm = Send_mail()
    sm.send_email()

喜欢博主的,麻烦关注、收藏、点赞哈

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值