def __init__(self, config, januapath): self.config = config self.postman = None self.januapath = januapath if self.config.enable: connection = ESMTPConnection(config.smtp_host, config.smtp_port, config.smtp_username, config.smtp_password, local_hostname=None, ssl=config.smtp_ssl, tls=config.smtp_tls, timeout=config.smtp_timeout) self.postman = Postman(config.mail_from, connection)
def test_smtp(self): # Fake msg = Message(['*****@*****.**'], 'Test') postman = Postman('*****@*****.**', None) # It's never used anyway # Cannot use `None` as Connection self.assertRaises(AttributeError, postman.connect().__enter__) # Mock and test with postman.loopback() as lo: # Send with postman.connect() as c: c.sendmail(msg) # Check self.assertEqual(len(lo), 1) self.assertEqual(len(lo), 1) # Still usable # Now fails again self.assertRaises(AttributeError, postman.connect().__enter__)
def test_smtp(self): # Set socket timeout (because in some cases the test hangs for 120 seconds) socket.setdefaulttimeout(2.0) self.addCleanup(socket.setdefaulttimeout, None) # Fake msg = Message(['*****@*****.**'], 'Test') postman = Postman( '*****@*****.**', SMTPConnection('smtp.gmail.com', 587, '*****@*****.**', 'wrong', tls=True)) # try to connect: an error should occur try: with postman.connect() as c: raise AssertionError('Exception not raised') except smtplib.SMTPAuthenticationError as e: self.assertEqual(e.smtp_code, 535) self.assertIn(b'Username and Password not accepted', e.smtp_error)
def test_real_mail_smtpd(self): """ Test sending messages with a real SMTPD server """ # port+1 because python can't let it go.. :) hack? smtp_server = TestingSMTPServer(port=self.smtpd_port + 1) smtp_server.start() sleep(0.5) # Initialize a Postman postman = Postman( '*****@*****.**', NoLoginSMTP('localhost', self.smtpd_port + 1, None, None)) # Send messages with postman.connect() as c: # Send unicode message c.sendmail(Message(['*****@*****.**'], u'Håkon', u'Håkon')) # Test self.assertIsNotNone(smtp_server.received_data) # Clean-up smtp_server.close() smtp_server.join()
def test_real_mail_aiosmtpd(self): """ Test sending messages with a real-world SMTPD server """ if aiosmtpd is None: self.skipTest('aiosmtpd not available') # Start an smtp server mail_handler = StashingHandler() controller = Controller(mail_handler, loop=None, hostname='localhost', port=self.smtpd_port) controller.start() # Give it time to settle sleep(0.5) # Initialize a Postman postman = Postman( '*****@*****.**', NoLoginSMTP('localhost', self.smtpd_port, None, None)) # Send messages with postman.connect() as c: # Send plaintext message msg = Message(['*****@*****.**'], 'Subject', 'HTML message') c.sendmail(msg) # Send unicode message msg = Message(['*****@*****.**'], u'Håkon', u'Håkon') c.sendmail(msg) # Done controller.stop() # Test self.assertEqual(len(mail_handler.mail), 2)