예제 #1
0
def test_sending_unicode():
    global smtp_server
    smtp_server.flush_sink()

    mailer = SMTPMailer(host='127.0.0.1', port=8000, use_tls=False)
    email = EmailMessage(u'Olé', u'Contenido en español', u'*****@*****.**',
                         u'toБ@example.com')
    assert mailer.send_messages(email)
    sink = smtp_server.sink
    assert len(sink) == 1
예제 #2
0
def test_sending_unicode():
    global smtp_server
    smtp_server.flush_sink()

    mailer = SMTPMailer(host='127.0.0.1', port=8000, use_tls=False)
    email = EmailMessage(
        u'Olé',
        u'Contenido en español',
        u'*****@*****.**',
        u'toБ@example.com'
    )
    assert mailer.send_messages(email)
    sink = smtp_server.sink
    assert len(sink) == 1
예제 #3
0
def test_fail_silently():
    mailer = SMTPMailer(host='127.0.0.1', port=8000, use_tls=True,
                        fail_silently=True)
    mailer.open()

    mailer = SMTPMailer(host='123', port=8000, use_tls=False,
                        fail_silently=True)
    mailer.open()

    mailer = SMTPMailer(host='127.0.0.1', port=3000, use_tls=False,
                        fail_silently=True)
    mailer.open()
예제 #4
0
def mail(app_conf, project_root='.'):
    if not app_conf:
        return
    smtp_conf = {
        k[5:]: v
        for k, v in app_conf.__dict__.items() if k.startswith('smtp_')
    }
    if smtp_conf:
        smtp_conf.setdefault('timeout', app_conf.socket_timeout)
    mailer = SMTPMailer(**smtp_conf) if smtp_conf else DummyMailer()
    emails = {}
    emails_dir = project_root + '/emails/'
    i = len(emails_dir)
    for spt in find_files(emails_dir, '*.spt'):
        base_name = spt[i:-4]
        emails[base_name] = compile_email_spt(spt)

    def log_email(message):
        message = dict(message)
        html, text = message.pop('html'), message.pop('text')
        print('\n', ' ', '=' * 26, 'BEGIN EMAIL', '=' * 26)
        print(json.dumps(message))
        print('[---] text/html')
        print(html)
        print('[---] text/plain')
        print(text)
        print('  ', '=' * 27, 'END EMAIL', '=' * 27)

    log_email = log_email if app_conf.log_emails else lambda *a, **kw: None

    return {'emails': emails, 'log_email': log_email, 'mailer': mailer}
예제 #5
0
def test_sending():
    global smtp_server
    smtp_server.flush_sink()

    mailer = SMTPMailer(host=SMTP_HOST, port=SMTP_PORT, use_tls=False)
    email1, email2, email3, email4 = make_emails()

    assert mailer.send_messages(email1) == 1
    assert mailer.send_messages(email2, email3) == 2
    assert mailer.send_messages(email4) == 1

    sink = smtp_server.sink
    assert len(sink) == 4

    message = sink[0]
    print(message)
    assert message.get_content_type() == 'text/plain'
    assert message.get('subject') == 'Subject-1'
    assert message.get('from') == '*****@*****.**'
    assert message.get('to') == '*****@*****.**'
예제 #6
0
def test_sending():
    global smtp_server
    smtp_server.flush_sink()

    mailer = SMTPMailer(host=SMTP_HOST, port=SMTP_PORT, use_tls=False)
    email1, email2, email3, email4 = make_emails()

    assert mailer.send_messages(email1) == 1
    assert mailer.send_messages(email2, email3) == 2
    assert mailer.send_messages(email4) == 1

    sink = smtp_server.sink
    assert len(sink) == 4

    message = sink[0]
    print(message)
    assert message.get_content_type() == 'text/plain'
    assert message.get('subject') == 'Subject-1'
    assert message.get('from') == '*****@*****.**'
    assert message.get('to') == '*****@*****.**'
예제 #7
0
def test_batch_too_many_recipients():
    global smtp_server
    smtp_server.flush_sink()

    mailer = SMTPMailer(host='127.0.0.1', port=8000, use_tls=False, max_recipients=200)
    send_to = ['user{}@example.com'.format(i) for i in range(1, 1501)]
    msg = EmailMessage('The Subject', 'Content', '*****@*****.**', send_to)

    assert mailer.send_messages(msg) == 1
    sink = smtp_server.sink
    assert len(sink) == 8

    assert len(sink[0].get('to').split(',')) == 200
    assert len(sink[1].get('to').split(',')) == 200
    assert len(sink[2].get('to').split(',')) == 200
    assert len(sink[3].get('to').split(',')) == 200
    assert len(sink[4].get('to').split(',')) == 200
    assert len(sink[5].get('to').split(',')) == 200
    assert len(sink[6].get('to').split(',')) == 200
    assert len(sink[7].get('to').split(',')) == 100
예제 #8
0
def test_batch_too_many_recipients():
    global smtp_server
    smtp_server.flush_sink()

    mailer = SMTPMailer(host='127.0.0.1',
                        port=8000,
                        use_tls=False,
                        max_recipients=200)
    send_to = ['user{}@example.com'.format(i) for i in range(1, 1501)]
    msg = EmailMessage('The Subject', 'Content', '*****@*****.**', send_to)

    assert mailer.send_messages(msg) == 1
    sink = smtp_server.sink
    assert len(sink) == 8

    assert len(sink[0].get('to').split(',')) == 200
    assert len(sink[1].get('to').split(',')) == 200
    assert len(sink[2].get('to').split(',')) == 200
    assert len(sink[3].get('to').split(',')) == 200
    assert len(sink[4].get('to').split(',')) == 200
    assert len(sink[5].get('to').split(',')) == 200
    assert len(sink[6].get('to').split(',')) == 200
    assert len(sink[7].get('to').split(',')) == 100
예제 #9
0
def mail(app_conf, env, project_root='.'):
    if not app_conf:
        return
    smtp_conf = {
        k[5:]: v
        for k, v in app_conf.__dict__.items() if k.startswith('smtp_')
    }
    if smtp_conf:
        smtp_conf.setdefault('timeout', app_conf.socket_timeout)
    if getattr(app_conf, 'ses_region', None):
        mailer = AmazonSESMailer(env.aws_access_key_id,
                                 env.aws_secret_access_key,
                                 region_name=app_conf.ses_region)
    elif smtp_conf:
        mailer = SMTPMailer(**smtp_conf)
    else:
        mailer = ToConsoleMailer()
    emails = {}
    emails_dir = project_root + '/emails/'
    i = len(emails_dir)
    for spt in find_files(emails_dir, '*.spt'):
        base_name = spt[i:-4]
        emails[base_name] = compile_email_spt(spt)

    def log_email(message):
        message = dict(message)
        html, text = message.pop('html'), message.pop('text')
        print('\n', ' ', '=' * 26, 'BEGIN EMAIL', '=' * 26)
        print(json.dumps(message))
        print('[---] text/html')
        print(html)
        print('[---] text/plain')
        print(text)
        print('  ', '=' * 27, 'END EMAIL', '=' * 27)

    if app_conf.log_emails and not isinstance(mailer, ToConsoleMailer):
        log_email = log_email
    else:
        log_email = lambda *a, **kw: None

    return {'emails': emails, 'log_email': log_email, 'mailer': mailer}
예제 #10
0
def mail_report(name, recipients=None, stdout=False, template=None):
    reports_dir = ConfigDir().sub('reports').path
    config = Config.load_default()
    template_dir = ConfigDir().sub('templates').path
    template_name = template or config.mailing.default_template
    if not template_name.endswith('.html'):
        template_name += '.html'
    loader = ChoiceLoader([
        FileSystemLoader(template_dir),
        PackageLoader('athena.broadcasting', 'templates')
    ])
    env = Environment(loader=loader)
    template = env.get_template(template_name)

    job_file = path_join(reports_dir, name)
    if not isfile(job_file):
        raise ValueError(
            "{} does not exist or is not a readable file!".format(name))
    with open(job_file, 'r') as f:
        job = yaml.load(f.read())
    title = job.get('title')
    description = job.get('description')
    data = job.get('data')
    inline_blocks = data.get('inline')
    csv_items = data.get('csv')
    today = datetime.now().date().strftime('%d %b %Y')

    if not recipients:
        job_recepients = job.get('recipients')
        recipients = [
            recipient.strip() for recipient in job_recepients.split(',')
        ] if job_recepients else None
    if not data or (not inline_blocks and not csv_items):
        raise ValueError(
            "Your job config must contain a 'data' section with one or more inline or csv entries"
        )
    if not recipients and not stdout:
        raise ValueError("No recipients to send the data!")

    blocks = []
    if inline_blocks:
        for block_item in inline_blocks:
            block = {
                'name': block_item['name'],
                'description': block_item['description']
            }
            if block_item['type'] == 'sql':
                rows, headers = query_impala(block_item['query'])
                block['data'] = {'headers': headers, 'rows': rows}
            else:
                raise ValueError(
                    "{} contains an inline block of unknown type ({})!".format(
                        name, block_item['type']))

            blocks.append(block)
    csvs = []
    if csv_items:
        tmpdir = create_tmp_dir(prefix=slugify(title, separator='_'))
        for item in csv_items:
            filenameretrieve = item['filename']
            csv_path = path_join(tmpdir, filenameretrieve)
            if item['type'] == 'sql':
                sql_query = item['query']
                if 'with_items' in item:
                    variables = item['with_items']
                    for variable in variables:
                        processed_sql_query = sql_query.replace(
                            "{{ item }}", variable)
                        processed_filename = filenameretrieve.replace(
                            "{{ item }}", variable)
                        csv_path_instance = path_join(tmpdir,
                                                      processed_filename)
                        query_to_csv(processed_sql_query, csv_path_instance)
                        csvs.append({
                            'name': processed_filename,
                            'path': csv_path_instance
                        })
                else:
                    query_to_csv(sql_query, csv_path)
                    csvs.append({'name': filenameretrieve, 'path': csv_path})
            else:
                raise ValueError(
                    "{} contains csv item of unknown type ({})!".format(
                        name, item['type']))

    html = template.render(title=title,
                           description=description,
                           today=today,
                           blocks=blocks)
    if stdout:
        print html
        print "\n"
        for c in csvs:
            print c
    else:
        mailer = SMTPMailer(host=config.mailing.smtp_host,
                            port=config.mailing.smtp_port,
                            username=config.mailing.smtp_username,
                            password=config.mailing.smtp_password,
                            use_tls=config.mailing.smtp_use_tls)

        email_msg = EmailMessage(subject="{title} {date}".format(title=title,
                                                                 date=today),
                                 text="This mail can only be viewed as HTML",
                                 from_email=config.mailing.from_address,
                                 to=recipients,
                                 html=html)
        for c in csvs:
            email_msg.attach_file(c['path'])

        mailer.send_messages(email_msg)
예제 #11
0
def test_fail_silently():
    mailer = SMTPMailer(host='127.0.0.1',
                        port=8000,
                        use_tls=True,
                        fail_silently=True)
    mailer.open()

    mailer = SMTPMailer(host='123',
                        port=8000,
                        use_tls=False,
                        fail_silently=True)
    mailer.open()

    mailer = SMTPMailer(host='127.0.0.1',
                        port=3000,
                        use_tls=False,
                        fail_silently=True)
    mailer.open()
예제 #12
0
def test_wrong_port():
    with pytest.raises(Exception):
        mailer = SMTPMailer(host='127.0.0.1', port=3000, use_tls=False)
        mailer.open()
예제 #13
0
def test_notls():
    with pytest.raises(SMTPException):
        mailer = SMTPMailer(host='127.0.0.1', port=8000, use_tls=True)
        mailer.open()
예제 #14
0
def test_wrong_port():
    with pytest.raises(Exception):
        mailer = SMTPMailer(host='127.0.0.1', port=3000, use_tls=False)
        mailer.open()
예제 #15
0
def test_notls():
    with pytest.raises(SMTPException):
        mailer = SMTPMailer(host='127.0.0.1', port=8000, use_tls=True)
        mailer.open()