38

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

And my DAG is as below:

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.

Best

Peter Cui
  • 419
  • 1
  • 4
  • 8

3 Answers3

62

Setting up SMTP Server for Airflow Email alerts using Gmail:

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.

For demo you can use any gmail account.

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. Visit your App passwords page. You may be asked to sign in to your Google Account.
  2. Under "Your app passwords", click Select app and choose "Mail".
  3. Click Select device and choose "Other (Custom name)" so that you can input "Airflow".
  4. Select Generate.
  5. Copy the generated App password (the 16 character code in the yellow bar), for example xxxxyyyyxxxxyyyy.
  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 (which you've created App passwords for) in your Google Account.

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 = xxxxyyyyxxxxyyyy
smtp_port = 587
smtp_mail_from = YOUR_EMAIL_ADDRESS

Edit the below parameters to the corresponding values:

YOUR_EMAIL_ADDRESS = Your Gmail address
xxxxyyyyxxxxyyyy = The App password generated above

blackraven
  • 5,284
  • 7
  • 19
  • 45
kaxil
  • 17,706
  • 2
  • 59
  • 78
  • 2
    this does seem to be working for many but is not working for me. I am not getting any emails, i have exact configuration. – saadi May 14 '20 at 10:49
  • do you know how to set this for a Google Group email instead of a personal Gmail account? I want to use Airflow to send emails from a Google Group email address – cryanbhu Sep 04 '20 at 06:37
  • I have found some helpful documentation here if sending from Google G Suite and not Gmail https://support.google.com/a/answer/176600?hl=en – cryanbhu Sep 04 '20 at 06:55
  • 1
    can you use your usual password instead of the `16_DIGIT_PASSWORD` if you dont want to use the app-password? – CutePoison Apr 22 '21 at 13:33
  • This worked for me! I used the 16 digit password generated by app passwords. – Alan Ma Feb 05 '22 at 01:58
3

I had same issue and i solved it by making sure i am doing a volume mount in my compose file

volumes:
  - ./dags:/usr/local/airflow/dags
  - ./config/airflow.cfg:/usr/local/airflow/airflow.cfg
Soumil Nitin Shah
  • 634
  • 2
  • 7
  • 18
1

I also encountered this issue. I changed the airflow.cfg with correct setting, but it's still not working. Finally, I found out that I should restart the airflow scheduler to load the changes of airflow.cfg. After I restarted the scheduler, it worked fine. The following two setting are both ok.

[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

---------or------------------

[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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Lingfeng
  • 11
  • 1