Type Annotations for Boto3 Pinpoint Email

1.42.3 · active · verified Sat Apr 11

mypy-boto3-pinpoint-email provides comprehensive type annotations for the Amazon Pinpoint Email Service client in the boto3 library, ensuring static type checking with tools like Mypy, improved IDE auto-completion, and early detection of potential runtime errors. It is currently at version 1.42.3 and is actively maintained with updates released in sync with boto3 and botocore, generated by the mypy-boto3-builder.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Pinpoint Email client with type annotations and send a simple email. It uses environment variables for sender and recipient emails for safe execution.

import os
from typing import TYPE_CHECKING

import boto3
from boto3.session import Session

# Import PinpointEmailClient for type hinting, conditionally for runtime performance
if TYPE_CHECKING:
    from mypy_boto3_pinpoint_email.client import PinpointEmailClient
    from mypy_boto3_pinpoint_email.type_defs import SendEmailResponseTypeDef

def send_pinpoint_email(recipient_email: str, sender_email: str, subject: str, body: str) -> None:
    """Sends an email using AWS Pinpoint Email service."""
    session = Session()
    client: 'PinpointEmailClient' = session.client("pinpoint-email")

    response: 'SendEmailResponseTypeDef' = client.send_email(
        FromEmailAddress=sender_email,
        Destination={
            'ToAddresses': [
                recipient_email,
            ],
        },
        Content={
            'Simple': {
                'Subject': {
                    'Data': subject,
                    'Charset': 'UTF-8'
                },
                'Body': {
                    'Text': {
                        'Data': body,
                        'Charset': 'UTF-8'
                    }
                }
            }
        }
    )
    print(f"Email sent! Message ID: {response['MessageId']}")

if __name__ == "__main__":
    # Replace with your verified sender and recipient email addresses
    # Ensure these are set as environment variables for production
    SENDER = os.environ.get('PINPOINT_SENDER_EMAIL', 'verified@example.com')
    RECIPIENT = os.environ.get('PINPOINT_RECIPIENT_EMAIL', 'recipient@example.com')
    EMAIL_SUBJECT = "Hello from mypy-boto3 Pinpoint Email!"
    EMAIL_BODY = "This is a test email sent using AWS Pinpoint Email with type annotations."

    if SENDER == 'verified@example.com' or RECIPIENT == 'recipient@example.com':
        print("Please set PINPOINT_SENDER_EMAIL and PINPOINT_RECIPIENT_EMAIL environment variables.")
        print("Make sure the sender email is verified in AWS Pinpoint Email.")
    else:
        send_pinpoint_email(RECIPIENT, SENDER, EMAIL_SUBJECT, EMAIL_BODY)

view raw JSON →