server/hookhelper.py
changeset 2234 1fbcf202882d
parent 2221 d9b85a7b0bdd
child 2609 a0f6fa90cc32
equal deleted inserted replaced
2209:2b91abd9f5a4 2234:1fbcf202882d
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     4 :copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     7 """
     7 """
     8 __docformat__ = "restructuredtext en"
     8 __docformat__ = "restructuredtext en"
     9 
       
    10 from smtplib import SMTP
       
    11 from threading import Lock
       
    12 
     9 
    13 from cubicweb import RepositoryError
    10 from cubicweb import RepositoryError
    14 from cubicweb.server.pool import SingleLastOperation
    11 from cubicweb.server.pool import SingleLastOperation
    15 
    12 
    16 
    13 
    45             yield session
    42             yield session
    46 
    43 
    47 
    44 
    48 # mail related ################################################################
    45 # mail related ################################################################
    49 
    46 
    50 SMTP_LOCK = Lock()
       
    51 
       
    52 class SendMailOp(SingleLastOperation):
    47 class SendMailOp(SingleLastOperation):
    53     def __init__(self, session, msg=None, recipients=None, **kwargs):
    48     def __init__(self, session, msg=None, recipients=None, **kwargs):
    54         # may not specify msg yet, as
    49         # may not specify msg yet, as
    55         # `cubicweb.sobjects.supervision.SupervisionMailOp`
    50         # `cubicweb.sobjects.supervision.SupervisionMailOp`
    56         if msg is not None:
    51         if msg is not None:
    68 
    63 
    69     def commit_event(self):
    64     def commit_event(self):
    70         self.repo.threaded_task(self.sendmails)
    65         self.repo.threaded_task(self.sendmails)
    71 
    66 
    72     def sendmails(self):
    67     def sendmails(self):
    73         server, port = self.config['smtp-host'], self.config['smtp-port']
    68         self.config.sendmails(self.to_send)
    74         SMTP_LOCK.acquire()
       
    75         try:
       
    76             try:
       
    77                 smtp = SMTP(server, port)
       
    78             except Exception, ex:
       
    79                 self.exception("can't connect to smtp server %s:%s (%s)",
       
    80                                server, port, ex)
       
    81                 return
       
    82             heloaddr = '%s <%s>' % (self.config['sender-name'],
       
    83                                     self.config['sender-addr'])
       
    84             for msg, recipients in self.to_send:
       
    85                 try:
       
    86                     smtp.sendmail(heloaddr, recipients, msg.as_string())
       
    87                 except Exception, ex:
       
    88                     self.exception("error sending mail to %s (%s)",
       
    89                                    recipients, ex)
       
    90             smtp.close()
       
    91         finally:
       
    92             SMTP_LOCK.release()
       
    93 
    69 
    94 
    70 
    95 # state related ###############################################################
    71 # state related ###############################################################
    96 
    72 
    97 def previous_state(session, eid):
    73 def previous_state(session, eid):