Python 如何设置 Airflow 发送电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51829200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set up Airflow Send Email?
提问by Peter Cui
I followed online tutorial to set up Email SMTP server in airflow.cfg as below:
我按照在线教程在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 = [email protected]
And my DAG is as below:
我的 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='[email protected]',
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.
我假设电子邮件操作员会在其他两个操作员之后运行,然后给我发送电子邮件。但是没有给我发邮件。我真的很感谢你的帮助。非常感谢。
Best
最好的事物
回答by kaxil
Setting up SMTP Server for Airflow Email alerts using Gmail:
使用 Gmail 为 Airflow 电子邮件警报设置 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.
创建一个电子邮件 id,您想从中发送有关 DAG 失败的警报,或者如果您想使用EmailOperator。编辑airflow.cfg
文件以编辑邮件服务器的 smtp 详细信息。
For demo you can use any gmail account.
对于演示,您可以使用任何 Gmail 帐户。
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.
为您的 gmail 帐户创建一个谷歌应用密码。[此处的说明] 这样做是为了让您不使用原始密码或 2 因素身份验证。
- Visit your App passwordspage. You may be asked to sign in to your Google Account.
- At the bottom, click Select appand choose the app you're using.
- Click Select deviceand choose the device you're using.
- Select Generate.
- Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
- Select Done.
- 访问您的应用密码页面。系统可能会要求您登录自己的 Google 帐户。
- 在底部,单击选择应用程序并选择您正在使用的应用程序。
- 单击选择设备并选择您正在使用的设备。
- 选择生成。
- 按照说明在您的设备上输入应用程序密码(黄色栏中的 16 个字符代码)。
- 选择完成。
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.
完成后,您将不会再看到该应用密码代码。但是,您将看到已为其创建应用密码的应用和设备列表。
Edit airflow.cfg
and edit the [smtp]
section as shown below:
编辑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
Edit the below parameters to the corresponding values:
将以下参数编辑为相应的值:
YOUR_EMAIL_ADDRESS
= Your Gmail address16_DIGIT_APP_PASSWORD
= The App password generated above
YOUR_EMAIL_ADDRESS
= 您的 Gmail 地址16_DIGIT_APP_PASSWORD
= 上面生成的应用密码