• python邮件发送封装类


    import os
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from common.config import configs

    class EmaliUtils:
    def __init__(self,smtp_body, attch_path =None):
    self.smtp_server = 'smtp.qq.com'
    self.smtp_prot = 25
    self.smtp_send = '***@qq.com'
    self.smtp_password = 'mkfarwmvotfqfgec'
    self.smtp_receiver = configs.SMTP_RECEIVER
    self.smtp_cc = '***@qq.com'
    self.smtp_subject = '自动化邮件发送'
    self.smtp_body = smtp_body
    self.attch_path = attch_path

    def email_message_body(self):
    message = MIMEMultipart()
    message['from'] = self.smtp_send
    message['to'] = self.smtp_receiver
    message['Cc'] = self.smtp_cc
    message['subject'] = self.smtp_subject
    message.attach(MIMEText(self.smtp_body, 'html', 'utf-8'))
    # print(self.attch_path)
    if self.attch_path:
    attach_file_obj = MIMEText(open(self.attch_path,'rb').read(), 'base64', 'utf-8')
    attach_file_obj['Content-Type'] = 'application/octet-stream'
    attach_file_obj.add_header('Content-Disposition', 'atachment',
    filename=('gbk', '', os.path.basename(self.attch_path)))
    message.attach(attach_file_obj)
    return message


    def send_mail(self):
    smtp = smtplib.SMTP()
    smtp.connect(self.smtp_server, self.smtp_prot)
    smtp.login(self.smtp_send, self.smtp_password)
    smtp.sendmail(self.smtp_send, self.smtp_receiver.split(',') + self.smtp_cc.split(','),
    self.email_message_body().as_string())
    smtp.close()




    if __name__ == '__main__':
    email_u = EmaliUtils('自动化邮件发送', '../report')
    email_u.send_mail()
  • 相关阅读:
    Python基础:Python可变对象和不可变对象
    python内置函数
    python元组和序列
    python模块简单使用
    python循环技巧
    皮尔逊积矩相关系数
    Python基础:Python的变量和对象
    统计学中的自由度
    python 速成指南
    在SQL Server中,SQL语句的Insert支持一次插入多条记录
  • 原文地址:https://www.cnblogs.com/boosli/p/14457423.html
Copyright © 2020-2023  润新知