[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Apache Airflow™](../index.md) > [Tutorials](index.md) > Other tutorials > Configuring an SMTP server to send e-mail notifications

# Configuring an SMTP server to send e-mail notifications

# Configuring an SMTP server to send e-mail notifications

{% note warning %}

This tutorial was tested on clusters with the Apache Airflow™ version below 3.0.

{% endnote %}

Use a [directed acyclic graph (DAG)](../concepts/index.md#about-the-service) to configure your SMTP server to send e-mail notifications. Data for connecting to the DB is stored in Yandex Lockbox and automatically inserted into the graph.

To configure an SMTP server to send e-mail notifications, follow these steps:

1. [Set up your infrastructure](#create-infrastracture).
1. [Create a Yandex Lockbox secret](#create-lockbox-secret).
1. [Prepare the DAG file and run the graph](#dag).
1. [Check the result](#check-result).

If you no longer need the resources you created, [delete them](#clear-out).


## Getting started {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).

### Required paid resources {#paid-resources}

* Managed Service for Apache Airflow™ cluster: computing resources of cluster components (see [Managed Service for Apache Airflow™ pricing](../pricing.md)).
* Yandex Object Storage bucket: use of storage, data operations (see [Object Storage pricing](../../storage/pricing.md)).
* Yandex Lockbox secret: number of stored secret versions and requests to them (see [Yandex Lockbox pricing](../../lockbox/pricing.md)).


## Set up your infrastructure {#create-infrastracture}

1. [Create a service account](../../iam/operations/sa/create.md#create-sa) named `airflow-sa` with the following roles:
   * `managed-airflow.integrationProvider`
   * `lockbox.payloadViewer`

   {% note info }

   There is no need to assign the `lockbox.payloadViewer` role for the whole folder. It is enough to [assign it for a specific Yandex Lockbox secret](../../lockbox/operations/secret-access.md) once you create it.

   {% endnote %}

1. [Create an Object Storage bucket](../../storage/operations/buckets/create.md) in any configuration.
1. [Edit the ACL](../../storage/operations/buckets/edit-acl.md) of the new bucket to give the `READ` permission to the `airflow-sa` service account.
1. [Create a Managed Service for Apache Airflow™ cluster](../operations/cluster-create.md#create-cluster) with the following parameters:
   * **Service account**: `airflow-sa`.
   * **Bucket name**: Name of the new bucket.
   * **Use Lockbox Secret Backend**: Make sure to enable this option.

   Under **Airflow configuration**, set up [additional Apache Airflow™ properties](https://airflow.apache.org/docs/apache-airflow/2.2.4/configurations-ref.html) to configure the SMTP server:

   | Key | Value |
   | ----- | ----- |
   | `smtp.smtp_host` | `smtp.yandex.ru` |
   | `smtp.smtp_port` | `465` |
   | `smtp.smtp_starttls` | `False` |
   | `smtp.smtp_ssl` | `True` |
   | `smtp.smtp_mail_from` | `user@yandex.ru` |

   Fill in the fields manually or import the settings from a configuration file (see a [configuration file example](https://storage.yandexcloud.net/doc-files/managed-airflow/airflow.cfg)).

## Create a Yandex Lockbox secret {#create-lockbox-secret}

[Create a Yandex Lockbox secret](../../lockbox/operations/secret-create.md) named `airflow/connections/smtp_default`, containing `json.dumps(conn)`, where `conn` is a connection to an SMTP server.

```json
conn = {
         "conn_type": "smtp",
         "login": "user@yandex.ru",
         "password": "<password_for_external_apps>"
}
```

The `airflow/connections/smtp_default` secret will store the data for connecting to the SMTP server.

## Prepare the DAG file and run the graph {#dag}

1. Create a local file named `test_smtp_notification.py` and paste the following script to it:

   ```python
   from airflow.decorators import dag, task

   default_args = {
    "email": ["user@yandex.ru"],
    "email_on_failure": True,
    "email_on_retry": True
   }


   @dag(schedule=None, default_args=default_args)
   def test_smtp_notification():
       @task
       def failing_task():
           raise RuntimeError("some test error")

       failing_task()


   test_smtp_notification()
   ```

1. Upload the `test_smtp_notification.py` DAG file to the bucket you created earlier. This will automatically create a graph with the same name in the Apache Airflow™ web interface.

1. [Open the Apache Airflow™ web interface](../operations/af-interfaces.md#web-gui).
1. Make sure a new graph named `test_smtp_notification` has appeared in the **DAGs** section.

   It may take a few minutes to upload a DAG file from the bucket.

1. To run the graph, click ![image](../../_assets/managed-airflow/trigger-dag.png =18x) in the line with its name.

## Check the result {#check-result}

To check the result in the Apache Airflow™ web interface:

1. In the **DAGs** section, open the `test_smtp_notification` graph.
1. Go to the **Graph** section.
1. Select **failing_task**.
1. Go to **Logs**.
1. Make sure the logs do not contain the `some test error` line. This means the query was successful.

## Delete the resources you created {#clear-out}

Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them:

1. [Service account](../../iam/operations/sa/delete.md).
1. [Object Storage bucket](../../storage/operations/buckets/delete.md).
1. [Managed Service for Apache Airflow™ cluster](../operations/cluster-delete.md#delete).
1. [Yandex Lockbox secret](../../lockbox/operations/secret-delete.md).