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 = "[email protected]"
    receiverList = ["[email protected]"]
    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')

你可能感兴趣的:(python)