iro/anbieter/smtp.py
changeset 0 a3b6e531f0d2
child 19 fcf8489f1c2f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/anbieter/smtp.py	Thu Oct 22 10:00:01 2009 +0200
@@ -0,0 +1,94 @@
+# -*- coding: utf-8 -*-
+#Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>
+
+#This program is free software; you can redistribute it and/or modify it under the terms
+#of the GNU General Public License as published by the Free Software Foundation;
+#either version 3 of the License, or any later version.
+#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+#See the GNU General Public License for more details.
+
+#You should have received a copy of the GNU General Public License
+#along with this program; if not, see <http://www.gnu.org/licenses/>.
+
+class anbieter:
+    default_conf = ''    # override this
+import smtplib
+import ConfigParser
+
+
+class SMTP():
+    def __init__(self,config_filename=None,section="smtp"):
+        self.config_filename=config_filename
+        self.section=section
+        self.bStart=False
+        self.bTLS=False
+        self.bSSL=False
+        self.max_recipients=1
+
+    def read_basic_config(self,config_filename=None):
+        """Read basic options from the config file"""
+        if not (config_filename is None):
+            self.config_filename=config_filename
+        
+        cp = ConfigParser.ConfigParser()
+        cp.read([self.config_filename])
+        self.config_parser = cp
+        self.send_from=cp.get(self.section, 'send_from')
+        self.host=cp.get(self.section, 'host')
+        self.port=cp.get(self.section, 'port')
+        self.user=cp.get(self.section, 'user')
+        self.pw=cp.get(self.section, 'password')
+        
+        try:
+            self.bTLS=cp.getboolean(self.section, 'TLS')
+        except ValueError:
+            self.bTLS=False
+        
+        try:
+            self.bSSL=cp.getboolean(self.section, 'SSL')
+        except ValueError:
+            self.bSSL=False
+
+
+    def prepareSMTP(self):
+        if self.bSSL:
+            self.smtp = smtplib.SMTP_SSL(self.host,self.port)
+        else:
+            self.smtp = smtplib.SMTP(self.host,self.port)
+        
+        if self.bTLS:
+            self.smtp.starttls()
+        
+        if not self.user == "":
+            self.smtp.login(self.user,self.pw)
+        
+        self.bStart=True
+
+    def sendMail(self,mail,recipients):   
+        if not self.bStart:
+            self.prepareSMTP()
+
+        mail.content['From']=self.send_from
+        while len(recipients) > 0:
+            tmp_recipients=recipients[:self.max_recipients]
+            print tmp_recipients
+            self.smtp.sendmail(self.send_from,  tmp_recipients, mail.as_string())
+            self.updateStatus( arranged=tmp_recipients)
+            recipients = recipients[self.max_recipients:]
+        
+        self.smtp.shutdownSMTP()
+
+
+    def updateStatus(self, arranged=None, failed=None):
+        """is a function that is called, if a new SMS/FAX was send
+        -arranged is non None, if SMS/FAX was sended successfully
+        -failed is non None, if SMS/FAX sending failed
+        the content will be the recipent"""
+        pass
+
+    def shutdownSMTP(self):
+        self.smtp.quit()
+        self.bStart=False
+    
+