5

Unable to receive email on task failure or even using EmailOperator

Hi Guys,

I am unable to receive email from my box even after adding required parameters to send one.

Below is how my default args looks like --

default_args = {
    'owner': 'phonrao',
    'depends_on_past': False,
    #'start_date': datetime(2019, 3, 28),
    'start_date': airflow.utils.dates.days_ago(2),
    'email': ['phonrao@gmail.com'],
    'email_on_failure': True,
    'email_on_retry': True,
    'retries': 1,
    'retry_delay': timedelta(minutes=5),
    #'on_failure_callback': report_failure,
    #'end_date': datetime(2020,4 ,1),
    #'schedule_interval': '@hourly',
}

I have few HttpsOperator task in between -- those are working good and are a success, but they do not send email on error(I purposely tried to introduce an error to check if they send any email). Below is an example of my task.

t1 = SimpleHttpOperator(
                   task_id='t1',
                   http_conn_id='http_waterfall',
                   endpoint='/update_data',
                   method='POST',
                   headers={"Content-Type":"application/json"},
                                   xcom_push=True,
                                   log_response=True,
                   dag=dag,
)

and this is my EmailOperator task

t2 = EmailOperator(
              dag=dag,
                  task_id="send_email",
                  to='phonrao@gmail.com',
                          subject='Success',
                      html_content="<h3>Success</h3>" 
)

t2 >> t1 

Below is the error from Logs:

[2019-04-02 15:28:21,305] {{base_task_runner.py:101}} INFO - Job 845: Subtask send_email [2019-04-02 15:28:21,305] {{cli.py:520}} INFO - Running <TaskInstance: schedulerDAG.send_email 2019-04-02T15:23:08.896589+00:00 [running]> on host a47cd79aa987
[2019-04-02 15:28:21,343] {{logging_mixin.py:95}} INFO - [2019-04-02 15:28:21,343] {{configuration.py:255}} WARNING - section/key [smtp/smtp_user] not found in config
[2019-04-02 15:28:21,343] {{models.py:1788}} ERROR - [Errno 99] Cannot assign requested address
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/airflow/models.py", line 1657, in _run_raw_task
    result = task_copy.execute(context=context)
  File "/usr/local/lib/python3.6/site-packages/airflow/operators/email_operator.py", line 78, in execute
    mime_subtype=self.mime_subtype, mime_charset=self.mime_charset)
  File "/usr/local/lib/python3.6/site-packages/airflow/utils/email.py", line 55, in send_email
    mime_subtype=mime_subtype, mime_charset=mime_charset, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/airflow/utils/email.py", line 101, in send_email_smtp
    send_MIME_email(smtp_mail_from, recipients, msg, dryrun)
  File "/usr/local/lib/python3.6/site-packages/airflow/utils/email.py", line 121, in send_MIME_email
    s = smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT) if SMTP_SSL else smtplib.SMTP(SMTP_HOST, SMTP_PORT)
  File "/usr/local/lib/python3.6/smtplib.py", line 251, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python3.6/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/lib/python3.6/smtplib.py", line 307, in _get_socket
    self.source_address)
  File "/usr/local/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/local/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
[2019-04-02 15:28:21,351] {{models.py:1817}} INFO - All retries failed; marking task as FAILED

Below is my airflow.cfg

 [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 = localhost
smtp_starttls = True
smtp_ssl = False
# Uncomment and set the user/pass settings if you want to use SMTP AUTH
# smtp_user = airflow
# smtp_password = airflow
smtp_port = 25
smtp_mail_from = airflow@example.com

Has anyone encounter this issue and any suggestions on how do I resolve this?

Pratik Honrao
  • 113
  • 1
  • 8
  • 1
    Have you configured airflow to send emails? https://stackoverflow.com/questions/51829200/how-to-set-up-airflow-send-email – Robert Moskal Apr 03 '19 at 11:40
  • What exactly do u mean by configuring, could you please elaborate more This is my airflow.cfg ``` [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 = localhost smtp_starttls = True smtp_ssl = False # Uncomment and set the user/pass settings if you want to use SMTP AUTH # smtp_user = airflow # smtp_password = airflow smtp_port = 25 smtp_mail_from = airflow@example.com``` – Pratik Honrao Apr 03 '19 at 14:12
  • Could you post that in the body of your question? – Robert Moskal Apr 03 '19 at 14:41
  • it gives me a warning `WARNING - section/key [smtp/smtp_user] not found in config` and then an error `{{models.py:1788}} ERROR - [Errno 99] Cannot assign requested address` If it is configuration issue, could you tell me what else do I have to do? – Pratik Honrao Apr 03 '19 at 14:47
  • 1
    Possible duplicate of [How to set up Airflow Send Email?](https://stackoverflow.com/questions/51829200/how-to-set-up-airflow-send-email) That should give you everything you need to configure airflow to use email. BTW, do you have an smtp host set up? – Robert Moskal Apr 03 '19 at 14:52

1 Answers1

1

If your airflow running on Kubernetes (installed by helm chart), you should take a look in "airflow-worker-0" pod, and make sure the environment variable of SMTP_HOST or SMTP_USER ... available in the config. Simply debugging, access to the container of airflow-worker and then run python, then trying these commands to make sure it works correctly.

import airflow 
airflow.utils.email.send_email('example@gmail.com', 'Airflow TEST HERE', 'This is airflow status success')

I have the same issues, by resolving the environment variable of SMTP. Now it works.

Vinh Trieu
  • 993
  • 8
  • 12
  • What it means "resolving the environment variable of SMTP"? Can you write the steps for resolving it? – Eric Gabriel Bellet Locker Oct 04 '21 at 07:01
  • 1
    I mean when you deploy your Airflow complete, you should add variable environment of SMTP server in Airflow config. Then checking the pod worker of airflow that exists the value which you has set. – Vinh Trieu Oct 06 '21 at 07:52