Le 15 septembre 2026, tous les environnements Cloud Composer 1 et Cloud Composer 2 version 2.0.x atteindront leur fin de vie prévue et vous ne pourrez plus les utiliser. Nous vous recommandons de planifier la migration vers Cloud Composer 3.
Cette page explique comment configurer des services SMTP pour votre environnement Cloud Composer.
Avant de commencer
Si votre environnement Cloud Composer est configuré pour une adresse IP privée, assurez-vous qu'il dispose d'une connectivité au point de terminaison de l'API de votre service de messagerie externe (par exemple, https://api.sendgrid.com ou le serveur SMTP de votre choix).
Les composants Airflow dans les environnements à adresse IP publique établissent des connexions sortantes, y compris l'envoi de messages électroniques, à partir d'adresses IP publiques et de ports alloués automatiquement. Si vous souhaitez que vos environnements d'adresses IP publiques envoient des e-mails à partir d'adresses IP et de ports prédéterminés, vous pouvez le faire en connectant un réseau VPC à votre environnement et en passant à une adresse IP privée. Pour en savoir plus sur la mise en réseau dans Cloud Composer 3, consultez Modifier le type de mise en réseau de l'environnement.
Si vous configurez un autre backend d'e-mail personnalisé, cette configuration ne sera pas propagée au planificateur. Par conséquent, l'envoi d'e-mails directement depuis le planificateur entraînerait un blocage et un échec répété de ce dernier.
Les nœuds de calcul et les processeurs de DAG Airflow sont compatibles avec d'autres backends personnalisés. Vous pouvez donc envoyer des e-mails à partir de ces composants. Les alertes par e-mail concernant les échecs de DAG sont un cas courant d'envoi d'e-mails directement depuis le planificateur. Pour contourner ce problème, vous pouvez envoyer des alertes d'échec de DAG via un rappel enregistré. Ils seront envoyés par le processeur DAG. Vous pouvez également utiliser d'autres backends personnalisés avec les opérateurs Airflow qui envoient des e-mails. Dans ce cas, l'e-mail sera envoyé par un nœud de calcul Airflow qui traite la tâche.
Configurer les services de messagerie SendGrid
Pour recevoir des notifications, configurez vos variables d'environnement pour l'envoi d'e-mails via le service de messagerie SendGrid.
S'inscrire à SendGrid
Si vous ne l'avez pas encore fait, inscrivez-vous à SendGrid dans la console Google Cloud , cliquez sur "Gérer sur le fournisseur" pour accéder au domaine SendGrid, puis cliquez sur "Paramètres" pour récupérer votre nom d'utilisateur et créer une clé API. En tant que développeur Google Cloud , vous pouvez commencer avec 12 000 e-mails gratuits par mois.
SendGrid utilise la clé API et une adresse e-mail "De" pour envoyer des e-mails. Vous pouvez fournir ces informations de l'une des manières suivantes :
Configurez Secret Manager pour votre environnement. Veillez à configurer les autorisations et les options de configuration Airflow pour le backend de secrets.
Ignorez les options de configuration Airflow suivantes :
Créez un secret pour la connexion SendGrid nommée airflow-connections-sendgrid_default. Définissez la valeur du code secret sur l'URI de connexion. Exemple :
Configurez l'adresse e-mail pour SendGrid. Il n'est pas possible de configurer l'adresse e-mail à l'aide d'un secret. Remplacez plutôt l'option de configuration Airflow suivante :
Section
Clé
Valeur
email
from_email
Adresse e-mail de l'expéditeur, telle que noreply@example.com.
Stocker des valeurs dans Airflow
Ignorez les options de configuration Airflow suivantes :
Configurez l'adresse e-mail pour SendGrid. Il n'est pas possible de configurer l'adresse e-mail à l'aide d'un secret. Remplacez plutôt l'option de configuration Airflow suivante :
Section
Clé
Valeur
email
from_email
Adresse e-mail de l'expéditeur, telle que noreply@example.com.
Tester votre configuration SendGrid
Pour tester la configuration SendGrid, procédez comme suit :
Créez un DAG de test utilisant EmailOperator. Exemple :
importdatetimeimportairflowfromairflow.operators.emailimportEmailOperatorwithairflow.DAG("composer_sample_sendgrid",start_date=datetime.datetime(2022,1,1),)asdag:task_email=EmailOperator(task_id="send-email",conn_id="sendgrid_default",# You can specify more than one recipient with a list.to="user@example.com",subject="EmailOperator test for SendGrid",html_content="This is a test message sent through SendGrid.",dag=dag,)
Importez le DAG dans votre environnement et assurez-vous que la tâche est bien exécutée.
Connectez-vous à SendGrid à l'aide de vos identifiants SendGrid.
Dans l'interface utilisateur de SendGrid, accédez à la page "Activité".
Cherchez l'e-mail dans la liste. Vous devriez constater que SendGrid a traité et envoyé l'e-mail.
Si l'e-mail n'est pas traité, ni distribué, effectuez les opérations suivantes :
Vérifiez votre configuration SendGrid.
Vérifiez que vous avez activé le backend Secret Manager. Assurez-vous d'avoir accordé des autorisations supplémentaires et défini des remplacements pour les options de configuration Airflow.
Vérifiez le filtre antispam dans votre client de messagerie.
Configurer des services SMTP tiers
Pour envoyer un e-mail via un service SMTP tiers, remplacez l'option de configuration Airflow email_backend et configurez les autres paramètres SMTP.
Pour configurer un service SMTP tiers, remplacez les options de configuration Airflow suivantes :
Section
Clé
Valeur
email
email_backend
airflow.utils.email.send_email_smtp
smtp
smtp_host
Nom d'hôte du serveur SMTP.
smtp
smtp_user
Nom d'utilisateur sur le serveur SMTP.
smtp
smtp_port
Port du serveur SMTP. Le port 25 n'est pas disponible. Vous pouvez utiliser d'autres ports, par exemple les ports SMTP standards 465 et 587.
smtp
smtp_password
La définition d'un mot de passe via smtp_password n'est pas acceptée. Pour définir un mot de passe SMTP, suivez les instructions fournies dans l'article Configurer un mot de passe SMTP.
smtp
smtp_mail_from
Adresse e-mail de l'expéditeur, telle que noreply-composer@.
smtp
smtp_starttls
Pour plus de sécurité, définissez la valeur sur True.
smtp
smtp_ssl
Pour plus de sécurité, définissez la valeur sur True.
Configurer un mot de passe SMTP pour un service SMTP tiers
Conserver un mot de passe SMTP en texte brut dans le fichier de configuration Airflow constitue une mauvaise pratique de sécurité. C'est pourquoi Cloud Composer n'est pas compatible avec cette méthode. Il est possible d'utiliser deux autres méthodes pour configurer un mot de passe SMTP.
Utiliser une commande pour récupérer le mot de passe SMTP
Vous pouvez utiliser un remplacement de configuration pour spécifier une commande qui obtient le mot de passe SMTP. Lors de la communication avec votre service SMTP, Airflow s'en sert pour obtenir la valeur du mot de passe. Spécifiez une seule commande, sans opérateurs Bash tels que les canaux et les redirections.
Pour utiliser cette méthode, remplacez l'option de configuration Airflow suivante :
Section
Clé
Valeur
smtp
smtp_password_cmd
Spécifiez une commande qui renvoie le mot de passe SMTP.
Utiliser un secret stocké dans Secret Manager pour récupérer un mot de passe SMTP
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/01 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/09/01 (UTC)."],[[["\u003cp\u003eThis guide provides instructions on configuring SMTP services for Cloud Composer 3 environments, including using SendGrid and third-party SMTP services.\u003c/p\u003e\n"],["\u003cp\u003eFor SendGrid, users can configure email sending by storing the API key and connection details either in Secret Manager, which is recommended, or directly within Airflow, though the latter is less secure.\u003c/p\u003e\n"],["\u003cp\u003ePublic IP environments can send emails from predetermined IP addresses by connecting a VPC network and switching to Private IP.\u003c/p\u003e\n"],["\u003cp\u003eThird-party SMTP services require overriding specific Airflow configuration options, and Cloud Composer supports retrieving the SMTP password securely through either a command or from Secret Manager.\u003c/p\u003e\n"],["\u003cp\u003eTesting the configuration can be done by creating a DAG that utilizes the EmailOperator, and checking if it was successful, along with checking the activity page on the specified email server.\u003c/p\u003e\n"]]],[],null,["# Configure email notifications\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\n**Cloud Composer 3** \\| [Cloud Composer 2](/composer/docs/composer-2/configure-email \"View this page for Cloud Composer 2\") \\| [Cloud Composer 1](/composer/docs/composer-1/configure-email \"View this page for Cloud Composer 1\")\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nThis page explains how to configure SMTP services for your\nCloud Composer environment.\n\nBefore you begin\n----------------\n\n- If your Cloud Composer environment is\n [configured for Private IP](/composer/docs/composer-3/configure-private-ip), make sure that it has\n connectivity to the API endpoint for your external email service (such as\n `https://api.sendgrid.com`, or your preferred SMTP server).\n\n- Airflow components in Public IP environments establish outbound connections,\n including sending email messages, from automatically allocated public IP\n addresses and ports. If you want your Public IP environments to send email\n from predetermined IP addresses and ports, you can do so through\n [connecting a VPC network](/composer/docs/composer-3/connect-vpc-network) to your environment and\n [switching it to Private IP](/composer/docs/composer-3/change-networking-type#about-networking-type). For more\n information about networking in Cloud Composer 3, see\n [Change environment networking type](/composer/docs/composer-3/change-networking-type#about-networking-type).\n\n- The Airflow scheduler in Cloud Composer 3 supports only the following\n [custom email backends](#third-party):\n\n - airflow.providers.sendgrid.utils.emailer.send_email\n - airflow.utils.email.send_email_smtp\n\n If you configure a different custom email backend, this configuration won't\n be propagated to the scheduler. As a result, sending email directly from the\n scheduler would lead to the scheduler becoming unresponsive and repeatedly\n failing.\n\n Airflow workers and DAG processors support other custom backends, so you\n can send email from these components. One common case when email is sent\n directly from the scheduler is email alerts about DAG failures. As a\n workaround, you can send DAG failure alerts\n [through a registered callback](https://airflow.apache.org/docs/apache-airflow/stable/administration-and-deployment/logging-monitoring/callbacks.html) instead. They will be\n sent from the DAG processor. You can also use other custom backends with\n Airflow operators that send email. In this case, email will be sent from\n an Airflow worker that processes the task.\n\nConfigure SendGrid email services\n---------------------------------\n\nTo receive notifications, configure your environment variables\nto send email through the SendGrid email service.\n\n### Sign up with SendGrid\n\nIf you haven't already, sign up with SendGrid in Google Cloud console, click\nManage On Provider to go to the SendGrid domain, and click Settings to retrieve\nyour username and to create an API key. As a Google Cloud developer, you can\nstart with 12,000 free emails per month.\n\n[Go to SendGrid Email API](https://console.cloud.google.com/marketplace/details/sendgrid-app/sendgrid-email)\n\n### Configure variables\n\nSendGrid uses the API key and a From email address for sending mail. You\ncan provide this information with one of the following options:\n\n- (Recommended)\n [Store values in Secret Manager](#store-secret-manager)\n\n- [Store values in Airflow](#store-airflow)\n\n | **Caution:** If you use this option, the value for the SendGrid API key is stored in an Airflow connection.\n\n#### Store values in Secret Manager\n\nTo store values in Secret Manager:\n\n1. [Configure Secret Manager](/composer/docs/composer-3/configure-secret-manager) for your\n environment. Make sure to set up permissions and Airflow configuration\n options for the secrets backend.\n\n2. [Override](/composer/docs/composer-3/override-airflow-configurations) the following Airflow configuration options:\n\n3. Create a secret for the SendGrid connection named\n `airflow-connections-sendgrid_default`. Set the secret's value to the\n connection URI. For example:\n\n sendgrid://\u003cusername\u003e:\u003csendgrid_api_key\u003e@smtp.sendgrid.net:587\n\n For more information about storing connections in\n Secret Manager, see\n [Manage Airflow connections](/composer/docs/composer-3/manage-airflow-connections).\n4. Configure the email address for SendGrid. It's not possible to set up the\n email address through a secret. Instead, [override](/composer/docs/composer-3/override-airflow-configurations) the\n following Airflow configuration option:\n\n#### Store values in Airflow\n\n1. [Override](/composer/docs/composer-3/override-airflow-configurations) the following Airflow configuration options:\n\n2. In Airflow, [configure the connection](/composer/docs/composer-3/manage-airflow-connections)\n named `sendgrid_default`. Specify the Sendgrid API key in the connection\n URI. For example:\n\n gcloud composer environments run \u003cvar translate=\"no\"\u003eENVIRONMENT_NAME\u003c/var\u003e \\\n --location \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e \\\n connections add -- \\\n --conn-uri \"sendgrid://\u003cvar translate=\"no\"\u003eUSERNAME\u003c/var\u003e:\u003cvar translate=\"no\"\u003eSENDGRID_API_KEY\u003c/var\u003e@smtp.sendgrid.net:587\" \\\n sendgrid_default\n\n Replace the following:\n - `ENVIRONMENT_NAME`: the name of your environment.\n - `LOCATION`: the region where the environment is located.\n - `USERNAME`: the SendGrid username.\n - `SENDGRID_API_KEY`: the SendGrid API key.\n3. Configure the email address for SendGrid. It's not possible to set up the\n email address through a secret. Instead, [override](/composer/docs/composer-3/override-airflow-configurations) the\n following Airflow configuration option:\n\n### Test your SendGrid configuration\n\nTo test SendGrid configuration:\n\n1. Create a test DAG that uses the `EmailOperator`. For example:\n\n\n import datetime\n\n import airflow\n from airflow.operators.email import EmailOperator\n\n\n with airflow.DAG(\n \"composer_sample_sendgrid\",\n start_date=datetime.datetime(2022, 1, 1),\n ) as dag:\n task_email = EmailOperator(\n task_id=\"send-email\",\n conn_id=\"sendgrid_default\",\n # You can specify more than one recipient with a list.\n to=\"user@example.com\",\n subject=\"EmailOperator test for SendGrid\",\n html_content=\"This is a test message sent through SendGrid.\",\n dag=dag,\n )\n\n1. [Upload the DAG](/composer/docs/composer-3/manage-dags#add) to your environment and check that the task succeeds.\n2. Sign in to SendGrid in with your SendGrid credentials.\n3. In the SendGrid UI, go to Activity page.\n4. Search the list for the email. You should see that SendGrid processed and delivered the email.\n5. If the email is not processed and delivered:\n\n - Check your SendGrid configuration.\n - Verify that you enabled the Secret Manager backend. Make sure that you granted extra permissions and set overrides for Airflow configuration options.\n - Check the spam filter in your email client.\n\nConfigure third-party SMTP services\n-----------------------------------\n\nTo send email through a third-party SMTP service,\n[override](/composer/docs/composer-3/override-airflow-configurations) the `email_backend` Airflow configuration option\nand configure other SMTP-related parameters.\n| **Note:** Using a third-party SMTP service requires setting an SMTP password. Cloud Composer does not support configuring the `smtp_password` because the value is stored in plain text in `airflow.cfg` and considered non-secure. See [Configuring an SMTP password](#smtp_password) to learn about setting an SMTP password in a more secure way.\n\nTo configure a third-party SMTP service, [override](/composer/docs/composer-3/override-airflow-configurations) the following Airflow configuration options:\n\n### Configure an SMTP password for a third-party SMTP service\n\nKeeping an SMTP password in plain text in Airflow configuration file is a bad\nsecurity practice. That's why Cloud Composer does not support\nthis method. Instead, you can use two other methods for configuring an SMTP\npassword.\n\n#### Using a command to retrieve the SMTP password\n\nYou can use a configuration override to specify a command that obtains the\nSMTP password. When communicating with your SMTP service, Airflow uses this\ncommand to get the value of the password. Specify a single command, without\nbash operators such as pipes and redirects.\n\nTo use this method, [override](/composer/docs/composer-3/override-airflow-configurations) the following Airflow\nconfiguration option:\n\n\n#### Using a secret stored in Secret Manager to retrieve an SMTP password\n\nYou can [configure Secret Manager](/composer/docs/composer-3/configure-secret-manager) as your\nAirflow secrets backend.\n| **Note:** If the `smtp_ssl` Airflow configuration option is set to `True` for your environment, then disable this override if you get the `SSL_WRONG_VERSION_NUMBER` error.\n\nOnce you configure Secret Manager for your Composer\nenvironment, you can store an SMTP password in Secret Manager:\n\n1. Create a new secret:\n\n echo -n \"\u003cvar translate=\"no\"\u003eSMTP_PASSWORD\u003c/var\u003e\" | gcloud beta secrets create \\\n airflow-config-smtp-password \\\n --data-file=- \\\n --replication-policy=automatic\n\n Replace `SMTP_PASSWORD` with your SMTP password.\n2. Configure Airflow to obtain the SMTP password from\n Secret Manager. To do so, [override](/composer/docs/composer-3/override-airflow-configurations)\n the following Airflow configuration option:\n\nWhat's next\n-----------\n\n- [Override Airflow configuration options](/composer/docs/composer-3/override-airflow-configurations)\n- [Set environment variables](/composer/docs/composer-3/set-environment-variables)"]]