• 自动化测试基础篇--Selenium发送测试报告邮件


    发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需import即可使用。smtplib模块主要负责发送邮件,email模块主要负责构造邮件。其中MIMEText()定义邮件正文,Header()定义邮件标题。MIMEMulipart模块构造带附件。

    Selenium发送邮件流程:

    一、网易邮箱

    Selenium发送邮件步骤:

    1、导入smtplib和email模块;

    2、准备发邮件的参数,每个邮箱的发件服务器都不一样,以163为例,百度搜到发件服务器为:smtp.163.com;

    3、接下来就是写邮件的主题和正文内容,正文这里用html格式的;

    4、最后调用发件服务。

    5、参考代码

    import smtplib
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    
    # 以yeah邮箱为例
    # ----------------发件相关参数----------------
    smtpserver = 'smtp.163.com'
    port = 0
    sender = 'sanzang520@yeah.net'
    password = 'xxxxxxxxxxxx'
    receicer = 'sanzang520@126.com'
    
    # ----------------编辑邮件内容----------------
    subject = '发送邮件测试'
    body = '<p>发送邮件测试Test<p>'
    msg = MIMEText(body, 'html', 'UTF-8')
    msg['from'] = sender
    msg['to'] = receicer
    msg['subject'] = subject
    
    # ------------------发送邮件-----------------
    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(sender, password)
    smtp.sendmail(sender, receicer, msg.as_string())
    smtp.quit()

    二、腾讯邮箱

    Selenium发送邮件步骤:

    1、导入smtplib和email模块;

    2、腾讯邮箱是需要SSL认证的,找到QQ邮箱授权码,打开QQ邮箱-设置-账号-POP3开启服务-开启;

    3、发验证短信获取授权码,照着提示发个短信,如何点我已发送,就会收到授权码;

    4、收到授权码后复制,保存下来,这个就可以当QQ邮箱的密码;

    5、接下来就是写邮件的主题和正文内容,正文这里用html格式的;

    6、最后调用发件服务。

    7、参考代码

    import smtplib
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    # 以QQ邮箱为例
    # ----------------发件相关参数----------------
    smtpserver = 'smtp.qq.com'
    port = 0
    sender = '2215358510@qq.com'
    password = '授权码'
    receicer = 'sanzang520@126.com'
    
    # ----------------编辑邮件内容----------------
    subject = '发送邮件测试'
    body = '<p>发送邮件测试Test<p>'
    msg = MIMEText(body, 'html', 'UTF-8')
    msg['from'] = sender
    msg['to'] = receicer
    msg['subject'] = subject
    
    # ------------------发送邮件-----------------
    smtp = smtplib.SMTP_SSL(smtpserver,port)
    smtp.login(sender, password)
    smtp.sendmail(sender, receicer, msg.as_string())
    smtp.quit()

    三、同时兼容网易类和腾讯类邮箱

    四、多个收件人

    1、把receiver参数改成list对象,单个多个都是可以收到的;

    2、msg["to"]这个参数不能用list了,得先把receiver参数转化成字符串。

    五、发送附件

    六、参考代码

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Author  : chen
    # @File    : c.py
    # @Software: PyCharm
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    # 同时兼容网易类和腾讯类邮箱
    # ----------------发件相关参数----------------
    smtpserver = 'smtp.qq.com'
    port = 0
    sender = '2215358510@qq.com'
    password = '授权码'
    receicer = ['sanzang520@126.com','sanzang520@yeah.net',]
    
    # ----------------编辑邮件内容----------------
    subject = '发送邮件测试'
    body = '<p>发送邮件测试Test<p>'
    msg = MIMEText(body, 'html', 'UTF-8')
    msg['from'] = sender
    msg['to'] = ';'.join(receicer)
    msg['subject'] = subject
    
    # 文字部分
    part = MIMEText('TEST!!!')
    msg.attach(part)
    # 附件部分
    #---xlsx类型附件---
    part = MIMEApplication(open('D:\test.xlsx','rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="test.xlsx")
    msg.attach(part)
    # jpg类型附件(png类型和jpg一样)
    part = MIMEApplication(open('D:\test.jpg','rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="test.jpg")
    msg.attach(part)
    # pdf类型附件
    part = MIMEApplication(open('D:\test.pdf','rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="test.pdf")
    msg.attach(part)
    # mp3类型附件
    part = MIMEApplication(open('D:\test.mp3','rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="test.mp3")
    msg.attach(part)
    # html类型
    part = MIMEText('<html><h1>test!</h1></html>','html','utf-8')
    msg.attach(part)
    
    # ------------------发送邮件-----------------
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(sender, password)
    except:
        smtp = smtplib.SMTP_SSL(smtpserver,port)
        smtp.login(sender, password)
    smtp.sendmail(sender, receicer, msg.as_string())
    smtp.quit()
    写在最后的话:这些都是小编自己一个字一个字敲上去的,原创算不上,可能很多类似的资料,小编写这个的目的是为了激励自己在学习道路上养成良好的习惯,所以转载请注明出处,谢谢!

     

  • 相关阅读:
    字符串与数组的相互转换
    临时笔记-react实战
    临时笔记-react-router
    vuejs上传图片| table的data更新了,但插槽的数据不能及时更新
    Intellij IDEA软件使用教程
    Git软件使用教程
    阿里程序员常用的 15 款开发工具
    Office后缀含义
    Project软件使用教程
    PowerDesigner软件使用教程
  • 原文地址:https://www.cnblogs.com/sanzangTst/p/8377870.html
Copyright © 2020-2023  润新知