iro/anbieter/smtp.py
changeset 0 a3b6e531f0d2
child 19 fcf8489f1c2f
equal deleted inserted replaced
-1:000000000000 0:a3b6e531f0d2
       
     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 
       
    19 
       
    20 class SMTP():
       
    21     def __init__(self,config_filename=None,section="smtp"):
       
    22         self.config_filename=config_filename
       
    23         self.section=section
       
    24         self.bStart=False
       
    25         self.bTLS=False
       
    26         self.bSSL=False
       
    27         self.max_recipients=1
       
    28 
       
    29     def read_basic_config(self,config_filename=None):
       
    30         """Read basic options from the config file"""
       
    31         if not (config_filename is None):
       
    32             self.config_filename=config_filename
       
    33         
       
    34         cp = ConfigParser.ConfigParser()
       
    35         cp.read([self.config_filename])
       
    36         self.config_parser = cp
       
    37         self.send_from=cp.get(self.section, 'send_from')
       
    38         self.host=cp.get(self.section, 'host')
       
    39         self.port=cp.get(self.section, 'port')
       
    40         self.user=cp.get(self.section, 'user')
       
    41         self.pw=cp.get(self.section, 'password')
       
    42         
       
    43         try:
       
    44             self.bTLS=cp.getboolean(self.section, 'TLS')
       
    45         except ValueError:
       
    46             self.bTLS=False
       
    47         
       
    48         try:
       
    49             self.bSSL=cp.getboolean(self.section, 'SSL')
       
    50         except ValueError:
       
    51             self.bSSL=False
       
    52 
       
    53 
       
    54     def prepareSMTP(self):
       
    55         if self.bSSL:
       
    56             self.smtp = smtplib.SMTP_SSL(self.host,self.port)
       
    57         else:
       
    58             self.smtp = smtplib.SMTP(self.host,self.port)
       
    59         
       
    60         if self.bTLS:
       
    61             self.smtp.starttls()
       
    62         
       
    63         if not self.user == "":
       
    64             self.smtp.login(self.user,self.pw)
       
    65         
       
    66         self.bStart=True
       
    67 
       
    68     def sendMail(self,mail,recipients):   
       
    69         if not self.bStart:
       
    70             self.prepareSMTP()
       
    71 
       
    72         mail.content['From']=self.send_from
       
    73         while len(recipients) > 0:
       
    74             tmp_recipients=recipients[:self.max_recipients]
       
    75             print tmp_recipients
       
    76             self.smtp.sendmail(self.send_from,  tmp_recipients, mail.as_string())
       
    77             self.updateStatus( arranged=tmp_recipients)
       
    78             recipients = recipients[self.max_recipients:]
       
    79         
       
    80         self.smtp.shutdownSMTP()
       
    81 
       
    82 
       
    83     def updateStatus(self, arranged=None, failed=None):
       
    84         """is a function that is called, if a new SMS/FAX was send
       
    85         -arranged is non None, if SMS/FAX was sended successfully
       
    86         -failed is non None, if SMS/FAX sending failed
       
    87         the content will be the recipent"""
       
    88         pass
       
    89 
       
    90     def shutdownSMTP(self):
       
    91         self.smtp.quit()
       
    92         self.bStart=False
       
    93     
       
    94