iro/offer/smtp.py
author Sandro Knauß <knauss@netzguerilla.net>
Tue, 14 Feb 2012 21:31:06 +0100
branchdevel
changeset 166 6d6890f7e3f8
parent 68 iro/anbieter/smtp.py@85fc1e5dea79
child 168 42b55855e3a6
permissions -rw-r--r--
adding smtp provider

# -*- 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/>.

import smtplib
import copy

from ..model.status import Status
from ..error import UnknownOption, NeededOption, NoRoute, NoTyp
from ..offer import providers
from .provider import Provider

class SMTP(Provider):
    def __init__(self, name, config):
        Provider.__init__(self,name,config,["std"],["mail"])
        

    def loadConfig(self):
        """Read options from config"""
        needed=["send_from","host", "port", "user", "password"]

        for n in needed:
            setattr(self,n,None)

        Provider.loadConfig(self)
        self.bTLS = False
        self.bSSL = False

        for (n, v) in self.config:
            if n in needed:
                setattr(self,n,v)
            elif n == "TLS":
                self.bTLS = bool(v)
            elif n == "SSL":
                self.bSSL = bool(v)
            else:
                raise UnknownOption(self.name, n)

        for n in needed:
           if getattr(self,n) is None:
               raise NeededOption(self.name, n) 

    def send(self,mail,recipient):   
        try:
            if not self.testmode:
                if self.bSSL:
                    smtp = smtplib.SMTP_SSL(self.host,self.port)
                else:
                    smtp = smtplib.SMTP(self.host,self.port)
                
                if self.bTLS:
                    smtp.starttls()
               
                if not self.user == "":
                    smtp.login(self.user,self.pw)
            try:
                frm=self.send_from
                
                if mail.getFrom():
                    frm = mail.getFrom()
                
                tmpmail=copy.deepcopy(mail)
                tmpmail.content['From'] = frm 
                tmpmail.content['To']=recipient
                if not self.testmode:
                    smtp.sendmail(frm,  recipient, tmpmail.as_string())
                return Status(self, "std")
            finally:
                smtp.quit()
        except Exception as e:
            return Status(self,"std",e)

    def getSendFunc(self, typ, route):
        if typ != "mail":
            raise NoTyp(route)
        elif  route != "std":
            raise NoRoute(route)
        return self.send

providers["smtp"]=SMTP