[Checkins] SVN: zopyx.smartprintng.server/trunk/ asyncronous mail delivery

Andreas Jung andreas at andreas-jung.com
Sun Jul 19 09:01:01 EDT 2009


Log message for revision 102012:
  asyncronous mail delivery
  

Changed:
  U   zopyx.smartprintng.server/trunk/docs/HISTORY.txt
  U   zopyx.smartprintng.server/trunk/setup.py
  U   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/mail_util.py
  U   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/run.py

-=-
Modified: zopyx.smartprintng.server/trunk/docs/HISTORY.txt
===================================================================
--- zopyx.smartprintng.server/trunk/docs/HISTORY.txt	2009-07-19 12:11:30 UTC (rev 102011)
+++ zopyx.smartprintng.server/trunk/docs/HISTORY.txt	2009-07-19 13:01:01 UTC (rev 102012)
@@ -1,6 +1,13 @@
 Changelog
 =========
 
+0.4.2 (2009/07/19)
+------------------
+
+* switching back to zope.sendmail
+* implemented asynchronous mail delivery on top of zope.sendmail
+
+
 0.4.1 (2009/07/19)
 ------------------
 

Modified: zopyx.smartprintng.server/trunk/setup.py
===================================================================
--- zopyx.smartprintng.server/trunk/setup.py	2009-07-19 12:11:30 UTC (rev 102011)
+++ zopyx.smartprintng.server/trunk/setup.py	2009-07-19 13:01:01 UTC (rev 102012)
@@ -1,7 +1,7 @@
 from setuptools import setup, find_packages
 import os
 
-version = '0.4.1'
+version = '0.4.2'
 
 setup(name='zopyx.smartprintng.server',
       version=version,
@@ -27,7 +27,8 @@
           'repoze.bfg',
           'uuid',
           'zopyx.convert2',
-          'repoze.sendmail',
+          'zope.sendmail',
+          'transaction',
           'nose',
           # -*- Extra requirements: -*-
       ],

Modified: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/mail_util.py
===================================================================
--- zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/mail_util.py	2009-07-19 12:11:30 UTC (rev 102011)
+++ zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/mail_util.py	2009-07-19 13:01:01 UTC (rev 102012)
@@ -4,16 +4,20 @@
 ##########################################################################
 
 import os
+import tempfile
 import email.MIMEText
 import email.Header
 import email.MIMEBase
 import email.MIMEMultipart
 from email import Encoders
 from ConfigParser import ConfigParser
-from repoze.sendmail.mailer import SMTPMailer
+from zope.sendmail.mailer import SMTPMailer
+from zope.sendmail.delivery import QueuedMailDelivery, QueueProcessorThread
+import transaction
+from logger import LOG
 
 
-def makeMailer():
+def getMailConfiguration():
 
     mail_config = os.environ.get('EMAIL_CONFIG')
     if not mail_config:
@@ -29,27 +33,48 @@
     password = None
     no_tls = False
     force_tls = False
+    maildir = tempfile.mkdtemp(prefix='zopyx.smartprintng.server')
 
     if CP.has_option('mail', 'hostname'): hostname = CP.get('mail', 'hostname')
     if CP.has_option('mail', 'username'): username = CP.get('mail', 'username')
     if CP.has_option('mail', 'password'): password = CP.get('mail', 'password')
+    if CP.has_option('mail', 'maildir'): maildir = CP.get('mail', 'maildir')
     if CP.has_option('mail', 'no_tls'): no_tls = CP.getboolean('mail', 'no_tls')
     if CP.has_option('mail', 'force_tls'): force_tls = CP.getboolean('mail', 'force_tls')
 
-    return SMTPMailer(hostname=hostname,
-                      username=username,
-                      password=password,
-                      no_tls=no_tls,
-                      force_tls=force_tls)
+    # setup maildir structure
+    if not os.path.exists(maildir):
+        os.makedirs(maildir)
+    for subdir in ('cur', 'tmp', 'new'):
+        destdir = os.path.join(maildir, subdir)
+        if not os.path.exists(destdir):
+            os.makedirs(destdir)
 
+    return dict(hostname=hostname,
+                username=username,
+                password=password,
+                maildir=maildir,
+                force_tls=force_tls,
+                no_tls=no_tls)
 
+
+def setupMailer():
+
+    config = getMailConfiguration()
+
+    thread = QueueProcessorThread()
+    thread.setMailer(makeMailer())
+    thread.setQueuePath(config['maildir'])
+    thread.start()
+
+def makeMailer():
+    config = getMailConfiguration().copy()
+    del config['maildir']
+    return SMTPMailer(**config)
+
+
 def send_email(sender, recipient, subject, body, attachments=[]):
 
-    try:
-        mailer = makeMailer()
-    except Exception,e:
-        raise RuntimeError('Email configuration error (%s)' % e)
-
     msg = email.MIMEMultipart.MIMEMultipart()
     msg["From"] = sender
     msg["To"] = recipient
@@ -64,6 +89,8 @@
                         'attachment; filename="%s"' % os.path.basename(att))
         msg.attach(part)
 
-    mailer.send(sender, [recipient], msg.as_string())
+    config = getMailConfiguration()
+    delivery = QueuedMailDelivery(config['maildir'])
+    delivery.send(sender, [recipient], msg.as_string())
+    transaction.commit()
 
-

Modified: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/run.py
===================================================================
--- zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/run.py	2009-07-19 12:11:30 UTC (rev 102011)
+++ zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/run.py	2009-07-19 13:01:01 UTC (rev 102012)
@@ -5,6 +5,7 @@
 
 import os
 from repoze.bfg.router import make_app
+import mail_util
 
 def app(global_config, **kw):
     """ This function returns a repoze.bfg.router.Router object.  It
@@ -19,6 +20,7 @@
         mail_config = os.path.abspath(global_config['mail_config'])
         LOG.info('Using email configuration at %s' % mail_config)
         os.environ['EMAIL_CONFIG'] = mail_config
+        mail_util.setupMailer()
     LOG.info('SmartPrintNG server started')
     return make_app(get_root, zopyx.smartprintng.server, options=kw)
 



More information about the Checkins mailing list