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
......@@ -8,6 +8,10 @@ Zope Changes
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
- zope.testing : updated to 3.5.3
......
......@@ -155,15 +155,16 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
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 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
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 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
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):
......@@ -228,7 +229,6 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
queue_threads[path] = thread
LOG.info('Thread for %s started' % path)
security.declareProtected(view, 'queueLength')
def queueLength(self):
""" return length of mail queue """
......@@ -268,9 +268,12 @@ class MailBase(Acquisition.Implicit, OFS.SimpleItem.Item, RoleManager):
security.declarePrivate('_send')
def _send(self, mfrom, mto, messageText):
def _send(self, mfrom, mto, messageText, immediate=False):
""" Send the message """
if immediate:
self._makeMailer().send(mfrom, mto, messageText)
else:
if self.smtp_queue:
# Start queue processor thread, if necessary
self._startQueueProcessorThread()
......
......@@ -26,8 +26,9 @@ class DummyMailHost(MailHost):
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,6 +191,22 @@ This is the message body."""
mfrom='sender@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@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():
......
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