Beispiel #1
0
def test_get_identity_status(make_stubber, error_code):
    ses_client = boto3.client('ses')
    ses_stubber = make_stubber(ses_client)
    ses_identity = SesIdentity(ses_client)
    email = '*****@*****.**'
    status = 'Pending'

    ses_stubber.stub_get_identity_verification_attributes(
        [email], [status], error_code=error_code)

    if error_code is None:
        got_status = ses_identity.get_identity_status(email)
        assert got_status == status
    else:
        with pytest.raises(ClientError) as exc_info:
            ses_identity.get_identity_status(email)
        assert exc_info.value.response['Error']['Code'] == error_code
Beispiel #2
0
def test_verify_domain_identity(make_stubber, error_code):
    ses_client = boto3.client('ses')
    ses_stubber = make_stubber(ses_client)
    ses_identity = SesIdentity(ses_client)
    domain_name = 'example.com'
    token = 'test-token'

    ses_stubber.stub_verify_domain_identity(domain_name,
                                            token,
                                            error_code=error_code)

    if error_code is None:
        got_token = ses_identity.verify_domain_identity(domain_name)
        assert token == got_token
    else:
        with pytest.raises(ClientError) as exc_info:
            ses_identity.verify_domain_identity(domain_name)
        assert exc_info.value.response['Error']['Code'] == error_code
Beispiel #3
0
def test_list_identities(make_stubber, error_code):
    ses_client = boto3.client('ses')
    ses_stubber = make_stubber(ses_client)
    ses_identity = SesIdentity(ses_client)
    identity_type = 'EmailAddress'
    max_items = 5
    identities = ['*****@*****.**', '*****@*****.**']

    ses_stubber.stub_list_identities(identity_type,
                                     max_items,
                                     identities,
                                     error_code=error_code)

    if error_code is None:
        got_identities = ses_identity.list_identities(identity_type, max_items)
        assert got_identities == identities
    else:
        with pytest.raises(ClientError) as exc_info:
            ses_identity.list_identities(identity_type, max_items)
        assert exc_info.value.response['Error']['Code'] == error_code
Beispiel #4
0
def test_delete_identity(make_stubber, error_code):
    ses_client = boto3.client('ses')
    ses_stubber = make_stubber(ses_client)
    ses_identity = SesIdentity(ses_client)
    email = '*****@*****.**'

    ses_stubber.stub_delete_identity(email, error_code=error_code)

    if error_code is None:
        ses_identity.delete_identity(email)
    else:
        with pytest.raises(ClientError) as exc_info:
            ses_identity.delete_identity(email)
        assert exc_info.value.response['Error']['Code'] == error_code
Beispiel #5
0
def test_wait_until_identity_exists(make_stubber, error_code):
    ses_client = boto3.client('ses')
    ses_stubber = make_stubber(ses_client)
    ses_identity = SesIdentity(ses_client)
    email = '*****@*****.**'

    ses_stubber.stub_get_identity_verification_attributes(
        [email], ['Success'], error_code=error_code)

    if error_code is None:
        ses_identity.wait_until_identity_exists(email)
    else:
        with pytest.raises(WaiterError):
            ses_identity.wait_until_identity_exists(email)
def usage_demo():
    print('-'*88)
    print("Welcome to the Amazon Simple Email Service (Amazon SES) email demo!")
    print('-'*88)

    logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')

    ses_client = boto3.client('ses')
    ses_identity = SesIdentity(ses_client)
    ses_mail_sender = SesMailSender(ses_client)
    ses_template = SesTemplate(ses_client)
    email = input(
        "Enter an email address to send mail with Amazon SES: ")
    status = ses_identity.get_identity_status(email)
    verified = status == 'Success'
    if not verified:
        answer = input(
            f"The address '{email}' is not verified with Amazon SES. Unless your "
            f"Amazon SES account is out of sandbox, you can send mail only from "
            f"and to verified accounts. Do you want to verify this account for use "
            f"with Amazon SES? If yes, the address will receive a verification "
            f"email (y/n): ")
        if answer.lower() == 'y':
            ses_identity.verify_email_identity(email)
            print(f"Follow the steps in the email to {email} to complete verification.")
            print("Waiting for verification...")
            try:
                ses_identity.wait_until_identity_exists(email)
                print(f"Identity verified for {email}.")
                verified = True
            except WaiterError:
                print(f"Verification timeout exceeded. You must complete the "
                      f"steps in the email sent to {email} to verify the address.")

    if verified:
        test_message_text = "Hello from the Amazon SES mail demo!"
        test_message_html = "<p>Hello!</p><p>From the <b>Amazon SES</b> mail demo!</p>"

        print(f"Sending mail from {email} to {email}.")
        ses_mail_sender.send_email(
            email, SesDestination([email]), "Amazon SES demo",
            test_message_text, test_message_html)
        input("Mail sent. Check your inbox and press Enter to continue.")

        template = {
            'name': 'doc-example-template',
            'subject': 'Example of an email template.',
            'text': "This is what {{name}} will {{action}} if {{name}} can't display "
                    "HTML.",
            'html': "<p><i>This</i> is what {{name}} will {{action}} if {{name}} "
                    "<b>can</b> display HTML.</p>"}
        print("Creating a template and sending a templated email.")
        ses_template.create_template(**template)
        template_data = {'name': email.split('@')[0], 'action': 'read'}
        if ses_template.verify_tags(template_data):
            ses_mail_sender.send_templated_email(
                email, SesDestination([email]), ses_template.name(), template_data)
            input("Mail sent. Check your inbox and press Enter to continue.")

        print("Sending mail through the Amazon SES SMTP server.")
        boto3_session = boto3.Session()
        region = boto3_session.region_name
        credentials = boto3_session.get_credentials()
        port = 587
        smtp_server = f'email-smtp.{region}.amazonaws.com'
        password = calculate_key(credentials.secret_key, region)
        message = """
Subject: Hi there

This message is sent from the Amazon SES SMTP mail demo."""
        context = ssl.create_default_context()
        with smtplib.SMTP(smtp_server, port) as server:
            server.starttls(context=context)
            server.login(credentials.access_key, password)
            server.sendmail(email, email, message)
        print("Mail sent. Check your inbox!")

    if ses_template.template is not None:
        print("Deleting demo template.")
        ses_template.delete_template()
    if verified:
        answer = input(f"Do you want to remove {email} from Amazon SES (y/n)? ")
        if answer.lower() == 'y':
            ses_identity.delete_identity(email)
    print("Thanks for watching!")
    print('-'*88)