def createQueuedDelivery(): delivery = QueuedMailDelivery(queuePath) if permission is not None: delivery = _assertPermission(permission, IMailDelivery, delivery) handler('registerUtility', delivery, IMailDelivery, name) mailerObject = _get_mailer(mailer) if processorThread: thread = QueueProcessorThread() thread.setMailer(mailerObject) thread.setQueuePath(queuePath) thread.start()
def createQueuedDelivery(): delivery = QueuedMailDelivery(queuePath) if permission is not None: delivery = _assertPermission(permission, IMailDelivery, delivery) handler('registerUtility', delivery, IMailDelivery, name) mailerObject = queryUtility(IMailer, mailer) if mailerObject is None: raise ConfigurationError("Mailer %r is not defined" % mailer) if processorThread: thread = QueueProcessorThread() thread.setMailer(mailerObject) thread.setQueuePath(queuePath) thread.start()
def createQueuedDelivery(): delivery = QueuedMailDelivery(queuePath) if permission is not None: delivery = _assertPermission(permission, IMailDelivery, delivery) handler('registerUtility', delivery, IMailDelivery, name) mailerObject = queryUtility(IMailer, mailer) if mailerObject is None: raise ConfigurationError("Mailer %r is not defined" %mailer) if processorThread: thread = QueueProcessorThread() thread.setMailer(mailerObject) thread.setQueuePath(queuePath) thread.start()
class TestQueueProcessorThread(TestCase): def setUp(self): from zope.sendmail.queue import QueueProcessorThread self.md = MaildirStub('/foo/bar/baz') self.thread = QueueProcessorThread() self.thread.setMaildir(self.md) self.mailer = MailerStub() self.thread.setMailer(self.mailer) self.thread.log = LoggerStub() self.dir = mkdtemp() def tearDown(self): shutil.rmtree(self.dir) def test_parseMessage(self): hdr = ('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n') msg = ('Header: value\n' '\n' 'Body\n') f, t, m = self.thread._parseMessage(hdr + msg) self.assertEquals(f, '*****@*****.**') self.assertEquals(t, ('*****@*****.**', '*****@*****.**')) self.assertEquals(m, msg) def test_deliveration(self): self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) self.assertEquals(self.mailer.sent_messages, [('*****@*****.**', ('*****@*****.**', '*****@*****.**'), 'Header: value\n\nBody\n')]) self.failIf(os.path.exists(self.filename), 'File exists') self.assertEquals(self.thread.log.infos, [('Mail from %s to %s sent.', ('*****@*****.**', '[email protected], [email protected]'), {})]) def test_error_logging(self): self.thread.setMailer(BrokenMailerStub()) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) self.assertEquals(self.thread.log.errors, [('Error while sending mail from %s to %s.', ('*****@*****.**', '[email protected], [email protected]'), {'exc_info': 1})]) def test_smtp_response_error_transient(self): # Test a transient error self.thread.setMailer(SMTPResponseExceptionMailerStub(451)) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) # File must remail were it was, so it will be retried self.failUnless(os.path.exists(self.filename)) self.assertEquals(self.thread.log.errors, [('Error while sending mail from %s to %s.', ('*****@*****.**', '[email protected], [email protected]'), {'exc_info': 1})]) def test_smtp_response_error_permanent(self): # Test a permanent error self.thread.setMailer(SMTPResponseExceptionMailerStub(550)) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) # File must be moved aside self.failIf(os.path.exists(self.filename)) self.failUnless(os.path.exists(os.path.join(self.dir, '.rejected-message'))) self.assertEquals(self.thread.log.errors, [('Discarding email from %s to %s due to a ' 'permanent error: %s', ('*****@*****.**', '[email protected], [email protected]', "(550, 'Serious Error')"), {})]) def test_smtp_recipients_refused(self): # Test a permanent error self.thread.setMailer(SMTPRecipientsRefusedMailerStub( ['*****@*****.**'])) self.filename = os.path.join(self.dir, 'message') temp = open(self.filename, "w+b") temp.write('X-Zope-From: [email protected]\n' 'X-Zope-To: [email protected], [email protected]\n' 'Header: value\n\nBody\n') temp.close() self.md.files.append(self.filename) self.thread.run(forever=False) # File must be moved aside self.failIf(os.path.exists(self.filename)) self.failUnless(os.path.exists(os.path.join(self.dir, '.rejected-message'))) self.assertEquals(self.thread.log.errors, [('Email recipients refused: %s', ('*****@*****.**',), {})])
def create_emailUtilities(instance_id=None): '''Create the utilities to send the email messages :param str instance_id: The indentifier for the GroupServer instance :returns: ``None`` The :func:`create_emailUtilities` function loads the ``smtp`` section of the configuration of the instance specified by ``instance_id``. If no instance is specified then :func:`gs.config.getInstanceId` is used to determine the current instance. It then loads the following configuration options: * ``hostname`` * ``port`` * ``username`` * ``password`` * ``no_tls`` * ``force_tls`` * ``queuepath`` * ``processorthread`` * ``xverp`` If the XVERP option is ``True`` then :class:`gs.email.mailer.XVERPSMTPMailer` is registered as the utility used to connect to the SMTP host; otherwise :class:`zope.sendmail.mailer.SMTPMailer` is used. In either case the mailer is configured with the options in the config file.''' if not instance_id: instance_id = getInstanceId() config = Config(instance_id) config.set_schema('smtp', {'hostname': str, 'port': int, 'username': str, 'password': str, 'no_tls': bool_, 'force_tls': bool_, 'queuepath': str, 'processorthread': bool_, 'xverp': bool_}) smtpconfig = config.get('smtp', strict=False) name = '' for key in ('hostname', 'port', 'username', 'password', 'no_tls', 'force_tls'): name += '+%s+' % smtpconfig.get(key, None) gsm = getGlobalSiteManager() if not queryUtility(IMailer, 'gs.mailer.%s' % name): if smtpconfig.get('xverp', False): Mailer = XVERPSMTPMailer else: Mailer = SMTPMailer gsm.registerUtility( Mailer( hostname=smtpconfig.get('hostname', None), port=smtpconfig.get('port', None), username=smtpconfig.get('username', None), password=smtpconfig.get('password', None), no_tls=smtpconfig.get('no_tls', None), force_tls=smtpconfig.get('force_tls', None)), IMailer, name='gs.mailer.%s' % name) queuePath = smtpconfig.get('queuepath', '/tmp/mailqueue') if not queryUtility(IMailDelivery, name='gs.maildelivery'): delivery = QueuedMailDelivery(queuePath) gsm.registerUtility(delivery, IMailDelivery, name='gs.maildelivery') if smtpconfig.get('processorthread', True): mailerObject = getUtility(IMailer, 'gs.mailer.%s' % name) thread = QueueProcessorThread() thread.setMailer(mailerObject) thread.setQueuePath(queuePath) thread.start()
def start_processor_thread(): from zope.sendmail.queue import QueueProcessorThread thread = QueueProcessorThread() thread.setMailer(mailer_object) thread.setQueuePath(queue_path) thread.start()