如何设置Airflow发送电子邮件?

Pet*_*Cui 14 python smtp airflow

我按照在线教程在airflow.cfg中设置了Email 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
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

我假设电子邮件运营商将在其他两个运营商之后运行,然后向我发送电子邮件.但是电子邮件没有发给我.我非常感谢你的帮助.非常感谢你.

最好

kax*_*xil 22

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

创建一个电子邮件ID,您要从中发送有关DAG失败的警报或是否要使用EmailOperator.编辑airflow.cfg文件以编辑邮件服务器的smtp详细信息.

对于演示,您可以使用任何Gmail帐户.

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

  1. 访问您的应用密码页面.系统可能会要求您登录Google帐户.
  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
Run Code Online (Sandbox Code Playgroud)

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

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

  • 这似乎对很多人有用,但对我不起作用。我没有收到任何电子邮件,我有确切的配置。 (2认同)