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

    Python发送邮件分为四步

    • 连接到smtp服务器
    • 登陆smtp服务器
    • 准备邮件
    • 发送邮件

    导入所需要的包

    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服务器

    mtp.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、添加发件人、收件人、邮件主题

    '''
    Python学习交流,免费公开课,免费资料,
    免费答疑,系统学习加群:579817333 
    '''
    # 添加发件人
    msg["From"] = f_user
    # 添加收件人
    msg["To"] = t_user
    # 添加邮件主题
    msg["subject"] = subject
    

    四、发送邮件

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

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

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

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

    2.对发送邮件进行封装

    封装了两个方法:

    • send_text:发送文本邮件

    • send_file:发送文件附件邮件

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

    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)
    
  • 相关阅读:
    新入行程序员考虑自己是否明白以下这8件事情
    程序猿品种总结你是哪一类?
    利用趣味漫画讲述云计算起源
    设计师遇到瓶颈时的五个解困惑建议
    可以帮助到我今后的程序员生涯的十分钟思考
    oc编程中继承的相关实例
    centos和ubuntu服务器Bash漏洞修复方法
    jQuery经典面试题精选
    phpstudy -sql服务器2008r2 -中控考勤机 -php 组合查询考勤
    bat 批处理测试局域网速度 两端电脑
  • 原文地址:https://www.cnblogs.com/xxpythonxx/p/14927597.html
Copyright © 2011-2022 走看看