iro/offer/smtp.py
branchdevel
changeset 183 07ee5543751b
parent 180 55ab949cf0f8
child 186 b381eaa774ab
equal deleted inserted replaced
182:e6af45edff5a 183:07ee5543751b
    11 #You should have received a copy of the GNU General Public License
    11 #You should have received a copy of the GNU General Public License
    12 #along with this program; if not, see <http://www.gnu.org/licenses/>.
    12 #along with this program; if not, see <http://www.gnu.org/licenses/>.
    13 
    13 
    14 import smtplib
    14 import smtplib
    15 import copy
    15 import copy
       
    16 from functools import partial
    16 
    17 
       
    18 from ..validate import vInteger, vEmail,vBool
    17 from ..model.status import Status
    19 from ..model.status import Status
       
    20 from ..config import Option
    18 from .provider import Provider, providers
    21 from .provider import Provider, providers
    19 
    22 
    20 class SMTP(Provider):
    23 class SMTP(Provider):
    21     def __init__(self, name, config):
    24     def __init__(self, name, config):
    22         Provider.__init__(self,name,config,{"mail":[None]})
    25         Provider.__init__(self,name,config,{"mail":[None]})
    23         
    26         self.options.update({
    24 
    27             "send_from":Option(vEmail,long="Emailaddress from which mail will be sended.",must=True),
    25     def loadConfig(self):
    28             "host":Option(lambda x,y:x, long="Hostname of MTA", must=True),
    26         """Read options from config"""
    29             "port":Option(partial(vInteger,minv=0),long="Port of the MTA", default=25),
    27         needed=["send_from","host", "port", "user", "password"]
    30             "user":Option(lambda x,y:x, long="username to login into MTA.",default=""),
    28 
    31             "password":Option(lambda x,y:x, long="password to login into MTA.",default=""),
    29         for n in needed:
    32             "TLS":Option(vBool,long="use TLS for connection to MTA", default=False),
    30             setattr(self,n,None)
    33             "SSL":Option(vBool,long="use SSL for connection to MTA", default=False),
    31 
    34             })
    32         Provider.loadConfig(self)
    35         self.loadConfig()
    33         self.bTLS = False
       
    34         self.bSSL = False
       
    35 
       
    36         for (n, v) in self.config:
       
    37             if n in needed:
       
    38                 setattr(self,n,v)
       
    39             elif n == "TLS":
       
    40                 self.bTLS = bool(v)
       
    41             elif n == "SSL":
       
    42                 self.bSSL = bool(v)
       
    43             else:
       
    44                 raise UnknownOption(self.name, n)
       
    45 
       
    46         for n in needed:
       
    47            if getattr(self,n) is None:
       
    48                raise NeededOption(self.name, n) 
       
    49 
    36 
    50     def send(self,mail,recipient):   
    37     def send(self,mail,recipient):   
    51         if not self.testmode:
    38         if not self.testmode:
    52             if self.bSSL:
    39             if self.SSL:
    53                 smtp = smtplib.SMTP_SSL(self.host,self.port)
    40                 smtp = smtplib.SMTP_SSL(self.host,self.port)
    54             else:
    41             else:
    55                 smtp = smtplib.SMTP(self.host,self.port)
    42                 smtp = smtplib.SMTP(self.host,self.port)
    56             
    43             
    57             if self.bTLS:
    44             if self.TLS:
    58                 smtp.starttls()
    45                 smtp.starttls()
    59            
    46            
    60             if not self.user == "":
    47             if not self.user == "":
    61                 smtp.login(self.user,self.password)
    48                 smtp.login(self.user,self.password)
    62         try:
    49         try: