如何设置气流发送电子邮件? [英] How to set up Airflow Send Email?

查看:222
本文介绍了如何设置气流发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了在线教程,在airflow.cfg中设置电子邮件SMTP服务器,如下所示:

I followed online tutorial to set up Email SMTP server in airflow.cfg as below:

[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

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

I assumed the email operator will run after the other two operators and then send me an email. But email was not sent to me. I really appreciate your help. Thank you very much.

最好

推荐答案

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

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

Create an email id from which you want to send alerts about DAG failure or if you want to use EmailOperator. Edit airflow.cfg file to edit the smtp details for the mail server.

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

For demo you can use any gmail account.

为您的gmail帐户创建一个Google App密码。[此处的说明]不要使用您的原始密码或2因子身份验证。

Create a google App Password for your gmail account. [Instruction here] This is done so that you don't use your original password or 2 Factor authentication.


  1. 访问您的应用密码页面。可能会要求您登录
    Google帐户。

  2. 在底部,点击选择应用,然后选择您正在使用的应用

  3. 点击选择设备,然后选择您要使用
    的设备。

  4. 选择生成

  5. 按照说明在设备上输入应用程序
    密码(黄色栏中的16个字符)。

  6. 选择完成

  1. Visit your App passwords page. You may be asked to sign in to your Google Account.
  2. At the bottom, click Select app and choose the app you’re using.
  3. Click Select device and choose the device you’re using.
  4. Select Generate.
  5. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
  6. Select Done.

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

Once you are finished, you won’t see that App password code again. However, you will see a list of apps and devices you’ve created App passwords for.

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

Edit airflow.cfg and edit the [smtp] section as shown below:

[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

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

Edit the below parameters to the corresponding values:

YOUR_EMAIL_ADDRESS =您的Gmail地址

16_DIGIT_APP_PASSWORD =该应用程序上面生成的密码

YOUR_EMAIL_ADDRESS = Your Gmail address
16_DIGIT_APP_PASSWORD = The App password generated above

这篇关于如何设置气流发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆