class PyMailCli(): def __init__(self): self.parser = ArgumentParser( description= 'This is a python email sender which can be used from the command line.' ) self.parser.add_argument('--recipient', '-r', nargs='+', type=str) self.parser.add_argument('--body', '-b', type=str) self.parser.add_argument('--subject', '-s', type=str) self.parser.add_argument('--attachment', '-a', type=str) self.parser.add_argument('--type', '-t', type=str) self.args = self.parser.parse_args() self.recipients = self.args.recipient self.body = self.args.body self.subject = self.args.subject self.attachment = self.args.attachment self.type = self.args.type print(self.args) self.mail_service = MailService(self.recipients) def send_mail(self): self.mail_service.send_email(self.body, self.subject, self.attachment, self.type)
def test_mail_service_error_should_set_status(): m = new_email() ms = MailService(db) class MockPostman(object): def deliver(self, from_email, to, subject, text, html=None): return {'success': False, 'status': 400} ms.postman = MockPostman() ms.send_mail(m) mail = Email.by_id(str(m.id)) assert mail.status == Statuses.error
def test_successfull_mail(): m = new_email() ms = MailService(db) class MockPostman(object): def deliver(self, from_email, to, subject, text, html=None): return {'success': True, 'status': 200} ms.postman = MockPostman() ms.send_mail(m) mail = Email.by_id(str(m.id)) assert mail.status == Statuses.success
def test_mail_service_failover(): m = new_email() ms = MailService(db) class MockPostman(object): def __init__(self): self.switched = False def deliver(self, from_email, to, subject, text, html=None): return {'success': False, 'status': 500} def switch(self): self.switched = True ms.postman = MockPostman() ms.send_mail(m) mail = Email.by_id(str(m.id)) assert ms.postman.switched
from mail_service import MailService ms = MailService() ms.send_email("*****@*****.**", "test subject", "test message") #https://pypi.org/project/gmail/
def main(): py_mail = PyMailCli() cli_mail = MailService(py_mail.recipients) py_mail.send_mail()
#!/usr/bin/python3 from mail_service import MailService import os # to debug, uncomment the following environment vars and guarantee that files exists into your system # os.environ["MAIL_TO"]="*****@*****.**" # os.environ["SMTP_GOOGLE_MAIL_USER_FILE"]="/run/secrets/google.mail.user" # os.environ["SMTP_GOOGLE_MAIL_PASS_FILE"]="/run/secrets/google.mail.pass" # os.environ["POSTGRES_USER_FILE"]="/run/secrets/postgres.user.geoserver" # os.environ["POSTGRES_PASS_FILE"]="/run/secrets/postgres.pass.geoserver" mailService = MailService() mailService.sendMails()