最新消息:

重新封包的python smtplib

python admin 3242浏览 0评论
##
##******************************************************************************
##   NAME:       SendMail
##   PURPOSE:
##
##   REVISIONS:
##   Ver        Date        Author           Description
##   ---------  ----------  ---------------  ------------------------------------
##   1.0        2010/06/28  Litrin J.        1. Created this Script.
##
##   NOTES:
##
##      Object Name:     SendMail.py
##      Sysdate:         2010/06/28
##
##      Username:         Litrin Jiang
##      Website:          litrin.net
##
##******************************************************************************
##

import sys
import smtplib
import email
import re

class SendMail:

    MailServer = {}
    MailServer['UserName'] = ''
    MailServer['Password'] = ''                 #SMTP密码
    MailServer['Host'] = ''
    MailServer['SSL'] = False
    MailServer['Port'] = 0

    Subtype = 'html'
    Charset = 'gb2312'

    SenderName = ''
    ReciverName = ''
    Notification = False

    __aRealAddressList = []

    def __init__ (self, From = '', Address='', Subject='', Body='', CCAddress = '', BCCAddress = ''):

        if ((From + Address + Subject + Body) != ''):

            self.Send(self, From, Address, Subject, Body, CCAddress = '')


    def Send(self, From, Address, Subject, Body, CCAddress = '', BCCAddress = ''):

        self.Email = email.MIMEText.MIMEText (Body,_subtype=self.Subtype, _charset=self.Charset)

        self.Email['From'] = self.AddressList(From, self.SenderName, False)

        self.Email['To'] = self.AddressList(Address)

        if CCAddress != '':
            self.Email['Cc'] = self.AddressList(CCAddress)

        if BCCAddress != '':
            self.AddressList(BCCAddress)

        self.Email['Subject'] = self.StrEncode(Subject)

        self.Email['X-Maile'] = '<a title="Python" href="http://www.litrin.net/tag/python/">Python</a> Script by Litrin.net'
        self.Email['X-Python-Version'] = str(sys.platform) + str(sys.version_info)

        if (self.Notification) :
            self.Email['Disposition-Notification-To'] = From


        self.__ServerSending()

    def StrEncode(self, String, Charset = ''):
        if Charset == '':
            Charset = self.Charset

        return str(email.Header.Header (String, Charset))

    def AddressList(self, oAddress, ReciverName = '', SendingList = True):

        if isinstance(oAddress, dict):
            To = ''
            for dAddress in oAddress :
                To += self.AddressList(oAddress[dAddress],dAddress )  + ','
            return To

        elif isinstance(oAddress, list):
            To = ''
            for sAddress in oAddress :
                To += self.AddressList (sAddress, '') + ','
            return To

        else:

            try :
                self.CheckEmail(oAddress)
            except:
                print oAddress + 'is not a Email address!'
                exit()

            if ReciverName == '':
                ReciverName = self.ReciverName

            if ReciverName != '':
                To = self.StrEncode(ReciverName) + '<' + oAddress + '>'
            else:
                To = oAddress

            if SendingList :
                self.__aRealAddressList.append(oAddress)

            return To


    def CheckEmail(self, sEmailAddress):
        sRegex = r'^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$'
        return re.match(sRegex, sEmailAddress)

    def __ServerSending(self):

        if (self.MailServer['SSL'] == False):

            server = smtplib.SMTP(self.MailServer['Host'],port = self.MailServer['Port'])
        else:
            server = smtplib.SMTP_SSL(self.MailServer['Host'], port = self.MailServer['Port'])


        server.login(self.MailServer['UserName'], self.MailServer['Password'])

        for sAddress in self.__aRealAddressList:

            server.sendmail(self.Email['From'], sAddress, self.Email.as_string())

        server.quit()

if __name__ == '__main__':
    mail = SendMail()
    mail.MailServer['UserName'] = ''
    mail.MailServer['Password'] = ''                 #SMTP密码
    mail.MailServer['Host'] = ''
    #mail.MailServer['SSL'] = True
    mail.SenderName = 'Litrin J.'



    mail.Send(From = <a rel="nofollow" target="_blank" href="mailto:'abc@123.com'">'abc@123.com'</a>,
              Address = <a rel="nofollow" target="_blank" href="mailto:'abc@71inc.com'">'abc@71inc.com'</a>,
              Subject = 'Subject:test',
              Body = '<b>This is a test mail!</b>')

转载请注明:爱开源 » 重新封包的python smtplib

您必须 登录 才能发表评论!