def test_html_image(self): msg = Message( ['*****@*****.**'], u"Mail'em test with inline images", u"Cute: <img src='cid:cute.jpg' />", # cid:<filename> attachments=[ ImageAttachment('cute.jpg', b'\xff\xd8\xff\xe0\x00\x10JFIF', 'inline') ]) msg_str = str(msg) self.assertIn('multipart/mixed', msg_str) self.assertNotIn('multipart/alternative', msg_str) self.assertIn('text/html', msg_str) self.assertNotIn('text/plain', msg_str) self.assertIn('Content-Type: image/jpeg', msg_str) self.assertIn('Content-ID: <cute.jpg>', msg_str) if PY2: self.assertIn( 'Content-Disposition: inline; filename="=?utf-8?q?cute=2Ejpg?="', msg_str) else: self.assertIn('Content-Disposition: inline; filename="cute.jpg"', msg_str)
def test_html(self): msg = Message(['*****@*****.**'], u"Mail'em test °C", u"<b>yeah baby, it works!</b>", attachments=[Attachment(u'test.txt', 'abc')], sender=('a@b', 'A'), cc=[('a@b', 'A'), 'b@b'], bcc=[('a@b', 'A'), 'b@b'], reply_to='z@b', headers={'Custom': 'Test'}) msg_str = str(msg) self.assertIn('multipart/mixed', msg_str) self.assertNotIn('multipart/alternative', msg_str) self.assertIn('text/html', msg_str) self.assertNotIn('text/plain', msg_str) self.assertIn('application/octet-stream', msg_str) self.assertIn('Content-Disposition: attachment; filename="test.txt"', msg_str) self.assertIn('Subject: =?utf-8?q?Mail=27em_test_=C2=B0C?=', msg_str) self.assertIn('To: [email protected]', msg_str) if PY2: self.assertIn('From: =?utf-8?q?A?= <a@b>', msg_str) self.assertIn('Cc: =?utf-8?q?A?= <a@b>, b@b', msg_str) self.assertIn('Bcc: =?utf-8?q?A?= <a@b>, b@b', msg_str) else: self.assertIn('From: A <a@b>', msg_str) self.assertIn('Cc: A <a@b>, b@b', msg_str) self.assertIn('Bcc: A <a@b>, b@b', msg_str) self.assertIn('Reply-To: z@b', msg_str) self.assertIn('Message-ID:', msg_str) self.assertIn('Custom: Test', msg_str) self.assertIn('', msg_str)
def sendmail(self, mailobj): if not isinstance(mailobj, MailObj): raise MailError('argument must be MailObj') if not self.config.enable: raise MailError('Mail option has been disabled, dropping mail ...') subject = mailobj.subject message = mailobj.message if isinstance(mailobj.to, list): to = mailobj.to else: to = [mailobj.to] template = mailobj.template template_args = mailobj.template_args reply_to = mailobj.reply_to bcc = mailobj.bcc if self.postman: if template: tmpl = self.get_template(template) msg = tmpl(to, template_args, reply_to=reply_to, bcc=bcc) else: msg = Message(to, subject, text=message, reply_to=reply_to, bcc=bcc) if msg: try: with self.postman.connect() as c: c.sendmail(msg) except Exception, err: raise MailError(err) return True
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)
def test_html_text(self): msg = Message(['*****@*****.**'], u"Mail'em test", u'Mailtest', u"<b>yeah baby, it works!</b>", attachments=[Attachment(u'test.txt', 'abc')]) msg_str = str(msg) self.assertIn('multipart/mixed', msg_str) self.assertIn('multipart/alternative', msg_str) self.assertIn('text/html', msg_str) self.assertIn('text/plain', msg_str) self.assertIn('Content-Type: application/octet-stream', msg_str) self.assertIn('Content-Disposition: attachment; filename="test.txt"', msg_str)
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()