zoukankan      html  css  js  c++  java
  • python+selenium+unittest 搭建web自动化测试框架(八)

    自动发送邮件 configEmail.py

    import os
    import smtplib
    import base64
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
     
     
    class SendEmail(object):
        def __init__(self, username, passwd, recv, title, content,
                     file=None, ssl=False,
                     email_host='smtp.qq.com', port=25, ssl_port=465):
            self.username = username  # 用户名
            self.passwd = passwd  # 密码
            self.recv = recv  # 收件人,多个要传list ['a@qq.com','b@qq.com]
            self.title = title  # 邮件标题
            self.content = content  # 邮件正文
            self.file = file  # 附件路径,如果不在当前目录下,要写绝对路径
            self.email_host = email_host  # smtp服务器地址
            self.port = port  # 普通端口
            self.ssl = ssl  # 是否安全链接
            self.ssl_port = ssl_port  # 安全链接端口
     
        def send_email(self):
            msg = MIMEMultipart()
    
            # 发送内容的对象
            if self.file:  # 处理附件的
                print(self.file)
                try:
                    f = open(self.file, 'rb').read()
                except Exception as e:
                    raise Exception('附件打不开!!!!')
                else:
                    att = MIMEText(f, "base64", "utf-8")
                    att["Content-Type"] = 'application/octet-stream'
                    file_name = os.path.split(self.file)[1] # 只取文件名,不取路径
      
                    new_file_name = '=?utf-8?b?' + base64.b64encode(file_name.encode()).decode() + '?='
                    # 这里是处理文件名为中文名的,必须这么写
                    att["Content-Disposition"] = 'attachment; filename="%s"' % (new_file_name)
                    msg.attach(att)
            msg.attach(MIMEText(self.content))  # 邮件正文的内容
            msg['Subject'] = self.title  # 邮件主题
            msg['From'] = self.username  # 发送者账号
            msg['To'] = ','.join(self.recv)  # 接收者账号列表
            if self.ssl:
                self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.ssl_port)
            else:
                self.smtp = smtplib.SMTP(self.email_host, port=self.port)
            # 发送邮件服务器的对象
            self.smtp.login(self.username, self.passwd)
            self.smtp.sendmail(self.username, self.recv, msg.as_string())
    
            self.smtp.quit()
     
    
    
    if __name__ == '__main__':
     
        m = SendEmail(
            username='xxxxxx@qq.com',
            passwd='geazxbbaldbgbj',  #授权码,还要在163邮箱里添加xxxxxx@qq.com为白名单
            recv=['ssss@163.com'],
            title='2封信',
            content='测试发送邮件',
            file=None,
            ssl=False
        )
        m.send_email()
  • 相关阅读:
    配置管理puppet
    ruby安装
    angularjs 安装篇
    idea 快捷键
    rabbitmq java queue
    spring cloud bus rabbitmq
    rabbitmq 安装篇
    spring cloud eureka
    spring cloud config
    postgre 导入sql文件
  • 原文地址:https://www.cnblogs.com/huaniaoyuchong/p/13919813.html
Copyright © 2011-2022 走看看