EmailAlert.py
上传用户:lswyart
上传日期:2008-06-12
资源大小:3441k
文件大小:5k
源码类别:

杀毒

开发平台:

Visual C++

  1. #-----------------------------------------------------------------------------
  2. # Name:        EmailAlert.py
  3. # Product:     ClamWin Free Antivirus
  4. #
  5. # Author:      alch [alch at users dot sourceforge dot net]
  6. #
  7. # Created:     2004/28/04
  8. # Copyright:   Copyright alch (c) 2004
  9. # Licence:     
  10. #   This program is free software; you can redistribute it and/or modify
  11. #   it under the terms of the GNU General Public License as published by
  12. #   the Free Software Foundation; either version 2 of the License, or
  13. #   (at your option) any later version.
  14. #   This program is distributed in the hope that it will be useful,
  15. #   but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. #   GNU General Public License for more details.
  18. #   You should have received a copy of the GNU General Public License
  19. #   along with this program; if not, write to the Free Software
  20. #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. #-----------------------------------------------------------------------------
  22. import sys, os, re
  23. import smtplib
  24. # Import the email modules we'll need
  25. from email.MIMEText import MIMEText
  26. from email.MIMEMultipart import MIMEMultipart
  27. import email
  28. import Utils, ThreadFuture
  29. class EmailMsg(MIMEMultipart):
  30.     def __init__(self, From, To, Subject, Body, Reports=()):
  31.         MIMEMultipart.__init__(self)
  32.         self['Date'] =  email.Utils.formatdate(localtime = True)
  33.         self['From'] = From
  34.         self['To'] = To
  35.         self['Subject'] = Subject        
  36.         self.preamble = 'This is a MIME Messagern'
  37.         self.epilogue = 'rn'
  38.         self.attach(MIMEText(Body))        
  39.         for attachment in Reports:
  40.             report = file(attachment, 'rt').read()
  41.             #if sys.platform.startswith('win'):
  42.                 # replace cygwin-like pathes with windows-like
  43.                 #report = re.sub('/cygdrive/([A-Za-z])/', r'1:/', report).replace('/', '\')  
  44.             part = MIMEText(report)
  45.             reprot = None
  46.             part.add_header('Content-Disposition', 'attachment',
  47.                         filename='report.txt')
  48.             self.attach(part)        
  49.             
  50.     def Send(self, host, port=25, username='', password='', wait=False):
  51.         Func = ThreadFuture.Future(self._Send, self['From'], self['To'], self.as_string(), 
  52.                 host, port, username, password)    
  53.         if wait:
  54.             return Func()
  55.                 
  56.     def _Send(From, To, Body, Host, Port, Username, Password):
  57.         error = ''
  58.         # try toi send 3 times before giving up
  59.         for i in range(3):
  60.             try:
  61.                 # Send the email via our own SMTP server.
  62.                 s = smtplib.SMTP()
  63.                 ##s.set_debuglevel(9)
  64.                 s.connect(Host, Port)        
  65.                 if len(Username):
  66.                     s.login(Username, Password)            
  67.                 # To Address can be separated by commas or semicolons
  68.                 if To.find(',') != -1:
  69.                     To = To.split(',')
  70.                 elif To.find(';') != -1:
  71.                     To = To.split(';')            
  72.                 s.sendmail(From, To, Body)
  73.                 s.quit()
  74.                 s.close()
  75.                 print 'Email alert to %s has been sent successfully.' % To
  76.                 return (True, '')
  77.             except Exception, e:
  78.                 error = str(e)
  79.                 print 'Could not send an email. Error: %s' % error                                        
  80.         return (False, error)         
  81.     _Send = staticmethod(_Send)    
  82.         
  83. class VirusAlertMsg(EmailMsg):
  84.     def __init__(self, From, To, Subject, Host, Port, 
  85.                 User, Password, Reports=(), Body=None):                                    
  86.         if Body is None:
  87.             # get computer name for the message body                          
  88.             Body = 'ClamWin detected a virus on the following computer: %snn' 
  89.                     'Please review the attached log files for more details.n' % Utils.GetHostName()
  90.         
  91.         self._host = Host
  92.         self._port = Port
  93.         self._user = User
  94.         self._password = Password
  95.         
  96.         EmailMsg.__init__(self, From, To, Subject, Body, Reports)           
  97.                             
  98.     def Send(self,  wait = False):
  99.         return EmailMsg.Send(self, self._host, self._port, self._user, self._password, wait)                        
  100.             
  101.         
  102. class ConfigVirusAlertMsg(VirusAlertMsg):
  103.      def __init__(self, config, Reports=(), Body=None):                    
  104.         VirusAlertMsg.__init__(self, config.Get('EmailAlerts', 'From'),
  105.                             config.Get('EmailAlerts', 'To'), 
  106.                             config.Get('EmailAlerts', 'Subject'),
  107.                             config.Get('EmailAlerts', 'SMTPHost'),
  108.                             int(config.Get('EmailAlerts', 'SMTPPort')),
  109.                             config.Get('EmailAlerts', 'SMTPUser'),
  110.                             config.Get('EmailAlerts', 'SMTPPassword'),
  111.                             Reports, Body)
  112.                         
  113. if __name__ == '__main__':
  114.     import Config     
  115.     config = Config.Settings('ClamWin.conf')    
  116.     config.Read()
  117.     msg = ConfigVirusAlertMsg(config, ('c:\test.txt',))
  118.     msg.Send(wait=True)
  119.     print 'exiting'
  120.