iro/anbieter/smtp.py
branchdevel
changeset 166 6d6890f7e3f8
parent 165 dd07766f4fee
child 167 374f34025fa1
equal deleted inserted replaced
165:dd07766f4fee 166:6d6890f7e3f8
     1 # -*- coding: utf-8 -*-
       
     2 #Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>
       
     3 
       
     4 #This program is free software; you can redistribute it and/or modify it under the terms
       
     5 #of the GNU General Public License as published by the Free Software Foundation;
       
     6 #either version 3 of the License, or any later version.
       
     7 #This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
       
     8 #without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
       
     9 #See the GNU General Public License for more details.
       
    10 
       
    11 #You should have received a copy of the GNU General Public License
       
    12 #along with this program; if not, see <http://www.gnu.org/licenses/>.
       
    13 
       
    14 class anbieter:
       
    15     default_conf = ''    # override this
       
    16 import smtplib
       
    17 import ConfigParser
       
    18 import logging
       
    19 import copy
       
    20 
       
    21 logger=logging.getLogger("SMTP")
       
    22 
       
    23 class SMTP():
       
    24     def __init__(self,config_filenames=None,section="smtp"):
       
    25         self.config_filenames=config_filenames
       
    26         self.section=section
       
    27         self.bStart=False
       
    28         self.bTLS=False
       
    29         self.bSSL=False
       
    30         self.max_recipients=1
       
    31         
       
    32 
       
    33     def read_basic_config(self,config_filenames=None):
       
    34         """Read basic options from the config file"""
       
    35         if not (config_filenames is None):
       
    36             self.config_filenames=config_filenames
       
    37         
       
    38         cp = ConfigParser.ConfigParser()
       
    39         cp.read(self.config_filenames)
       
    40         self.config_parser = cp
       
    41         self.send_from=cp.get(self.section, 'send_from')
       
    42         self.host=cp.get(self.section, 'host')
       
    43         self.port=cp.get(self.section, 'port')
       
    44         self.user=cp.get(self.section, 'user')
       
    45         self.pw=cp.get(self.section, 'password')
       
    46         
       
    47         try:
       
    48             self.bTLS=cp.getboolean(self.section, 'TLS')
       
    49         except ValueError:
       
    50             self.bTLS=False
       
    51         
       
    52         try:
       
    53             self.bSSL=cp.getboolean(self.section, 'SSL')
       
    54         except ValueError:
       
    55             self.bSSL=False
       
    56 
       
    57     def prepareSMTP(self):
       
    58         if self.bSSL:
       
    59             self.smtp = smtplib.SMTP_SSL(self.host,self.port)
       
    60         else:
       
    61             self.smtp = smtplib.SMTP(self.host,self.port)
       
    62         
       
    63         if self.bTLS:
       
    64             self.smtp.starttls()
       
    65         
       
    66         if not self.user == "":
       
    67             self.smtp.login(self.user,self.pw)
       
    68         
       
    69         self.bStart=True
       
    70 
       
    71     def sendMail(self,mail,recipients):   
       
    72         logger.debug('SMTP.sendMail(%s,%s)'%(mail,  str(recipients)))
       
    73         if not self.bStart:
       
    74             self.prepareSMTP()
       
    75        
       
    76         frm=self.send_from
       
    77         
       
    78         if mail.getFrom():
       
    79             frm = mail.getFrom()
       
    80         
       
    81         mail.content['From'] = frm 
       
    82 	
       
    83 
       
    84         while len(recipients) > 0:
       
    85             tmp_recipients=recipients[:self.max_recipients]
       
    86 	    tmpmail=copy.deepcopy(mail)
       
    87             tmpmail.content['To']=", ".join(tmp_recipients)
       
    88             logger.debug('self.smtp.sendmail(%s,%s,%s)'%(frm,  str(tmp_recipients), tmpmail.as_string()))
       
    89             self.smtp.sendmail(frm,  tmp_recipients, tmpmail.as_string())
       
    90             self.updateStatus( arranged=tmp_recipients)
       
    91             recipients = recipients[self.max_recipients:]
       
    92         
       
    93         self.shutdownSMTP()
       
    94 
       
    95 
       
    96     def updateStatus(self, arranged=None, failed=None):
       
    97         """is a function that is called, if a new SMS/FAX was send
       
    98         -arranged is non None, if SMS/FAX was sended successfully
       
    99         -failed is non None, if SMS/FAX sending failed
       
   100         the content will be the recipent"""
       
   101         pass
       
   102 
       
   103     def shutdownSMTP(self):
       
   104         self.smtp.quit()
       
   105         self.bStart=False
       
   106     
       
   107