def setUp(self, port=None): self.smtp = None if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u'localhost', port) self.mailer.smtp = self._makeSMTP self.smtp_hook = lambda smtp: None
def send(self, fromaddr, toaddrs, message): if self.username and self.password: mailer = SMTPMailer(self.hostname, self.port, self.username, self.password) else: mailer = SMTPMailer(self.hostname, self.port) try: mailer.send(fromaddr, toaddrs, message) except Exception, err: logging.getLogger('zojax.mail').exception(str(err))
def setUp(self, port=None): global SMTP class SMTP(object): fail_on_quit = False def __init__(myself, h, p): myself.hostname = h myself.port = p myself.quitted = False myself.closed = False if not isinstance(p, port_types): raise socket.error("Int or String expected") self.smtp = myself def sendmail(self, f, t, m): self.fromaddr = f self.toaddrs = t self.msgtext = m def login(self, username, password): self.username = username self.password = password def quit(self): if self.fail_on_quit: raise SSLError("dang") self.quitted = True self.close() def close(self): self.closed = True def has_extn(self, ext): return True def ehlo(self): self.does_esmtp = True return (200, 'Hello, I am your stupid MTA mock') def starttls(self): pass if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u'localhost', port) self.mailer.smtp = SMTP
def setUp(self, port=None): global SMTP class SMTP(object): fail_on_quit = False def __init__(myself, h, p): myself.hostname = h myself.port = p myself.quitted = False myself.closed = False if type(p) == type(u""): raise socket.error("Int or String expected") self.smtp = myself def sendmail(self, f, t, m): self.fromaddr = f self.toaddrs = t self.msgtext = m def login(self, username, password): self.username = username self.password = password def quit(self): if self.fail_on_quit: raise socket.sslerror("dang") self.quitted = True self.close() def close(self): self.closed = True def has_extn(self, ext): return True def ehlo(self): self.does_esmtp = True return (200, 'Hello, I am your stupid MTA mock') def starttls(self): pass if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u'localhost', port) self.mailer.smtp = SMTP
def _makeMailer(self): """ Create a SMTPMailer """ return SMTPMailer(hostname=self.smtp_host, port=int(self.smtp_port), username=self.smtp_uid or None, password=self.smtp_pwd or None, force_tls=self.force_tls)
def _getMailer(self): """ Create a new SMTPMailer instance """ if not hasattr(self, '_v_mailhost'): self._v_mailer = SMTPMailer(self.smtp_host, self.smtp_port, self.smtp_username or None, self.smtp_password or None) return self._v_mailer
def smtpMailer(_context, name, hostname="localhost", port="25", username=None, password=None): _context.action(discriminator=('utility', IMailer, name), callable=handler, args=('registerUtility', SMTPMailer(hostname, port, username, password), IMailer, name))
def send(self, fromaddr, toaddrs, message): # log message if self.log_emails: logger.log(logging.INFO, toaddrs) logger.log(logging.INFO, message) if self.smtpuser and self.smtppass: mailer = SMTPMailer(self.hostname, self.port, self.smtpuser, self.smtppass) else: mailer = SMTPMailer(self.hostname, self.port) # delivery if self.disabled: logger.info("Mail delivery is disabled.") return try: mailer.send(fromaddr, toaddrs, message) except Exception, err: logger.exception(str(err))
def _getMailer(self): """ Create a new SMTPMailer instance """ if not hasattr(self, '_v_mailhost'): try: # new extended zope.sendmail package self._v_mailer = SMTPMailer( self.smtp_host, self.smtp_port, self.smtp_username or None, self.smtp_password or None, self.no_tls, self.force_tls, ) except TypeError: # standard zope.sendmail package self._v_mailer = SMTPMailer( self.smtp_host, self.smtp_port, self.smtp_username or None, self.smtp_password or None, ) return self._v_mailer
def setUp(self, port=None): class SMTPWithNoEHLO(SMTP): does_esmtp = False def __init__(myself, h, p): myself.hostname = h myself.port = p myself.quitted = False myself.closed = False if type(p) == type(u""): raise socket.error("Int or String expected") self.smtp = myself def helo(self): return (200, 'Hello, I am your stupid MTA mock') def ehlo(self): return (502, 'I don\'t understand EHLO') if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u'localhost', port) self.mailer.smtp = SMTPWithNoEHLO
def __init__(self, argv=sys.argv, verbose=True): self.script_name = argv[0] self.verbose = verbose self._process_args(argv[1:]) self.mailer = SMTPMailer(self.hostname, self.port, self.username, self.password, self.no_tls, self.force_tls)
class TestSMTPMailer(unittest.TestCase): SMTPClass = SMTP # Avoid DeprecationWarning for assertRaisesRegexp on Python 3 while # coping with Python 2 not having the Regex spelling variant assertRaisesRegex = getattr(unittest.TestCase, 'assertRaisesRegex', unittest.TestCase.assertRaisesRegexp) def _makeSMTP(self, h, p): self.smtp = self.SMTPClass(h, p) self.smtp_hook(self.smtp) return self.smtp def setUp(self, port=None): self.smtp = None if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u'localhost', port) self.mailer.smtp = self._makeSMTP self.smtp_hook = lambda smtp: None def test_interface(self): verifyObject(ISMTPMailer, self.mailer) def test_send(self): for run in (1, 2): if run == 2: self.setUp(u'25') fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEqual(self.smtp.fromaddr, fromaddr) self.assertEqual(self.smtp.toaddrs, toaddrs) self.assertEqual(self.smtp.msgtext, msgtext) self.assertTrue(self.smtp.quitted) self.assertTrue(self.smtp.closed) def test_send_multiple_same_mailer(self): # The mailer re-opens itself as needed when sending # multiple mails. smtps = [] def hook(smtp): smtps.append(smtp) self.smtp_hook = hook for run in (1, 2): fromaddr = '*****@*****.**' + str(run) toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEqual(self.smtp.fromaddr, fromaddr) self.assertEqual(self.smtp.toaddrs, toaddrs) self.assertEqual(self.smtp.msgtext, msgtext) self.assertTrue(self.smtp.quitted) self.assertTrue(self.smtp.closed) self.assertEqual(2, len(smtps)) def test_send_multiple_threads(self): import threading results = [] def run(): try: self.test_send_multiple_same_mailer() except BaseException as e: # pragma: no cover results.append(e) raise else: results.append(True) threads = [] for _ in range(2): threads.append(threading.Thread(target=run)) for t in threads: t.start() for t in threads: t.join() self.assertEqual([True for _ in threads], results) def test_send_auth(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = '******' self.mailer.password = '******' self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEqual(self.smtp.username, 'foo') self.assertEqual(self.smtp.password, 'evil') self.assertEqual(self.smtp.hostname, 'spamrelay') self.assertEqual(self.smtp.port, '31337') self.assertEqual(self.smtp.fromaddr, fromaddr) self.assertEqual(self.smtp.toaddrs, toaddrs) self.assertEqual(self.smtp.msgtext, msgtext) self.assertTrue(self.smtp.quitted) self.assertTrue(self.smtp.closed) def test_send_auth_unicode(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = u'f\u00f8\u00f8' # double o slash self.mailer.password = u'\u00e9vil' # e acute self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEqual(self.smtp.username, 'føø') self.assertEqual(self.smtp.password, 'évil') def test_send_auth_nonascii(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = b'f\xc3\xb8\xc3\xb8' # double o slash self.mailer.password = b'\xc3\xa9vil' # e acute self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEqual(self.smtp.username, b'f\xc3\xb8\xc3\xb8') self.assertEqual(self.smtp.password, b'\xc3\xa9vil') def test_send_failQuit(self): SMTP.fail_on_quit = True self.addCleanup(lambda: setattr(SMTP, 'fail_on_quit', False)) fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEqual(self.smtp.fromaddr, fromaddr) self.assertEqual(self.smtp.toaddrs, toaddrs) self.assertEqual(self.smtp.msgtext, msgtext) self.assertTrue(not self.smtp.quitted) self.assertTrue(self.smtp.closed) def test_vote_bad_connection(self): def hook(smtp): smtp.ehlo_code = 100 smtp.helo = lambda: (100, "Nope") self.smtp_hook = hook with self.assertRaisesRegex(RuntimeError, "Error sending HELO to the SMTP server"): self.mailer.vote(None, None, None) def test_abort_no_conn(self): self.assertIsNone(self.mailer.abort()) def test_abort_fails_call_close(self): class Conn(object): closed = False def quit(self): raise SSLError() def close(self): self.closed = True conn = Conn() self.mailer.connection = conn self.mailer.abort() self.assertTrue(conn.closed) def test_send_no_tls_forced(self): class Conn(object): def has_extn(self, name): assert name == 'starttls' return False self.mailer.force_tls = True self.mailer.connection = Conn() with self.assertRaisesRegex(RuntimeError, 'TLS is not available'): self.mailer.send(None, None, None) def test_send_no_esmtp_with_username(self): class Conn(object): does_esmtp = False def has_extn(self, *args): return False self.mailer.connection = Conn() self.mailer.username = '******' with self.assertRaisesRegex( RuntimeError, "Mailhost does not support ESMTP but a username"): self.mailer.send(None, None, None)
class TestSMTPMailer(unittest.TestCase): def setUp(self, port=None): global SMTP class SMTP(object): fail_on_quit = False def __init__(myself, h, p): myself.hostname = h myself.port = p myself.quitted = False myself.closed = False if not isinstance(p, port_types): raise socket.error("Int or String expected") self.smtp = myself def sendmail(self, f, t, m): self.fromaddr = f self.toaddrs = t self.msgtext = m def login(self, username, password): self.username = username self.password = password def quit(self): if self.fail_on_quit: raise SSLError("dang") self.quitted = True self.close() def close(self): self.closed = True def has_extn(self, ext): return True def ehlo(self): self.does_esmtp = True return (200, 'Hello, I am your stupid MTA mock') def starttls(self): pass if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u'localhost', port) self.mailer.smtp = SMTP def test_interface(self): verifyObject(ISMTPMailer, self.mailer) def test_send(self): for run in (1,2): if run == 2: self.setUp(u'25') fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(self.smtp.quitted) self.assert_(self.smtp.closed) def test_send_auth(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = '******' self.mailer.password = '******' self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, b'foo') self.assertEquals(self.smtp.password, b'evil') self.assertEquals(self.smtp.hostname, 'spamrelay') self.assertEquals(self.smtp.port, '31337') self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(self.smtp.quitted) self.assert_(self.smtp.closed) def test_send_auth_unicode(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = u'f\u00f8\u00f8' # double o slash self.mailer.password = u'\u00e9vil' # e acute self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, b'f\xc3\xb8\xc3\xb8') self.assertEquals(self.smtp.password, b'\xc3\xa9vil') def test_send_auth_nonascii(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = b'f\xc3\xb8\xc3\xb8' # double o slash self.mailer.password = b'\xc3\xa9vil' # e acute self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, b'f\xc3\xb8\xc3\xb8') self.assertEquals(self.smtp.password, b'\xc3\xa9vil') def test_send_failQuit(self): self.mailer.smtp.fail_on_quit = True try: fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(not self.smtp.quitted) self.assert_(self.smtp.closed) finally: self.mailer.smtp.fail_on_quit = False
class TestSMTPMailer(unittest.TestCase): def setUp(self, port=None): global SMTP class SMTP(object): fail_on_quit = False def __init__(myself, h, p): myself.hostname = h myself.port = p myself.quitted = False myself.closed = False if type(p) == type(u""): raise socket.error("Int or String expected") self.smtp = myself def sendmail(self, f, t, m): self.fromaddr = f self.toaddrs = t self.msgtext = m def login(self, username, password): self.username = username self.password = password def quit(self): if self.fail_on_quit: raise socket.sslerror("dang") self.quitted = True self.close() def close(self): self.closed = True def has_extn(self, ext): return True def ehlo(self): self.does_esmtp = True return (200, 'Hello, I am your stupid MTA mock') def starttls(self): pass if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u'localhost', port) self.mailer.smtp = SMTP def test_interface(self): verifyObject(ISMTPMailer, self.mailer) def test_send(self): for run in (1, 2): if run == 2: self.setUp(u'25') fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(self.smtp.quitted) self.assert_(self.smtp.closed) def test_send_auth(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = '******' self.mailer.password = '******' self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, 'foo') self.assertEquals(self.smtp.password, 'evil') self.assertEquals(self.smtp.hostname, 'spamrelay') self.assertEquals(self.smtp.port, '31337') self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(self.smtp.quitted) self.assert_(self.smtp.closed) def test_send_auth_unicode(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = u'f\u00f8\u00f8' # double o slash self.mailer.password = u'\u00e9vil' # e acute self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, 'f\xc3\xb8\xc3\xb8') self.assertEquals(self.smtp.password, '\xc3\xa9vil') def test_send_auth_nonascii(self): fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.username = '******' # double o slash self.mailer.password = '******' # e acute self.mailer.hostname = 'spamrelay' self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, 'f\xc3\xb8\xc3\xb8') self.assertEquals(self.smtp.password, '\xc3\xa9vil') def test_send_failQuit(self): self.mailer.smtp.fail_on_quit = True try: fromaddr = '*****@*****.**' toaddrs = ('*****@*****.**', '*****@*****.**') msgtext = 'Headers: headers\n\nbodybodybody\n-- \nsig\n' self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(not self.smtp.quitted) self.assert_(self.smtp.closed) finally: self.mailer.smtp.fail_on_quit = False
class TestSMTPMailer(unittest.TestCase): def setUp(self, port=None): global SMTP class SMTP(object): fail_on_quit = False def __init__(myself, h, p): myself.hostname = h myself.port = p myself.quitted = False myself.closed = False if type(p) == type(u""): raise socket.error("Int or String expected") self.smtp = myself def sendmail(self, f, t, m): self.fromaddr = f self.toaddrs = t self.msgtext = m def login(self, username, password): self.username = username self.password = password def quit(self): if self.fail_on_quit: raise socket.sslerror("dang") self.quitted = True self.close() def close(self): self.closed = True def has_extn(self, ext): return True def ehlo(self): self.does_esmtp = True return (200, "Hello, I am your stupid MTA mock") def starttls(self): pass if port is None: self.mailer = SMTPMailer() else: self.mailer = SMTPMailer(u"localhost", port) self.mailer.smtp = SMTP def test_interface(self): verifyObject(ISMTPMailer, self.mailer) def test_send(self): for run in (1, 2): if run == 2: self.setUp(u"25") fromaddr = "*****@*****.**" toaddrs = ("*****@*****.**", "*****@*****.**") msgtext = "Headers: headers\n\nbodybodybody\n-- \nsig\n" self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(self.smtp.quitted) self.assert_(self.smtp.closed) def test_send_auth(self): fromaddr = "*****@*****.**" toaddrs = ("*****@*****.**", "*****@*****.**") msgtext = "Headers: headers\n\nbodybodybody\n-- \nsig\n" self.mailer.username = "******" self.mailer.password = "******" self.mailer.hostname = "spamrelay" self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, "foo") self.assertEquals(self.smtp.password, "evil") self.assertEquals(self.smtp.hostname, "spamrelay") self.assertEquals(self.smtp.port, "31337") self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(self.smtp.quitted) self.assert_(self.smtp.closed) def test_send_auth_unicode(self): fromaddr = "*****@*****.**" toaddrs = ("*****@*****.**", "*****@*****.**") msgtext = "Headers: headers\n\nbodybodybody\n-- \nsig\n" self.mailer.username = u"f\u00f8\u00f8" # double o slash self.mailer.password = u"\u00e9vil" # e acute self.mailer.hostname = "spamrelay" self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, "f\xc3\xb8\xc3\xb8") self.assertEquals(self.smtp.password, "\xc3\xa9vil") def test_send_auth_nonascii(self): fromaddr = "*****@*****.**" toaddrs = ("*****@*****.**", "*****@*****.**") msgtext = "Headers: headers\n\nbodybodybody\n-- \nsig\n" self.mailer.username = "******" # double o slash self.mailer.password = "******" # e acute self.mailer.hostname = "spamrelay" self.mailer.port = 31337 self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.username, "f\xc3\xb8\xc3\xb8") self.assertEquals(self.smtp.password, "\xc3\xa9vil") def test_send_failQuit(self): self.mailer.smtp.fail_on_quit = True try: fromaddr = "*****@*****.**" toaddrs = ("*****@*****.**", "*****@*****.**") msgtext = "Headers: headers\n\nbodybodybody\n-- \nsig\n" self.mailer.send(fromaddr, toaddrs, msgtext) self.assertEquals(self.smtp.fromaddr, fromaddr) self.assertEquals(self.smtp.toaddrs, toaddrs) self.assertEquals(self.smtp.msgtext, msgtext) self.assert_(not self.smtp.quitted) self.assert_(self.smtp.closed) finally: self.mailer.smtp.fail_on_quit = False