iro/offer/smtp.py
branchdevel
changeset 169 aabc04843d25
parent 168 42b55855e3a6
child 171 5619596a0053
equal deleted inserted replaced
168:42b55855e3a6 169:aabc04843d25
    19 from ..offer import providers
    19 from ..offer import providers
    20 from .provider import Provider
    20 from .provider import Provider
    21 
    21 
    22 class SMTP(Provider):
    22 class SMTP(Provider):
    23     def __init__(self, name, config):
    23     def __init__(self, name, config):
    24         Provider.__init__(self,name,config,["std"],["mail"])
    24         Provider.__init__(self,name,config,{"mail":[None]})
    25         
    25         
    26 
    26 
    27     def loadConfig(self):
    27     def loadConfig(self):
    28         """Read options from config"""
    28         """Read options from config"""
    29         needed=["send_from","host", "port", "user", "password"]
    29         needed=["send_from","host", "port", "user", "password"]
    48         for n in needed:
    48         for n in needed:
    49            if getattr(self,n) is None:
    49            if getattr(self,n) is None:
    50                raise NeededOption(self.name, n) 
    50                raise NeededOption(self.name, n) 
    51 
    51 
    52     def send(self,mail,recipient):   
    52     def send(self,mail,recipient):   
       
    53         if not self.testmode:
       
    54             if self.bSSL:
       
    55                 smtp = smtplib.SMTP_SSL(self.host,self.port)
       
    56             else:
       
    57                 smtp = smtplib.SMTP(self.host,self.port)
       
    58             
       
    59             if self.bTLS:
       
    60                 smtp.starttls()
       
    61            
       
    62             if not self.user == "":
       
    63                 smtp.login(self.user,self.password)
    53         try:
    64         try:
       
    65             frm=self.send_from
       
    66             
       
    67             if mail.getFrom():
       
    68                 frm = mail.getFrom()
       
    69             
       
    70             tmpmail=copy.deepcopy(mail)
       
    71             tmpmail.content['From'] = frm 
       
    72             tmpmail.content['To']=recipient
    54             if not self.testmode:
    73             if not self.testmode:
    55                 if self.bSSL:
    74                 smtp.sendmail(frm,  recipient, tmpmail.as_string())
    56                     smtp = smtplib.SMTP_SSL(self.host,self.port)
    75             return Status(self, "std")
    57                 else:
    76         finally:
    58                     smtp = smtplib.SMTP(self.host,self.port)
    77             smtp.quit()
    59                 
       
    60                 if self.bTLS:
       
    61                     smtp.starttls()
       
    62                
       
    63                 if not self.user == "":
       
    64                     smtp.login(self.user,self.password)
       
    65             try:
       
    66                 frm=self.send_from
       
    67                 
       
    68                 if mail.getFrom():
       
    69                     frm = mail.getFrom()
       
    70                 
       
    71                 tmpmail=copy.deepcopy(mail)
       
    72                 tmpmail.content['From'] = frm 
       
    73                 tmpmail.content['To']=recipient
       
    74                 if not self.testmode:
       
    75                     smtp.sendmail(frm,  recipient, tmpmail.as_string())
       
    76                 return Status(self, "std")
       
    77             finally:
       
    78                 smtp.quit()
       
    79         except Exception as e:
       
    80             return Status(self,"std",e)
       
    81 
    78 
    82     def getSendFunc(self, typ, route):
    79     def getSendFunc(self, typ, route):
    83         if typ != "mail":
    80         try:
       
    81             if route not in self.typs[typ]:
       
    82                 raise NoRoute(route)
       
    83         except KeyError:
    84             raise NoTyp(route)
    84             raise NoTyp(route)
    85         elif  route != "std":
    85         
    86             raise NoRoute(route)
       
    87         return self.send
    86         return self.send
    88 
    87 
    89 providers["smtp"]=SMTP
    88 providers["smtp"]=SMTP
    90 
    89