iro/offer/smtp.py
author Sandro Knauß <knauss@netzguerilla.net>
Sun, 25 Mar 2012 20:14:35 +0200
branchdevel
changeset 253 e8d56537c9cc
parent 220 602720314930
child 269 0d134b173cb1
permissions -rw-r--r--
start documenting with reST syntax.

# -*- 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 functools import partial

from ..validate import vInteger, vEmail,vBool
from ..model.status import Status
from ..config import Option
from .provider import Provider, providers

class SMTP(Provider):
    """A SMTP Provider to send emails.
    This Provider has only one typ ``"mail"`` and one route ``None``. 
    
    If :attr:`~iro.offer.provider.Provider.testmode` is **True** no mail will be send, only a connection is created to server.

    """
    def __init__(self, name):
        Provider.__init__(self,name,{"mail":[None]})
        self.options.update({
            "send_from":Option(vEmail,long="Emailaddress from which mail will be sended.",must=True),
            "host":Option(lambda x,y:x, long="Hostname of MTA", must=True),
            "port":Option(partial(vInteger,minv=0),long="Port of the MTA", default=25),
            "user":Option(lambda x,y:x, long="username to login into MTA.",default=""),
            "password":Option(lambda x,y:x, long="password to login into MTA.",default=""),
            "SSL":Option(vBool,long="use SSL for connection to MTA", default=False),
            "TLS":Option(vBool,long="use TLS for connection to MTA", default=False),
            })
        self.order.extend(["host","port","user","password","SSL","TLS","send_from"])

    def send(self, recipient, mail):   
        """sends a mail to recipient
        
        :param string recipient: A valid email address.
        :param `iro.model.message.Mail` mail: Mail to send.
        :return: 
            - All went ok -- :class:`iro.model.status.Status` object
            - otherwise -- an exception
        """
        if not self.testmode:
            if self.SSL:
                smtp = smtplib.SMTP_SSL(self.host,self.port)
            else:
                smtp = smtplib.SMTP(self.host,self.port)
            
            if self.TLS:
                smtp.starttls()
           
            if not self.user == "":
                smtp.login(self.user,self.password)
        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, None)
        finally:
            smtp.quit()

    def getSendFunc(self, typ, route):
        """returns :meth:`send` method, if typ and route is valid."""

        Provider.getSendFunc(self, typ, route)
        return self.send

providers["smtp"]=SMTP