[Zope-Checkins] SVN: Zope/trunk/ - Launchpad #246748: added 'immediate' option to sendXXX() methods

Andreas Jung andreas at andreas-jung.com
Sun Jul 13 06:49:21 EDT 2008


Log message for revision 88310:
        - Launchpad #246748: added 'immediate' option to sendXXX() methods
          for sending a mail immediately by-passing the zope.sendmail delivery
          mechanism
  

Changed:
  U   Zope/trunk/doc/CHANGES.txt
  U   Zope/trunk/lib/python/Products/MailHost/MailHost.py
  U   Zope/trunk/lib/python/Products/MailHost/tests/testMailHost.py

-=-
Modified: Zope/trunk/doc/CHANGES.txt
===================================================================
--- Zope/trunk/doc/CHANGES.txt	2008-07-13 09:37:05 UTC (rev 88309)
+++ Zope/trunk/doc/CHANGES.txt	2008-07-13 10:49:19 UTC (rev 88310)
@@ -204,6 +204,10 @@
 
     Bugs Fixed
 
+      - Launchpad #246748: added 'immediate' option to sendXXX() methods
+        for sending a mail immediately by-passing the zope.sendmail delivery
+        mechanism
+
       - Launchpad #246290: fixed backward compatibility issue
 
       - Launchpad #245649:  the Products package is now a proper

Modified: Zope/trunk/lib/python/Products/MailHost/MailHost.py
===================================================================
--- Zope/trunk/lib/python/Products/MailHost/MailHost.py	2008-07-13 09:37:05 UTC (rev 88309)
+++ Zope/trunk/lib/python/Products/MailHost/MailHost.py	2008-07-13 10:49:19 UTC (rev 88310)
@@ -155,15 +155,16 @@
     security.declareProtected(use_mailhost_services, 'sendTemplate')
     def sendTemplate(trueself, self, messageTemplate,
                      statusTemplate=None, mto=None, mfrom=None,
-                     encode=None, REQUEST=None):
+                     encode=None, REQUEST=None, immediate=False):
         'render a mail template, then send it...'
         mtemplate = getattr(self, messageTemplate)
         messageText = mtemplate(self, trueself.REQUEST)
         messageText, mto, mfrom = _mungeHeaders( messageText, mto, mfrom)
         messageText=_encode(messageText, encode)
-        trueself._send(mfrom, mto, messageText)
+        trueself._send(mfrom, mto, messageText, immediate)
 
-        if not statusTemplate: return "SEND OK"
+        if not statusTemplate: 
+            return "SEND OK"
 
         try:
             stemplate=getattr(self, statusTemplate)
@@ -173,11 +174,11 @@
 
     security.declareProtected(use_mailhost_services, 'send')
     def send(self, messageText, mto=None, mfrom=None, subject=None,
-             encode=None):
+             encode=None, immediate=False):
 
         messageText, mto, mfrom = _mungeHeaders( messageText, mto, mfrom, subject)
         messageText = _encode(messageText, encode)
-        self._send(mfrom, mto, messageText)
+        self._send(mfrom, mto, messageText, immediate)
 
     # This is here for backwards compatibility only. Possibly it could
     # be used to send messages at a scheduled future time, or via a mail queue?
@@ -185,11 +186,11 @@
     scheduledSend = send
 
     security.declareProtected(use_mailhost_services, 'simple_send')
-    def simple_send(self, mto, mfrom, subject, body):
+    def simple_send(self, mto, mfrom, subject, body, immediate=False):
         body="From: %s\nTo: %s\nSubject: %s\n\n%s" % (
             mfrom, mto, subject, body)
 
-        self._send( mfrom, mto, body )
+        self._send(mfrom, mto, body, immediate)
 
 
     def _makeMailer(self):
@@ -218,7 +219,7 @@
     @synchronized(lock)
     def _startQueueProcessorThread(self):
         """ Start thread for processing the mail queue """
-        
+
         path = self.absolute_url(1)
         if not queue_threads.has_key(path):
             thread = QueueProcessorThread()
@@ -228,7 +229,6 @@
             queue_threads[path] = thread     
             LOG.info('Thread for %s started' % path)
 
-     
     security.declareProtected(view, 'queueLength')
     def queueLength(self):
         """ return length of mail queue """
@@ -268,17 +268,20 @@
 
 
     security.declarePrivate('_send')
-    def _send(self, mfrom, mto, messageText):
+    def _send(self, mfrom, mto, messageText, immediate=False):
         """ Send the message """
 
-        if self.smtp_queue:
-            # Start queue processor thread, if necessary
-            self._startQueueProcessorThread()
-            delivery = QueuedMailDelivery(self.smtp_queue_directory)
+        if immediate:
+            self._makeMailer().send(mfrom, mto, messageText)
         else:
-            delivery = DirectMailDelivery(self._makeMailer())
+            if self.smtp_queue:
+                # Start queue processor thread, if necessary
+                self._startQueueProcessorThread()
+                delivery = QueuedMailDelivery(self.smtp_queue_directory)
+            else:
+                delivery = DirectMailDelivery(self._makeMailer())
 
-        delivery.send(mfrom, mto, messageText)                
+            delivery.send(mfrom, mto, messageText)
 
 InitializeClass(MailBase)
 

Modified: Zope/trunk/lib/python/Products/MailHost/tests/testMailHost.py
===================================================================
--- Zope/trunk/lib/python/Products/MailHost/tests/testMailHost.py	2008-07-13 09:37:05 UTC (rev 88309)
+++ Zope/trunk/lib/python/Products/MailHost/tests/testMailHost.py	2008-07-13 10:49:19 UTC (rev 88310)
@@ -26,8 +26,9 @@
     def __init__(self, id):
         self.id = id
         self.sent = ''
-    def _send(self, mfrom, mto, messageText):
+    def _send(self, mfrom, mto, messageText, immediate=False):
         self.sent = messageText
+        self.immediate = immediate
 
 
 class TestMailHost(unittest.TestCase):
@@ -190,8 +191,24 @@
                              mfrom='sender at domain.com', subject='This is the subject',
                              body='This is the message body.')
         self.assertEqual(mailhost.sent, outmsg)
+        self.assertEqual(mailhost.immediate, False)
 
+    def testSendImmediate(self):
+        outmsg = """\
+From: sender at domain.com
+To: "Name, Nick" <recipient at domain.com>, "Foo Bar" <foo at domain.com>
+Subject: This is the subject
 
+This is the message body."""
+
+        mailhost = self._makeOne('MailHost')
+        mailhost.simple_send(mto='"Name, Nick" <recipient at domain.com>, "Foo Bar" <foo at domain.com>',
+                             mfrom='sender at domain.com', subject='This is the subject',
+                             body='This is the message body.', immediate=True)
+        self.assertEqual(mailhost.sent, outmsg)
+        self.assertEqual(mailhost.immediate, True)
+
+
 def test_suite():
     suite = unittest.TestSuite()
     suite.addTest( unittest.makeSuite( TestMailHost ) )



More information about the Zope-Checkins mailing list