Apache Airflow
Apache Airflow is an open-source platform that allows you to programmatically author, schedule, and monitor workflows. In this guide, you’ll learn how to connect it to Mailtrap, so you can send transactional emails (e.g., notifications, reports, alerts, and other workflow-triggered messages) directly from your Airflow DAGs.
Requirements:
Python 3.9 or higher
Apache Airflow 2.4 or higher
A Mailtrap API Token with admin access level to that domain.
How does Apache Airflow work?
Apache Airflow is a workflow automation platform. You define workflows as Python code, and Airflow executes them on a schedule or on demand.
In Airflow, a workflow is called a DAG (Directed Acyclic Graph). A DAG is a series of steps that run in sequence. Each step is a task, and tasks are created using operators. Think of an operator as a declaration of a step: you define what it should do, and Airflow runs it when the time comes.
For example, a practical DAG might fetch data from an API, process it, and then send an email with the results, all automated.
Step 1. Install the provider package
In the environment that is hosting your Airflow instance, install the Mailtrap Airflow provider using pip:
pip install airflow-provider-mailtrapStep 2. Create a Mailtrap connection
Connections let Airflow communicate with external services. You'll create a Mailtrap connection once, and then reuse it across all your workflows. Here’s how it works:
In the Airflow UI, navigate to Admin → Connections.
Click Add a new record (+).
Fill in the connection details:
Click Save.

The Extra field is optional. You can use it to set a default sender address.
Step 3. Add an operator to your DAG
Now, you need to add a Mailtrap operator to your DAG. An operator is like a function call that declares a step: you define what it should do, and Airflow executes it when the workflow runs.
Mailtrap uses the MailtrapSendEmailOperator operator.
Then, in your DAG source code, copy/paste the Mailtrap operator with the parameters based on your use case:
All operator parameters support Jinja templating. You can use Airflow variables, XCom values, and macros to insert dynamic content:
Once you save the file, Airflow will automatically detect and load the DAG, and you’ll be able to see it in your UI. For instance:

Once setup is complete, you can use the Mailtrap operator in any workflow since the connection configuration is a one-time step.
Step 4. Run your DAG
Finally, try triggering your DAG manually to test the automation. If you followed everything correctly, the operator will send the email via the Mailtrap API and log the result.

And here is the email in the to address:

Additionally, you’ll be able to see the email in the Mailtrap Email Logs.
For more use cases, code examples, and extras, please visit the official GitHub repository.
Use cases
Workflow notifications and alerts
Scheduled reports and data exports
Any transactional or automated emails triggered from Airflow
Next steps
Use Mailtrap Templates to send branded emails with variables.
Track open and click rates with Mailtrap Analytics.
Last updated
Was this helpful?

