zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    网游内存数据库的设计(1)
    基于用户级线程的远程调用效率测试
    实现c协程
    KendyNet for linux
    开源一个lua的网络库
    C语言重写网络发送/接收封包
    C协程使用举例
    各种内存分配器的对比测试
    KendyNet性能测试
    C协程实现的效率对比
  • 原文地址:https://www.cnblogs.com/boosli/p/14457423.html
Copyright © 2011-2022 走看看