zoukankan      html  css  js  c++  java
  • Python发送邮件以及对其封装

    对Python发送邮件进行封装

    Python发送邮件分为四步

    1. 连接到smtp服务器
    2. 登陆smtp服务器
    3. 准备邮件
    4. 发送邮件

    导入所需要的包

    import smtplib
    from email.mime.text import MIMEText
    from email.mime.application import MIMEApplication
    from email.mime.multipart import MIMEMultipart
    

    一、连接到smtp服务器

    方式一:不使用ssl加密

    smtp = smtplib.SMTP(host="smtp.163.com", port=25)
    

    方式二:使用ssl加密

    smtp = smtplib.SMTP_SSL(host="smtp.163.com", port=465)
    

    *注意:传host参数时,如果是QQ邮箱就改成'smtp.qq.com'

    二、登陆smtp服务器

    smtp.login(user="发件人地址", password="授权码")
    

    三、准备邮件

    ①:发送文本邮件

    1、准备内容

    f_user = "发件人地址"
    t_user = "收件人地址"
    content = "邮件的正文"
    subject = "邮件的主题"
    

    2、使用email构造邮件

    msg = MIMEText(content, _subtype='plain', _charset="utf8")
    # 添加发件人
    msg["From"] = f_user
    # 添加收件人
    msg["To"] = t_user
    # 添加邮件主题
    msg["subject"] = subject
    
    ②:发送带附件的邮件

    1、准备内容

    f_user = "发件人地址"
    t_user = "收件人地址"
    content = "邮件的正文"
    subject = "邮件的主题"
    # 读取要发送附件的内容
    file_content = open("附件文件名", "rb").read()
    

    2、使用email构造邮件

    (1)构造一封多组件的邮件

    msg = MIMEMultipart()
    

    (2)往多组件邮件中加入文本内容

    text_msg = MIMEText(content, _subtype='plain', _charset="utf8")
    msg.attach(text_msg)
    

    (3)往多组件邮件中加入文件附件

    file_msg = MIMEApplication(file_content)
    file_msg.add_header('content-disposition', 'attachment', filename='发送附件的名称(可自定义)')
    msg.attach(file_msg)
    

    3、添加发件人、收件人、邮件主题

    # 添加发件人
    msg["From"] = f_user
    # 添加收件人
    msg["To"] = t_user
    # 添加邮件主题
    msg["subject"] = subject
    

    四、发送邮件

    smtp.send_message(msg, from_addr=f_user, to_addrs=t_user)
    

    像这样上面这样写发送邮件,写一次还好,如果说一个项目中多个地方都需要用发送邮件,那就显得笨重了,所以呢,这个时候就需要给上面内容做一个封装,供项目中所有用到发送邮件的地方都可以直接调用.

    一、首先,创建一个配置文件conf.ini

    [email]
    # smtp服务地址
    host = smtp.163.com
    # 端口
    port = 465
    # 发件人
    user = 163邮箱
    # 授权码
    pwd = 授权码
    # 收件人
    to_user = 收件人邮箱
    # 邮件正文
    content = 正文
    # 邮件主题
    subject = 主题
    

    二、对发送邮件进行封装

    封装了两个方法:
    1. send_text:发送文本邮件

    2. send_file:发送文件附件邮件

    3. 以下代码带[]的都是要从配置文件中获取的

    class SendEMail(object):
        """封装发送邮件类"""
    
        def __init__(self):
            # 第一步:连接到smtp服务器
            self.smtp_s = smtplib.SMTP_SSL(host=[host],
                                           port=[port])
            # 第二步:登陆smtp服务器
            self.smtp_s.login(user=[user],
                              password=[pwd])
    
        def send_text(self, to_user, content, subject):
            """
            发送文本邮件
            :param to_user: 对方邮箱
            :param content: 邮件正文
            :param subject: 邮件主题
            :return:
            """
            # 第三步:准备邮件
            # 使用email构造邮件
            msg = MIMEText(content, _subtype='plain', _charset="utf8")
            # 添加发件人
            msg["From"] = [user]
            # 添加收件人
            msg["To"] = to_user
            # 添加邮件主题
            msg["subject"] = subject
            # 第四步:发送邮件
            self.smtp_s.send_message(msg, from_addr=[user], to_addrs=to_user)
    
        def send_file(self, to_user, content, subject, reports_path, file_name):
            """
            发送测试报告邮件
            :param to_user: 对方邮箱
            :param content: 邮件正文
            :param subject: 邮件主题
            :param reports_path: 测试报告路径
            :param file_name: 发送时测试报告名称
            """
            # 读取报告文件中的内容
            file_content = open(reports_path, "rb").read()
            # 2.使用email构造邮件
            # (1)构造一封多组件的邮件
            msg = MIMEMultipart()
            # (2)往多组件邮件中加入文本内容
            text_msg = MIMEText(content, _subtype='plain', _charset="utf8")
            msg.attach(text_msg)
            # (3)往多组件邮件中加入文件附件
            file_msg = MIMEApplication(file_content)
            file_msg.add_header('content-disposition', 'attachment', filename=file_name)
            msg.attach(file_msg)
            # 添加发件人
            msg["From"] = [user]
            # 添加收件人
            msg["To"] = to_user
            # 添加邮件主题
            msg["subject"] = subject
            # 第四步:发送邮件
            self.smtp_s.send_message(msg, from_addr=[user], to_addrs=to_user)
    
  • 相关阅读:
    meta 标签禁止缩放失效
    [UE4]打包EXE
    [UE4]Set Array Elem
    [UML]用例图
    [UE4]函数参数引用
    阻止移动鼠标双击页面放大, no double tap
    spring boot入门 -- 介绍和第一个例子
    SpringBoot 启动错误搜集
    spring boot 启动找不到或无法加载主类
    Spring Boot中Starter是什么
  • 原文地址:https://www.cnblogs.com/desireyang/p/12050426.html
Copyright © 2011-2022 走看看