如何设置气流发送电子邮件?

发布于 2021-01-29 15:16:09

我遵循了在线教程,在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
被浏览
175
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    使用Gmail设置SMTP服务器以发送气流电子邮件警报

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

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

    为您的Gmail帐户创建一个Google App密码。[此处的说明]这样做是为了避免使用原始密码或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
    

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

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看