pythonメール送信ツールモジュールをカプセル化

13403 ワード

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   Project:     Ai_codes
   IDE Name:    PyCharm
   File Name:   email_utils
   Email:       [email protected]
   Author :       
   Date:        2018/11/13
   Description :
-------------------------------------------------
   Change Activity: 2018/11/13:
-------------------------------------------------
"""
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

#   smtplib     
#       ,           。
smtpserver = 'smtp.163.com'
username = '[email protected]'
password = 'xxxx'
sender = '[email protected]'


class SendEmail():

    def __init__(self, receiver, subject, ):
        '''
                 
        :param receiver:    :list   -['[email protected]','[email protected]']
        :param subject:     
        '''
        self.receiver = receiver
        self.msg = MIMEMultipart('mixed')
        #      ,   ,   ,            。
        self.msg['Subject'] = subject
        self.msg['From'] = '{} '.format(username, username)
        #          ,  join       ;       
        self.msg['To'] = ";".join(receiver)
        self.msg['Date'] = '{}'.format(datetime.datetime.now())

    def add_text(self, text_plain):
        '''
              
        :param text_plain:                  
        :return:
        '''
        text_plain = MIMEText(text_plain, 'plain', 'utf-8')
        self.msg.attach(text_plain)

    def add_img(self, img_name, sendimagefile):
        '''
            
          :
            sendimagefile = open(img_path, 'rb').read()
            send_img('ima_name',sendimagefile)
        :param imgname:     
        :return:
        '''

        image = MIMEImage(sendimagefile)
        image.add_header('Content-ID', '')
        image["Content-Disposition"] = 'attachment; filename="{}"'.format(img_name)
        self.msg.attach(image)

    def add_html(self, html):
        '''
          html
        :param html: html  
        :return:
        '''
        text_html = MIMEText(html, 'html', 'utf-8')
        text_html["Content-Disposition"] = 'attachment; filename="texthtml.html"'
        self.msg.attach(text_html)

    def add_file(self, filename, sendfile):
        '''
            
                :
                    sendfile = open(r'xxxx.xls', 'rb').read()
                    add_file('file_name',sendfile)
        :param filename:     
        :param sendfile:     
        :return:
        '''
        #     
        text_att = MIMEText(sendfile, 'base64', 'utf-8')
        text_att["Content-Type"] = 'application/octet-stream'
        #           aaa.txt
        # text_att["Content-Disposition"] = 'attachment; filename="aaa.txt"'
        #        
        text_att.add_header('Content-Disposition', 'attachment', filename=filename)
        #        ok
        # text_att["Content-Disposition"] = u'attachment; filename="    .txt"'.decode('utf-8')
        self.msg.attach(text_att)

    def send(self):
        #     
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        #    set_debuglevel(1)       SMTP          。
        # smtp.set_debuglevel(1)
        smtp.login(username, password)
        smtp.sendmail(sender, self.receiver, self.msg.as_string())
        smtp.quit()


if __name__ == '__main__':
    em_obj = SendEmail(['[email protected]'],'    ')
    em_obj.add_text('        ,    ')
    em_obj.send()