zoukankan      html  css  js  c++  java
  • Python 邮件类

     #-*- MailClassLibrary -*-

    import smtplib
    import os
    import sys
    from email.header import Header
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart 


    class MailClassLibrary:
        def __init__(self, mailType):
            self.mailSendType = mailType
        sender = "xsmhero@aa.com"
        receiverList = ["xsmhero@aa.com"]
        subject = ""
        smtpServer = "www.mail.com"
        userName = "aa@aacom"
        password = "paword"    
        '''  =========================方法================================  '''
        # 中文乱码
        def setChineseGarbled(self, text):
            type = sys.getfilesystemencoding()
            text = text.decode('UTF-8').encode(type)
            return text
        '''  ===========================================================  '''

       
        '''     文件形式的邮件    '''
        def sendContent(self, subject, content):
     msg = MIMEText(content, 'plain', 'utf-8')
     msg['Subject'] = Header(subject, 'utf-8')
     msg['From'] = self.sender
            msg['To'] = ";".join(self.receiverList)

            try:
                sn = smtplib.SMTP()
                sn.connect(self.smtpServer)
                sn.login(self.userName,self.password)
                if(len(self.receiverList) > 1):
                    sn.sendmail(self.sender,self.receiverList ,msg.as_string())
                else:
                    sn.sendmail(self.sender,msg['To'] ,msg.as_string())
                sn.close()
            except Exception, e:
                print str(e)
               
        '''     HTML形式的邮件    '''       
        def sendHtml(self, subject, html):
     msg = MIMEText(html, 'html', 'utf-8')
     msg['Subject'] = Header(subject, 'utf-8')
     msg['From'] = self.sender
            msg['To'] = ";".join(self.receiverList)

            try:
                sn = smtplib.SMTP()
                sn.connect(self.smtpServer)
                sn.login(self.userName,self.password)
                if(len(self.receiverList) > 1):
                    sn.sendmail(self.sender,self.receiverList ,msg.as_string())
                else:
                    sn.sendmail(self.sender,msg['To'] ,msg.as_string())
                sn.close()
            except Exception, e:
                print str(e)

        '''     带图片的HTML邮件    '''
        def sendPicHtml(self, subject, html, picPath):
            picPath = self.setChineseGarbled(picPath)
            msgRoot = MIMEMultipart('related') 
            msgRoot['Subject'] = Header(subject, 'utf-8')
     msgRoot['From'] = self.sender
            msgRoot['To'] = ";".join(self.receiverList)
           
            msgText = MIMEText(html,'html','utf-8') 
            msgRoot.attach(msgText) 
             
            fp = open(picPath, 'rb') 
            msgImage = MIMEImage(fp.read()) 
            fp.close() 
             
            msgImage.add_header('Content-ID', '<image1>') 
            msgRoot.attach(msgImage) 
            try:
                sn = smtplib.SMTP()
                sn.connect(self.smtpServer)
                sn.login(self.userName,self.password)
                if(len(self.receiverList) > 1):
                    sn.sendmail(self.sender,self.receiverList ,msgRoot.as_string())
                else:
                    sn.sendmail(self.sender,msgRoot['To'] ,msgRoot.as_string())
                sn.close()
            except Exception, e:
                print str(e)

       

    '''     带附件的邮件    '''
        def sendAttach(self, subject, content, filePath):
            msgRoot = MIMEMultipart('related')  
            msgRoot['Subject'] = Header(subject, 'utf-8')
    msgRoot['From'] = self.sender
            msgRoot['To'] = ";".join(self.receiverList)
            if(len(self.ccList)>0):
                msg['Cc'] = ";".join(self.ccList)
                msg['Disposition-Notification-To'] = ";".join(self.ccList)
    msgText = MIMEText(content,'html','utf-8') 
            
    msgRoot.attach(msgText)
    #构造附件  
            #注意:传入的参数attfile为unicode,否则带中文的目录或名称的文件读不出来  
            #      basename 为文件名称,由于传入的参数attfile为unicode编码,此处的basename也为unicode编码  
            basename = os.path.basename(filePath)  
            att = MIMEText(open(filePath, 'rb').read(), 'base64', 'utf-8')  
            att["Content-Type"] = 'application/octet-stream'  
            att["Content-Disposition"] = 'attachment; filename="' + basename.encode('utf-8') + '"'  
            msgRoot.attach(att)  
            try:
                sn = smtplib.SMTP()
                sn.connect(self.smtpServer)
                sn.login(self.userName,self.password)
                if(len(self.receiverList) > 1):
                    sn.sendmail(self.sender,self.receiverList ,msgRoot.as_string())
                else:
                    sn.sendmail(self.sender,msgRoot['To'] ,msgRoot.as_string())
                sn.close()
            except Exception, e:
                print str(e)
        '''     带附件的邮件    '''
        def sendAttachs(self, subject, content, filePaths):
            msgRoot = MIMEMultipart('related')  
            msgRoot['Subject'] = Header(subject, 'utf-8')
    msgRoot['From'] = self.sender
            msgRoot['To'] = ";".join(self.receiverList)
            if(len(self.ccList)>0):
                msg['Cc'] = ";".join(self.ccList)
                msg['Disposition-Notification-To'] = ";".join(self.ccList)
    msgText = MIMEText(content,'html','utf-8') 
            
    msgRoot.attach(msgText)
    for i in range(0,len(filePaths)):
       #构造附件
                att = MIMEText(open(filePaths[i], 'rb').read(), 'base64', 'utf-8')
                filePath = os.path.basename(filePaths[i]) 
       att["Content-Type"] = 'application/octet-stream'  
       #att["Content-Disposition"] = 'attachment; filename="' + os.path.basename(filePath) + '"'
       att["Content-Disposition"] = 'attachment; filename="' + filePath.encode('utf-8') + '"'  
       msgRoot.attach(att)  
            
            try:
                sn = smtplib.SMTP()
                sn.connect(self.smtpServer)
                sn.login(self.userName,self.password)
                if(len(self.receiverList) > 1):
                    sn.sendmail(self.sender,self.receiverList ,msgRoot.as_string())
                else:
                    sn.sendmail(self.sender,msgRoot['To'] ,msgRoot.as_string())
                sn.close()
            except Exception, e:
                print str(e)

        '''     基于SSL的邮件    '''
        def sendSSLContent(self, subject, content):
     msg = MIMEText(content, 'text', 'utf-8')
     msg['Subject'] = Header(subject, 'utf-8')
     msg['From'] = self.sender
            msg['To'] = ";".join(self.receiverList)

            try:
                sn = smtplib.SMTP()
                sn.connect(self.smtpServer)
                sn.ehlo()
                sn.starttls()
                sn.ehlo()
                sn.set_debuglevel(1)
                sn.login(self.userName,self.password)
                if(len(self.receiverList) > 1):
                    sn.sendmail(self.sender,self.receiverList ,msg.as_string())
                else:
                    sn.sendmail(self.sender,msg['To'] ,msg.as_string())
                sn.close()
            except Exception, e:
                print str(e)

    #p = MailClassLibrary('text')
    #p.sendAttach('rar','swire.rar','C://Documents and Settings//xusm//桌面//swire.rar')

  • 相关阅读:
    Vmstat主要关注哪些数据?
    Swap是个什么东东?
    Buffers与cached啥区别
    做错的题目——关于构造器返回值
    做错的题目——this的指向
    JS判断一个数是否为质数
    数组扁平化
    JS实现快速排序
    正则实现千分符
    获取鼠标的当前位置
  • 原文地址:https://www.cnblogs.com/xsmhero/p/2493667.html
Copyright © 2011-2022 走看看