Example #1
0
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)
Example #2
0
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
Example #3
0
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
Example #4
0
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
Example #5
0
from mail_service import MailService

ms = MailService()
ms.send_email("*****@*****.**", "test subject", "test message")
#https://pypi.org/project/gmail/
Example #6
0
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()