我遵循了在线教程,如下在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
我假设电子邮件操作员将在其他两个操作员之后运行,然后向我发送电子邮件。 但是电子邮件没有发送给我。 非常感谢您的帮助。非常感谢。
最佳
答案 0 :(得分:9)
为使用Gmail设置Airflow电子邮件警报的SMTP服务器:
创建一个电子邮件ID,您可以从中发送有关DAG失败或是否要使用 EmailOperator 的警报。编辑airflow.cfg
文件以编辑邮件服务器的smtp详细信息。
对于演示,您可以使用任何Gmail帐户。
为您的Gmail帐户创建一个Google App密码。 [此处的说明]这样做是为了避免使用原始密码或2因子身份验证。
完成后,您将不会再看到该应用的密码。但是,您将看到为其创建应用密码的应用和设备的列表。
编辑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
=上面生成的应用密码
答案 1 :(得分:2)
我遇到了同样的问题,我通过确保在我的撰写文件中进行卷挂载来解决它
volumes:
- ./dags:/usr/local/airflow/dags
- ./config/airflow.cfg:/usr/local/airflow/airflow.cfg
答案 2 :(得分:0)
我也遇到过这个问题。我用正确的设置更改了airflow.cfg,但它仍然无法正常工作。最后发现应该重启airflow scheduler来加载airflow.cfg的变化。重新启动调度程序后,它工作正常。 下面两个设置都可以。
[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
smtp_user = youremail@gmail.com
smtp_password = your password
smtp_port = 587
smtp_mail_from = youremail@gmail.com
---------或------------------
[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 = False
smtp_ssl = True
smtp_user = youremail@gmail.com
smtp_password = your password
smtp_port = 465
smtp_mail_from = youremail@gmail.com