Amazon Simple Email Service (SES)¶
Amazon Simple Email Service (Amazon SES) is a cloud email service provider that can integrate into any application for bulk email sending. Whether you send transactional or marketing emails, you pay only for what you use. Amazon SES also supports a variety of deployments including dedicated, shared, or owned IP addresses. Reports on sender statistics and a deliverability dashboard help businesses make every email count.
Prerequisite Tasks¶
To use these operators, you must do a few things:
Create necessary resources using AWS Console or AWS CLI.
Install API libraries via pip.
pip install 'apache-airflow[amazon]'Detailed information is available Installation of Airflow®
Generic Parameters¶
- aws_conn_id
Reference to Amazon Web Services Connection ID. If this parameter is set to
Nonethen the default boto3 behaviour is used without a connection lookup. Otherwise use the credentials stored in the Connection. Default:aws_default- region_name
AWS Region Name. If this parameter is set to
Noneor omitted then region_name from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None- verify
Whether or not to verify SSL certificates.
False- Do not validate SSL certificates.path/to/cert/bundle.pem - A filename of the CA cert bundle to use. You can specify this argument if you want to use a different CA cert bundle than the one used by botocore.
If this parameter is set to
Noneor is omitted then verify from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:None- botocore_config
The provided dictionary is used to construct a botocore.config.Config. This configuration can be used to configure Avoid Throttling exceptions, timeouts, etc.
Example, for more detail about parameters please have a look botocore.config.Config¶{ "signature_version": "unsigned", "s3": { "us_east_1_regional_endpoint": True, }, "retries": { "mode": "standard", "max_attempts": 10, }, "connect_timeout": 300, "read_timeout": 300, "tcp_keepalive": True, }
If this parameter is set to
Noneor omitted then config_kwargs from AWS Connection Extra Parameter will be used. Otherwise use the specified value instead of the connection value. Default:NoneNote
Specifying an empty dictionary,
{}, will overwrite the connection configuration for botocore.config.Config
Operators¶
Send an email using Amazon SES¶
To send an email using Amazon Simple Email Service you can use
SesEmailOperator.
The following example shows how to send a basic email:
# Basic email sending
# Note: In SES sandbox mode, both sender and recipient must be verified.
send_basic_email = SesEmailOperator(
task_id="send_basic_email",
mail_from=verified_email,
to=[verified_email],
subject="Test Email from Airflow",
html_content="<h1>Hello</h1><p>This is a test email sent via Amazon SES.</p>",
aws_conn_id="aws_default",
)
You can also send emails with CC and BCC recipients:
# Email with CC and BCC
send_email_with_cc_bcc = SesEmailOperator(
task_id="send_email_with_cc_bcc",
mail_from=verified_email,
to=[verified_email],
cc=[verified_email],
bcc=[verified_email],
subject="Test Email with CC and BCC",
html_content="<h1>Hello</h1><p>This email has CC and BCC recipients.</p>",
aws_conn_id="aws_default",
)
For more advanced use cases, you can add custom headers and set reply-to addresses:
# Email with custom headers and reply-to
send_email_with_headers = SesEmailOperator(
task_id="send_email_with_headers",
mail_from=verified_email,
to=[verified_email],
subject="Test Email with Custom Headers",
html_content="<h1>Hello</h1><p>This email has custom headers.</p>",
reply_to=verified_email,
return_path=verified_email,
custom_headers={"X-Custom-Header": "CustomValue"},
aws_conn_id="aws_default",
)
The operator also supports Jinja templating for dynamic content:
# Email with template variables
send_templated_email = SesEmailOperator(
task_id="send_templated_email",
mail_from=verified_email,
to=[verified_email],
subject="DAG Run: {{ dag.dag_id }} - {{ ds }}",
html_content="""
<h1>DAG Run Report</h1>
<p>DAG ID: {{ dag.dag_id }}</p>
<p>Execution Date: {{ ds }}</p>
<p>Run ID: {{ run_id }}</p>
""",
aws_conn_id="aws_default",
)