iro/offer/smtp.py
branchdevel
changeset 253 e8d56537c9cc
parent 220 602720314930
child 269 0d134b173cb1
equal deleted inserted replaced
252:d0de2ca7201a 253:e8d56537c9cc
    19 from ..model.status import Status
    19 from ..model.status import Status
    20 from ..config import Option
    20 from ..config import Option
    21 from .provider import Provider, providers
    21 from .provider import Provider, providers
    22 
    22 
    23 class SMTP(Provider):
    23 class SMTP(Provider):
       
    24     """A SMTP Provider to send emails.
       
    25     This Provider has only one typ ``"mail"`` and one route ``None``. 
       
    26     
       
    27     If :attr:`~iro.offer.provider.Provider.testmode` is **True** no mail will be send, only a connection is created to server.
       
    28 
       
    29     """
    24     def __init__(self, name):
    30     def __init__(self, name):
    25         Provider.__init__(self,name,{"mail":[None]})
    31         Provider.__init__(self,name,{"mail":[None]})
    26         self.options.update({
    32         self.options.update({
    27             "send_from":Option(vEmail,long="Emailaddress from which mail will be sended.",must=True),
    33             "send_from":Option(vEmail,long="Emailaddress from which mail will be sended.",must=True),
    28             "host":Option(lambda x,y:x, long="Hostname of MTA", must=True),
    34             "host":Option(lambda x,y:x, long="Hostname of MTA", must=True),
    33             "TLS":Option(vBool,long="use TLS for connection to MTA", default=False),
    39             "TLS":Option(vBool,long="use TLS for connection to MTA", default=False),
    34             })
    40             })
    35         self.order.extend(["host","port","user","password","SSL","TLS","send_from"])
    41         self.order.extend(["host","port","user","password","SSL","TLS","send_from"])
    36 
    42 
    37     def send(self, recipient, mail):   
    43     def send(self, recipient, mail):   
       
    44         """sends a mail to recipient
       
    45         
       
    46         :param string recipient: A valid email address.
       
    47         :param `iro.model.message.Mail` mail: Mail to send.
       
    48         :return: 
       
    49             - All went ok -- :class:`iro.model.status.Status` object
       
    50             - otherwise -- an exception
       
    51         """
    38         if not self.testmode:
    52         if not self.testmode:
    39             if self.SSL:
    53             if self.SSL:
    40                 smtp = smtplib.SMTP_SSL(self.host,self.port)
    54                 smtp = smtplib.SMTP_SSL(self.host,self.port)
    41             else:
    55             else:
    42                 smtp = smtplib.SMTP(self.host,self.port)
    56                 smtp = smtplib.SMTP(self.host,self.port)
    52             if mail.getFrom():
    66             if mail.getFrom():
    53                 frm = mail.getFrom()
    67                 frm = mail.getFrom()
    54             
    68             
    55             tmpmail=copy.deepcopy(mail)
    69             tmpmail=copy.deepcopy(mail)
    56             tmpmail.content['From'] = frm 
    70             tmpmail.content['From'] = frm 
    57             tmpmail.content['To']=recipient
    71             tmpmail.content['To'] = recipient
    58             if not self.testmode:
    72             if not self.testmode:
    59                 smtp.sendmail(frm,  recipient, tmpmail.as_string())
    73                 smtp.sendmail(frm,  recipient, tmpmail.as_string())
    60             return Status(self, None)
    74             return Status(self, None)
    61         finally:
    75         finally:
    62             smtp.quit()
    76             smtp.quit()
    63 
    77 
    64     def getSendFunc(self, typ, route):
    78     def getSendFunc(self, typ, route):
       
    79         """returns :meth:`send` method, if typ and route is valid."""
       
    80 
    65         Provider.getSendFunc(self, typ, route)
    81         Provider.getSendFunc(self, typ, route)
    66         return self.send
    82         return self.send
    67 
    83 
    68 providers["smtp"]=SMTP
    84 providers["smtp"]=SMTP
    69 
    85