Apache Airflow

Apache Airflowarrow-up-right 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:

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-mailtrap

Step 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:

  1. In the Airflow UI, navigate to AdminConnections.

  2. Click Add a new record (+).

  3. Fill in the connection details:

  4. Click Save.

circle-info

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:

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:

circle-info

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 Logsarrow-up-right.

For more use cases, code examples, and extras, please visit the official GitHub repositoryarrow-up-right.

Use cases

  • Workflow notifications and alerts

  • Scheduled reports and data exports

  • Any transactional or automated emails triggered from Airflow

Next steps

Last updated

Was this helpful?