How to use the mailmerge.sendmail_client.SendmailClient function in mailmerge

To help you get started, we’ve selected a few mailmerge examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_no_dry_run(mock_getpass, mock_SMTP_SSL, tmp_path):
    """Verify --no-dry-run calls SMTP sendmail()."""
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = open-smtp.example.com
        port = 465
        security = SSL/TLS
        username = admin
    """))
    sendmail_client = SendmailClient(config_path, dry_run=False)
    message = email.message_from_string(u"""
        TO: test@test.com
        SUBJECT: Testing mailmerge
        FROM: test@test.com

        Hello world
    """)

    # Mock the password entry
    mock_getpass.return_value = "password"

    # Send a message
    sendmail_client.sendmail(
        sender="from@test.com",
        recipients=["to@test.com"],
        message=message,
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_bad_config_key(tmp_path):
    """Verify config file with bad key throws an exception."""
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        badkey = open-smtp.example.com
    """))
    with pytest.raises(configparser.Error):
        SendmailClient(config_path, dry_run=True)
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_dry_run(mock_getpass, mock_SMTP, tmp_path):
    """Verify no sendmail() calls when dry_run=True."""
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = open-smtp.example.com
        port = 25
        security = Never
    """))
    sendmail_client = SendmailClient(
        config_path,
        dry_run=True,
    )
    message = email.message_from_string(u"""
        TO: test@test.com
        SUBJECT: Testing mailmerge
        FROM: test@test.com

        Hello world
    """)

    sendmail_client.sendmail(
        sender="from@test.com",
        recipients=["to@test.com"],
        message=message,
    )
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_smtp(mock_SMTP, tmp_path):
    """Verify SMTP library calls."""
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = open-smtp.example.com
        port = 25
    """))
    sendmail_client = SendmailClient(
        config_path,
        dry_run=False,
    )
    message = email.message_from_string(u"""
        TO: to@test.com
        SUBJECT: Testing mailmerge
        FROM: from@test.com

        Hello world
    """)

    sendmail_client.sendmail(
        sender="from@test.com",
        recipients=["to@test.com"],
        message=message,
    )
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_security_open(mock_getpass, mock_SMTP_SSL, mock_SMTP, tmp_path):
    """Verify open (Never) security configuration."""
    # Config for no security SMTP server
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = open-smtp.example.com
        port = 25
    """))

    # Simple template
    sendmail_client = SendmailClient(config_path, dry_run=False)
    message = email.message_from_string(u"Hello world")

    # Send a message
    sendmail_client.sendmail(
        sender="test@test.com",
        recipients=["test@test.com"],
        message=message,
    )

    # Verify SMTP library calls
    assert mock_getpass.call_count == 0
    assert mock_SMTP.call_count == 1
    assert mock_SMTP_SSL.call_count == 0
    smtp = mock_SMTP.return_value.__enter__.return_value
    assert smtp.sendmail.call_count == 1
    assert smtp.login.call_count == 0
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_smtp_error(mock_getpass, mock_SMTP_SSL, tmp_path):
    """Connection is closed on error."""
    # Config for SSL SMTP server
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = smtp.mail.umich.edu
        port = 465
        security = SSL/TLS
        username = YOUR_USERNAME_HERE
    """))

    # Simple template
    sendmail_client = SendmailClient(config_path, dry_run=False)
    message = email.message_from_string(u"Hello world")

    # Mock the password entry
    mock_getpass.return_value = "password"

    # Configure SMTP_SSL to raise an exception
    mock_SMTP_SSL.return_value.__enter__.return_value.login = mock.Mock(
        side_effect=smtplib.SMTPAuthenticationError(
            code=534,
            msg="Error from mock",
        )
    )

    # Send a message
    with pytest.raises(smtplib.SMTPAuthenticationError):
        sendmail_client.sendmail(
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_security_open_legacy(mock_SMTP, tmp_path):
    """Verify legacy "security = Never" configuration."""
    # Config SMTP server with "security = Never" legacy option
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = open-smtp.example.com
        port = 25
        security = Never
    """))

    # Simple template
    sendmail_client = SendmailClient(config_path, dry_run=False)
    message = email.message_from_string(u"Hello world")

    # Send a message
    sendmail_client.sendmail(
        sender="test@test.com",
        recipients=["test@test.com"],
        message=message,
    )

    # Verify SMTP library calls
    smtp = mock_SMTP.return_value.__enter__.return_value
    assert smtp.sendmail.call_count == 1
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_security_starttls(mock_getpass, mock_SMTP_SSL, mock_SMTP, tmp_path):
    """Verify open (Never) security configuration."""
    # Config for STARTTLS SMTP server
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = newman.eecs.umich.edu
        port = 25
        security = STARTTLS
        username = YOUR_USERNAME_HERE
    """))

    # Simple template
    sendmail_client = SendmailClient(config_path, dry_run=False)
    message = email.message_from_string(u"Hello world")

    # Mock the password entry
    mock_getpass.return_value = "password"

    # Send a message
    sendmail_client.sendmail(
        sender="test@test.com",
        recipients=["test@test.com"],
        message=message,
    )

    # Verify SMTP library calls
    assert mock_getpass.call_count == 1
    assert mock_SMTP.call_count == 1
    assert mock_SMTP_SSL.call_count == 0
github awdeorio / mailmerge / tests / test_sendmail_client.py View on Github external
def test_security_error(tmp_path):
    """Verify config file with bad security type throws an exception."""
    config_path = tmp_path/"server.conf"
    config_path.write_text(textwrap.dedent(u"""\
        [smtp_server]
        host = smtp.mail.umich.edu
        port = 465
        security = bad_value
        username = YOUR_USERNAME_HERE
    """))
    with pytest.raises(configparser.Error):
        SendmailClient(config_path, dry_run=False)
github awdeorio / mailmerge / mailmerge / __main__.py View on Github external
# https://github.com/pallets/click/issues/405
    template_path = Path(template_path)
    database_path = Path(database_path)
    config_path = Path(config_path)

    # Verify input files are present and give user hints about creating them
    check_input_files(template_path, database_path, config_path, sample)

    # No limit is an alias for limit=-1
    if no_limit:
        limit = -1

    try:
        template_message = TemplateMessage(template_path)
        csv_database = read_csv_database(database_path)
        sendmail_client = SendmailClient(config_path, dry_run)
        for i, row in enumerate_limit(csv_database, limit):
            sender, recipients, message = template_message.render(row)
            sendmail_client.sendmail(sender, recipients, message)
            print(">>> message {}".format(i + 1))
            print(message.as_string())
            for filename in get_attachment_filenames(message):
                print(">>> attached {}".format(filename))
            print(">>> sent message {}".format(i + 1))
    except jinja2.exceptions.TemplateError as err:
        sys.exit(">>> Error in Jinja2 template: {}".format(err))
    except csv.Error as err:
        sys.exit(">>> Error reading CSV file: {}".format(err))
    except smtplib.SMTPAuthenticationError as err:
        sys.exit(">>> Authentication error: {}".format(err))
    except configparser.Error as err:
        sys.exit(