def init_mailthon(self): self.postman = Postman( host=self.server, port=self.port, middlewares=[ TLS(force=True), Auth(username=self.username, password=self.password) ], )
def send_email(config, user_email, subject, content): envelope = email(sender=SENDER, receivers=[user_email], subject=subject, content=content) postman = Postman(host=config['host'], port=int(config['port']), middlewares=[TLS(force=True), Auth(username=config['username'], password=config['password'])]) postman.send(envelope)
def test_send_email_example(self, smtpserver): p = Postman(*smtpserver.addr) r = p.send( email( content='<p>Hello 世界</p>', subject='Hello world', sender='John <*****@*****.**>', receivers=['*****@*****.**'], )) assert r.ok assert len(smtpserver.outbox) == 1
def postman(self): def config(**kwargs): session.opts = kwargs return session session = Mock(spec=Session) session.side_effect = config session.send.return_value = SendmailResponse(250, 'ok', {}) return Postman( session=session, host='host', port=1000, )
def __init__(self): """check email-service parameters from config.py""" if self.host == "": self.error_message = "Email Error: host is empty" elif self.username == "": self.error_message = "Email Error: username is empty" elif self.password == "": self.error_message = "Email Error: password is empty" else: self.available = True # initial postman self.postman = Postman(host=self.host, port=self.port, middlewares=[ TLS(force=True), Auth(username=self.username, password=self.password) ])
def send_email(config, user_email, subject, content): envelope = email( sender=SENDER, receivers=[user_email], subject=subject, content=content ) postman = Postman(host=config['mail.host'], port=int(config['mail.port']), middlewares=[TLS(force=True), Auth(username=config['mail.username'], password=config['mail.password'])]) try: response = postman.send(envelope) if response.ok: logger.info("Successfully send email to %s" % user_email) else: logger.error("Failed to send email to %s" % user_email) except Exception, e: logger.error("Exception occured when send email to %s" % e)
def postman(host, port=587, auth=(None, None), force_tls=False, options=None): """ Creates a Postman object with TLS and Auth middleware. TLS is placed before authentication because usually authentication happens and is accepted only after TLS is enabled. :param auth: Tuple of (username, password) to be used to ``login`` to the server. :param force_tls: Whether TLS should be forced. :param options: Dictionary of keyword arguments to be used when the SMTP class is called. """ return Postman(host=host, port=port, middlewares=[ middleware.tls(force=force_tls), middleware.auth(*auth), ], **options)
def email(): postman = Postman( host='SOLLOES-VM02.SOLLOBRASIL.NET', port=587, middlewares=[ TLS(force=True), Auth(username='******', password='******') ], ) envelope = email( sender='sender <*****@*****.**>', receivers=['*****@*****.**'], subject='Manutenção/Suporte', content='', ) response = postman.send(envelope) print(response.message) print(response.status_code) if response.ok: print("OK! :)")
def test_send_email_attachment(self, smtpserver): p = Postman(*smtpserver.addr) r = p.send( email( sender='Me <*****@*****.**>', receivers=['*****@*****.**'], subject='Something', content='<p>hi</p>', attachments=['tests/assets/spacer.gif'], cc=['*****@*****.**', '*****@*****.**'], bcc=['*****@*****.**', '*****@*****.**'], encoding='ascii', )) assert r.ok assert len(smtpserver.outbox) == 1 message = smtpserver.outbox[0] assert message['Content-Type'].startswith('multipart/mixed') assert message['Subject'] == 'Something' assert message['To'] == '*****@*****.**' assert message['CC'] == '[email protected], [email protected]' assert message['From'] == 'Me <*****@*****.**>'
def postman(self, smtp): p = Postman(self.host, self.port) p.transport = smtp return p
from mailthon.postman import Postman from mailthon.middleware import TLS, Auth from mailthon import email while (True): postman = Postman( host='smtp-mail.outlook.com', port=587, middlewares=[ TLS(force=True), Auth(username='******', password='') ], ) envelope = email( sender='sender <*****@*****.**>', receivers=['*****@*****.**'], subject='Manutenção/Suporte', content='Hi', ) response = postman.send(envelope) print(response.message) print(response.status_code) if response.ok: print("OK! :)")