cwconfig.py
branchstable
changeset 2221 d9b85a7b0bdd
parent 2220 64aace08ae2f
child 2267 e1d2df3f1091
--- a/cwconfig.py	Wed Jul 01 16:41:30 2009 +0200
+++ b/cwconfig.py	Wed Jul 01 16:44:37 2009 +0200
@@ -17,6 +17,8 @@
 import sys
 import os
 import logging
+from smtplib import SMTP
+from threading import Lock
 from os.path import exists, join, expanduser, abspath, normpath, basename, isdir
 
 from logilab.common.decorators import cached
@@ -29,6 +31,8 @@
 
 CONFIGURATIONS = []
 
+SMTP_LOCK = Lock()
+
 
 class metaconfiguration(type):
     """metaclass to automaticaly register configuration"""
@@ -827,6 +831,28 @@
         sourcedirs.append(self.i18n_lib_dir())
         return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs)
 
+    def sendmails(self, msgs):
+        """msgs: list of 2-uple (message object, recipients)"""
+        server, port = self['smtp-host'], self['smtp-port']
+        SMTP_LOCK.acquire()
+        try:
+            try:
+                smtp = SMTP(server, port)
+            except Exception, ex:
+                self.exception("can't connect to smtp server %s:%s (%s)",
+                               server, port, ex)
+                return
+            heloaddr = '%s <%s>' % (self['sender-name'], self['sender-addr'])
+            for msg, recipients in msgs:
+                try:
+                    smtp.sendmail(heloaddr, recipients, msg.as_string())
+                except Exception, ex:
+                    self.exception("error sending mail to %s (%s)",
+                                   recipients, ex)
+            smtp.close()
+        finally:
+            SMTP_LOCK.release()
+
 set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration'))
 
 # alias to get a configuration instance from an application id