zoukankan      html  css  js  c++  java
  • python封装email模块

    一、代码

    from email.mime.text import MIMEText
    from email.header import Header
    from email.utils import parseaddr, formataddr
    from smtplib import SMTP_SSL
    
    
    class MailManager(object):
        smtp_server = 'smtp.qq.com'
    
        def __init__(self, from_addr, password, to_addr, type,name=''):
            '''
            :param from_addr: 发送者
            :param password: 授权码
            :param to_addr: 接受者
            :param type: 发送类型,html,plain
            :param name: 邮件标题
            '''
            self.password = password
            self.from_addr = from_addr
            self.to_addr = to_addr
            self.name = name
            self.type=type
    
        @staticmethod
        def _format_addr(s):
            # 格式化email的头部信息
            name, addr = parseaddr(s)
            return formataddr((Header(name, 'utf-8').encode(), addr))
    
        def newMail(self, content):
            # 创建一个MIMEText对象,content邮件正文内容
            msg = MIMEText(content, self.type, 'utf-8')
            msg['From'] = self._format_addr(self.name + '后台系统 <%s>' % self.from_addr)
            msg['To'] = self._format_addr(self.name + '客服 <%s>' % self.to_addr)
            # 邮件标题
            msg['Subject'] = Header(self.name + '系统通知', 'utf-8').encode()
            return msg
    
        def send(self, msg):
            # 通过ssl方式发送,服务器地址,端口
            server = SMTP_SSL(self.smtp_server, 465)
            # 登录到邮箱
            server.login(self.from_addr, self.password)
            # 发送邮件:发送方,收件方,要发送的消息
            server.sendmail(self.from_addr, [self.to_addr], msg.as_string())
            server.quit()
    
        def SendMail(self, content):
            try:
                msg = self.newMail(content)
                self.send(msg)
            except Exception as e:
                print(e)
    
    if __name__ == '__main__':
        email=MailManager(from_addr="填写自己的",
                          password="填写自己的",
                          to_addr="填写自己的",
                          name="orange",
                          type="html")
        email.SendMail('''
        <p><a href="http://www.baidu.com">这是一个链接</a></p>
        ''')
  • 相关阅读:
    prometheus+alertmanage+grafana安装部署
    HAproxy
    redis安装部署
    rsync+inotify实现实时同步
    简单的计算功能,还需要优化
    python3配置文件的增删改查,记录一下
    一个简单的购物商城,记录一下。
    python函数参数
    python list内部功能记录
    python3 str各个功能记录
  • 原文地址:https://www.cnblogs.com/angelyan/p/11199757.html
Copyright © 2011-2022 走看看