# -*- 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
import logging
import copy
logger=logging.getLogger("SMTP")
class SMTP():
def __init__(self,config_filenames=None,section="smtp"):
self.config_filenames=config_filenames
self.section=section
self.bStart=False
self.bTLS=False
self.bSSL=False
self.max_recipients=1
def read_basic_config(self,config_filenames=None):
"""Read basic options from the config file"""
if not (config_filenames is None):
self.config_filenames=config_filenames
cp = ConfigParser.ConfigParser()
cp.read(self.config_filenames)
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):
logger.debug('SMTP.sendMail(%s,%s)'%(mail, str(recipients)))
if not self.bStart:
self.prepareSMTP()
frm=self.send_from
if mail.getFrom():
frm = mail.getFrom()
mail.content['From'] = frm
while len(recipients) > 0:
tmp_recipients=recipients[:self.max_recipients]
tmpmail=copy.deepcopy(mail)
tmpmail.content['To']=", ".join(tmp_recipients)
logger.debug('self.smtp.sendmail(%s,%s,%s)'%(frm, str(tmp_recipients), tmpmail.as_string()))
self.smtp.sendmail(frm, tmp_recipients, tmpmail.as_string())
self.updateStatus( arranged=tmp_recipients)
recipients = recipients[self.max_recipients:]
self.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