如何设置Airflow发送邮件?

新手上路,请多包涵

我按照在线教程在 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

我假设电子邮件操作员会在其他两个操作员之后运行,然后给我发一封电子邮件。但是电子邮件没有发送给我。非常感谢你的帮助。非常感谢你。

最好

原文由 Peter Cui 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.5k
2 个回答

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

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

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

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

  1. 访问您的 应用程序密码 页面。系统可能会要求您登录您的 Google 帐户。
  2. 在底部,点击 选择应用 并选择您正在使用的应用。
  3. 单击 选择设备 并选择您正在使用的设备。
  4. 选择 生成
  5. 按照说明在您的设备上输入 App 密码(黄色栏中的 16 位字符代码)。
  6. 选择 完成

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

编辑 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 = 上面生成的App密码

原文由 kaxil 发布,翻译遵循 CC BY-SA 4.0 许可协议

我有同样的问题,我通过确保我在我的撰写文件中进行卷安装来解决它

volumes:
  - ./dags:/usr/local/airflow/dags
  - ./config/airflow.cfg:/usr/local/airflow/airflow.cfg

原文由 Soumil Nitin Shah 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏