Skip to content

SES

Utilities for working with Amazon Simple Email Service.


is_verified

is_verified(identity)

Check if an email address or domain identity is verified in SES.

Parameters:

Name Type Description Default
identity str

The email address or domain to check.

required

Raises:

Type Description
AWSError

If the verification status cannot be checked.

Returns:

Type Description
bool

True if the identity is verified, False otherwise.

Example
# Check email identity
is_verified('myemail@subdomain.domain.com')

# Check subdomain identity
is_verified('subdomain.domain.com')

# Check domain identity
is_verified('domain.com')
Source code in src/aibs_informatics_aws_utils/ses.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def is_verified(identity: str) -> bool:
    """Check if an email address or domain identity is verified in SES.

    Args:
        identity (str): The email address or domain to check.

    Raises:
        AWSError: If the verification status cannot be checked.

    Returns:
        True if the identity is verified, False otherwise.

    Example:
        ```python
        # Check email identity
        is_verified('myemail@subdomain.domain.com')

        # Check subdomain identity
        is_verified('subdomain.domain.com')

        # Check domain identity
        is_verified('domain.com')
        ```
    """
    ses = get_ses_client(region=get_region())
    try:
        response = ses.get_identity_verification_attributes(Identities=[identity])
        v_attrs = response["VerificationAttributes"]
        if identity not in v_attrs:
            return False
        return v_attrs[identity]["VerificationStatus"] == "Success"
    except ClientError as e:
        logger.exception(e)
        raise AWSError(f"Could not check verification status, error: {e}")

send_email

send_email(request)

Send an email using SES.

Parameters:

Name Type Description Default
request SendEmailRequestTypeDef

The SES send email request configuration.

required

Raises:

Type Description
AWSError

If the email cannot be sent.

Returns:

Type Description
SendEmailResponseTypeDef

The send email response containing the message ID.

Source code in src/aibs_informatics_aws_utils/ses.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
def send_email(request: SendEmailRequestTypeDef) -> SendEmailResponseTypeDef:
    """Send an email using SES.

    Args:
        request (SendEmailRequestTypeDef): The SES send email request configuration.

    Raises:
        AWSError: If the email cannot be sent.

    Returns:
        The send email response containing the message ID.
    """
    logger.info(f"Sending email request: {request}")
    ses = get_ses_client(region=get_region())

    try:
        response = ses.send_email(**request)
    except ClientError as e:
        logger.exception(e.response)
        raise AWSError(f"Could not send email, error: {e}, {e.response}")

    return response

send_email_with_attachment

send_email_with_attachment(
    source,
    to_addresses,
    subject,
    body="",
    attachments_paths=None,
)

Send an email with attachments using SES.

Parameters:

Name Type Description Default
source Union[str, EmailAddress]

The sender email address.

required
to_addresses Sequence[Union[str, EmailAddress]]

List of recipient addresses.

required
subject str

The email subject line.

required
body Union[str, MIMEText]

The email body (plain text or MIMEText for HTML).

''
attachments_paths Optional[List[Path]]

Optional list of file paths to attach.

None

Returns:

Type Description
SendRawEmailResponseTypeDef

The send raw email response containing the message ID.

Source code in src/aibs_informatics_aws_utils/ses.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def send_email_with_attachment(
    source: Union[str, EmailAddress],
    to_addresses: Sequence[Union[str, EmailAddress]],
    subject: str,
    body: Union[str, MIMEText] = "",
    attachments_paths: Optional[List[Path]] = None,
) -> SendRawEmailResponseTypeDef:
    """Send an email with attachments using SES.

    Args:
        source (Union[str, EmailAddress]): The sender email address.
        to_addresses (Sequence[Union[str, EmailAddress]]): List of recipient addresses.
        subject (str): The email subject line.
        body (Union[str, MIMEText]): The email body (plain text or MIMEText for HTML).
        attachments_paths (Optional[List[Path]]): Optional list of file paths to attach.

    Returns:
        The send raw email response containing the message ID.
    """
    msg = MIMEMultipart("mixed")
    msg["Subject"] = subject
    msg["From"] = source
    msg["To"] = ", ".join(to_addresses)

    msg_body = MIMEMultipart("alternative")
    if isinstance(body, str):
        msg_body.attach(MIMEText(body))
    else:
        msg_body.attach(body)
    msg.attach(msg_body)

    if attachments_paths is not None:
        for attachments_path in attachments_paths:
            attachment_obj = _construct_mime_attachment_from_path(path=attachments_path)
            msg.attach(attachment_obj)

    return send_raw_email(SendRawEmailRequestTypeDef(RawMessage={"Data": msg.as_string()}))

send_raw_email

send_raw_email(request)

Send a raw MIME email using SES.

Parameters:

Name Type Description Default
request SendRawEmailRequestTypeDef

The SES send raw email request.

required

Raises:

Type Description
AWSError

If the email cannot be sent.

Returns:

Type Description
SendRawEmailResponseTypeDef

The send raw email response containing the message ID.

Source code in src/aibs_informatics_aws_utils/ses.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def send_raw_email(request: SendRawEmailRequestTypeDef) -> SendRawEmailResponseTypeDef:
    """Send a raw MIME email using SES.

    Args:
        request (SendRawEmailRequestTypeDef): The SES send raw email request.

    Raises:
        AWSError: If the email cannot be sent.

    Returns:
        The send raw email response containing the message ID.
    """
    logger.info(f"Sending email request: {request}")
    ses = get_ses_client(region=get_region())

    try:
        response = ses.send_raw_email(**request)
    except ClientError as e:
        logger.exception(e.response)
        raise AWSError(f"Could not send email, error: {e}, {e.response}")

    return response

send_simple_email

send_simple_email(source, to_addresses, subject, body='')

Send a simple text email using SES.

Parameters:

Name Type Description Default
source Union[str, EmailAddress]

The sender email address.

required
to_addresses Sequence[Union[str, EmailAddress]]

List of recipient addresses.

required
subject str

The email subject line.

required
body str

The email body text. Defaults to empty string.

''

Returns:

Type Description
SendEmailResponseTypeDef

The send email response containing the message ID.

Source code in src/aibs_informatics_aws_utils/ses.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def send_simple_email(
    source: Union[str, EmailAddress],
    to_addresses: Sequence[Union[str, EmailAddress]],
    subject: str,
    body: str = "",
) -> SendEmailResponseTypeDef:
    """Send a simple text email using SES.

    Args:
        source (Union[str, EmailAddress]): The sender email address.
        to_addresses (Sequence[Union[str, EmailAddress]]): List of recipient addresses.
        subject (str): The email subject line.
        body (str): The email body text. Defaults to empty string.

    Returns:
        The send email response containing the message ID.
    """
    return send_email(
        SendEmailRequestTypeDef(
            Source=source,
            Destination={"ToAddresses": to_addresses},
            Message={"Subject": {"Data": subject}, "Body": {"Text": {"Data": body}}},
        )
    )

verify_email_identity

verify_email_identity(email_address)

Send a verification email to an email address.

Initiates the email verification process for SES. The recipient will receive an email with a verification link.

Parameters:

Name Type Description Default
email_address str

The email address to verify.

required

Returns:

Type Description
Dict[str, Any]

The SES verify email identity response.

Source code in src/aibs_informatics_aws_utils/ses.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def verify_email_identity(email_address: str) -> Dict[str, Any]:  # no type def?
    """Send a verification email to an email address.

    Initiates the email verification process for SES. The recipient will
    receive an email with a verification link.

    Args:
        email_address (str): The email address to verify.

    Returns:
        The SES verify email identity response.
    """
    ses = get_ses_client(region=get_region())
    response = ses.verify_email_identity(EmailAddress=email_address)
    return response