如何设置气流发送电子邮件?

2024-04-19 20:21:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我按照联机教程在airflow.cfg中设置电子邮件SMTP服务器,如下所示:

[email]
email_backend = airflow.utils.email.send_email_smtp


[smtp]
# If you want airflow to send emails on retries, failure, and you want to use
# the airflow.utils.email.send_email_smtp function, you have to configure an
# smtp server here
smtp_host = smtp.gmail.com
smtp_starttls = True
smtp_ssl = False
# Uncomment and set the user/pass settings if you want to use SMTP AUTH 
# smtp_user =                       
# smtp_password =  
smtp_port = 587
smtp_mail_from = myemail@gmail.com

我的DAG如下:

from datetime import datetime
from airflow import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.python_operator import PythonOperator
from airflow.operators.email_operator import EmailOperator

def print_hello():
    return 'Hello world!'

default_args = {
        'owner': 'peter',
        'start_date':datetime(2018,8,11),
}

dag = DAG('hello_world', description='Simple tutorial DAG',
          schedule_interval='* * * * *',
          default_args = default_args, catchup=False)

dummy_operator = DummyOperator(task_id='dummy_task', retries=3, dag=dag)

hello_operator = PythonOperator(task_id='hello_task', python_callable=print_hello, dag=dag)

email = EmailOperator(
        task_id='send_email',
        to='to@gmail.com',
        subject='Airflow Alert',
        html_content=""" <h3>Email Test</h3> """,
        dag=dag
)

email >> dummy_operator >> hello_operator

我以为电子邮件接线员会跟在其他两个接线员后面,然后给我发一封电子邮件。 但邮件没有发给我。 我真的很感谢你的帮助。非常感谢你。

最好的


Tags: tofromimportyousendhellotaskemail
1条回答
网友
1楼 · 发布于 2024-04-19 20:21:48

使用Gmail为气流电子邮件警报设置SMTP服务器:

创建一个电子邮件id,从中可以发送有关DAG故障的警报,或者如果要使用电子邮件操作员。编辑airflow.cfg文件以编辑邮件服务器的smtp详细信息。

对于演示,您可以使用任何gmail帐户。

为你的gmail帐户创建一个google应用程序密码。[此处说明]这样做是为了您不使用原始密码或2因素身份验证。

  1. 访问您的App passwords页面。可能会要求您登录到 谷歌账户。
  2. 在底部,单击“选择应用程序”并选择应用程序 你在用。
  3. 单击“选择设备”,然后选择您的设备 使用。
  4. 选择“生成”。
  5. 按照说明进入应用程序 设备上的密码(黄色条中的16个字符代码)。
  6. 选择“完成”。

完成后,您将不会再看到该应用程序密码。但是,您将看到已为其创建应用程序密码的应用程序和设备的列表。

编辑airflow.cfg并编辑[smtp]部分,如下所示:

[smtp]
smtp_host = smtp.gmail.com
smtp_starttls = True
smtp_ssl = False
smtp_user = YOUR_EMAIL_ADDRESS
smtp_password = 16_DIGIT_APP_PASSWORD
smtp_port = 587
smtp_mail_from = YOUR_EMAIL_ADDRESS

将以下参数编辑为相应的值:

YOUR_EMAIL_ADDRESS=您的Gmail地址
16_DIGIT_APP_PASSWORD=上面生成的应用程序密码

相关问题 更多 >