Commit 2f19b3b1 authored by Andreas Jung's avatar Andreas Jung

- Launchpad #246748: added 'immediate' option to sendXXX() methods

        for sending a mail immediately by-passing the zope.sendmail delivery
        mechanism
parent 69cbcce9
...@@ -7,6 +7,10 @@ Zope Changes ...@@ -7,6 +7,10 @@ Zope Changes
After Zope 2.11.0 After Zope 2.11.0
Bugs Fixed 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 #246290: fixed backward compatibility issue
......
...@@ -155,15 +155,16 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -155,15 +155,16 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
security.declareProtected(use_mailhost_services, 'sendTemplate') security.declareProtected(use_mailhost_services, 'sendTemplate')
def sendTemplate(trueself, self, messageTemplate, def sendTemplate(trueself, self, messageTemplate,
statusTemplate=None, mto=None, mfrom=None, statusTemplate=None, mto=None, mfrom=None,
encode=None, REQUEST=None): encode=None, REQUEST=None, immediate=False):
'render a mail template, then send it...' 'render a mail template, then send it...'
mtemplate = getattr(self, messageTemplate) mtemplate = getattr(self, messageTemplate)
messageText = mtemplate(self, trueself.REQUEST) messageText = mtemplate(self, trueself.REQUEST)
messageText, mto, mfrom = _mungeHeaders( messageText, mto, mfrom) messageText, mto, mfrom = _mungeHeaders( messageText, mto, mfrom)
messageText=_encode(messageText, encode) 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: try:
stemplate=getattr(self, statusTemplate) stemplate=getattr(self, statusTemplate)
...@@ -173,11 +174,11 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -173,11 +174,11 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
security.declareProtected(use_mailhost_services, 'send') security.declareProtected(use_mailhost_services, 'send')
def send(self, messageText, mto=None, mfrom=None, subject=None, 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, mto, mfrom = _mungeHeaders( messageText, mto, mfrom, subject)
messageText = _encode(messageText, encode) 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 # 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? # be used to send messages at a scheduled future time, or via a mail queue?
...@@ -185,11 +186,11 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -185,11 +186,11 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
scheduledSend = send scheduledSend = send
security.declareProtected(use_mailhost_services, 'simple_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" % ( body="From: %s\nTo: %s\nSubject: %s\n\n%s" % (
mfrom, mto, subject, body) mfrom, mto, subject, body)
self._send( mfrom, mto, body ) self._send(mfrom, mto, body, immediate)
def _makeMailer(self): def _makeMailer(self):
...@@ -218,7 +219,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -218,7 +219,7 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
@synchronized(lock) @synchronized(lock)
def _startQueueProcessorThread(self): def _startQueueProcessorThread(self):
""" Start thread for processing the mail queue """ """ Start thread for processing the mail queue """
path = self.absolute_url(1) path = self.absolute_url(1)
if not queue_threads.has_key(path): if not queue_threads.has_key(path):
thread = QueueProcessorThread() thread = QueueProcessorThread()
...@@ -228,7 +229,6 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -228,7 +229,6 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
queue_threads[path] = thread queue_threads[path] = thread
LOG.info('Thread for %s started' % path) LOG.info('Thread for %s started' % path)
security.declareProtected(view, 'queueLength') security.declareProtected(view, 'queueLength')
def queueLength(self): def queueLength(self):
""" return length of mail queue """ """ return length of mail queue """
...@@ -268,17 +268,20 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager): ...@@ -268,17 +268,20 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
security.declarePrivate('_send') security.declarePrivate('_send')
def _send(self, mfrom, mto, messageText): def _send(self, mfrom, mto, messageText, immediate=False):
""" Send the message """ """ Send the message """
if self.smtp_queue: if immediate:
# Start queue processor thread, if necessary self._makeMailer().send(mfrom, mto, messageText)
self._startQueueProcessorThread()
delivery = QueuedMailDelivery(self.smtp_queue_directory)
else: else:
delivery = DirectMailDelivery(self._makeMailer()) if self.smtp_queue:
# Start queue processor thread, if necessary
delivery.send(mfrom, mto, messageText) self._startQueueProcessorThread()
delivery = QueuedMailDelivery(self.smtp_queue_directory)
else:
delivery = DirectMailDelivery(self._makeMailer())
delivery.send(mfrom, mto, messageText)
InitializeClass(MailBase) InitializeClass(MailBase)
......
...@@ -26,8 +26,9 @@ class DummyMailHost(MailHost): ...@@ -26,8 +26,9 @@ class DummyMailHost(MailHost):
def __init__(self, id): def __init__(self, id):
self.id = id self.id = id
self.sent = '' self.sent = ''
def _send(self, mfrom, mto, messageText): def _send(self, mfrom, mto, messageText, immediate=False):
self.sent = messageText self.sent = messageText
self.immediate = immediate
class TestMailHost(unittest.TestCase): class TestMailHost(unittest.TestCase):
...@@ -190,6 +191,22 @@ This is the message body.""" ...@@ -190,6 +191,22 @@ This is the message body."""
mfrom='sender@domain.com', subject='This is the subject', mfrom='sender@domain.com', subject='This is the subject',
body='This is the message body.') body='This is the message body.')
self.assertEqual(mailhost.sent, outmsg) self.assertEqual(mailhost.sent, outmsg)
self.assertEqual(mailhost.immediate, False)
def testSendImmediate(self):
outmsg = """\
From: sender@domain.com
To: "Name, Nick" <recipient@domain.com>, "Foo Bar" <foo@domain.com>
Subject: This is the subject
This is the message body."""
mailhost = self._makeOne('MailHost')
mailhost.simple_send(mto='"Name, Nick" <recipient@domain.com>, "Foo Bar" <foo@domain.com>',
mfrom='sender@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(): def test_suite():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment