I am struggling to connect my Gmail account to Airflow. I've followed ChatGPT suggestions and searched in this site for solutions but i haven't crossed upon one that addresses my context, and I don't know what else ask to chat gpt.
I am working in Ubuntu 22.04.4 LTS jammy. I installed Airflow with docker following the instructions in this video https://www.youtube.com/watch?v=aTaytcxy2Ck&t=335s. I can open the Airflow UI. I already generated App Password through Google, which I pasted in the Admin connections section at the Airflow UI. Specifically i have the following configuration:
Conn Id = smtp_gmail_connConn Type = emailHost = smtp.gmail.comPort = 587Login = my email which I dont show herePassword = the app password google gave me Is Encrypted = FalseIs Extra Enctrypted = False
Earlier on, chat-gpt suggested adding to the yml file the following code in the airflow-webserver part:
<<: *airflow-common-env AIRFLOW__SMTP__SMTP_HOST: smtp.gmail.com AIRFLOW__SMTP__SMTP_PORT: 587 AIRFLOW__SMTP__SMTP_USER: your_email@gmail.com AIRFLOW__SMTP__SMTP_PASSWORD: your_app_password AIRFLOW__SMTP__SMTP_STARTTLS: "True" AIRFLOW__SMTP__SMTP_SSL: "False"
Yet it did not work, so I deleted those variables from the YAML file.
This is an extract of my task's log file. The error is shown at the bottom:
[2024-03-13, 17:26:37 UTC] {task_command.py:423} INFO - Running <TaskInstance: example_email_dag.send_email_task manual__2024-03-13T17:19:27.422265+00:00 [running]> on host bce2c982946e[2024-03-13, 17:26:38 UTC] {taskinstance.py:2510} INFO - Exporting env vars: AIRFLOW_CTX_DAG_OWNER='my_owner' AIRFLOW_CTX_DAG_ID='example_email_dag' AIRFLOW_CTX_TASK_ID='send_email_task' AIRFLOW_CTX_EXECUTION_DATE='2024-03-13T17:19:27.422265+00:00' AIRFLOW_CTX_TRY_NUMBER='3' AIRFLOW_CTX_DAG_RUN_ID='manual__2024-03-13T17:19:27.422265+00:00'[2024-03-13, 17:26:38 UTC] {base.py:83} INFO - Using connection ID 'smtp_gmail_conn' for task execution.[2024-03-13, 17:26:38 UTC] {email.py:270} INFO - Email alerting: attempt 1[2024-03-13, 17:26:38 UTC] {taskinstance.py:2728} ERROR - Task failed with exceptionTraceback (most recent call last): File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 444, in _execute_task result = _execute_callable(context=context, **execute_callable_kwargs) File "/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 414, in _execute_callable return execute_callable(context=context, **execute_callable_kwargs) File "/home/airflow/.local/lib/python3.8/site-packages/airflow/operators/email.py", line 79, in execute send_email( File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 80, in send_email return backend( File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 154, in send_email_smtp send_mime_email(e_from=mail_from, e_to=recipients, mime_msg=msg, conn_id=conn_id, dryrun=dryrun) File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 272, in send_mime_email smtp_conn = _get_smtp_connection(smtp_host, smtp_port, smtp_timeout, smtp_ssl) File "/home/airflow/.local/lib/python3.8/site-packages/airflow/utils/email.py", line 316, in _get_smtp_connection return smtplib.SMTP(host=host, port=port, timeout=timeout) File "/usr/local/lib/python3.8/smtplib.py", line 255, in __init__ (code, msg) = self.connect(host, port) File "/usr/local/lib/python3.8/smtplib.py", line 339, in connect self.sock = self._get_socket(host, port, self.timeout) File "/usr/local/lib/python3.8/smtplib.py", line 310, in _get_socket return socket.create_connection((host, port), timeout, File "/usr/local/lib/python3.8/socket.py", line 808, in create_connection raise err File "/usr/local/lib/python3.8/socket.py", line 796, in create_connection sock.connect(sa)OSError: [Errno 99] Cannot assign requested address
I understand this error is because the connection is failing to set up. I already double check my app password. I cant find the cfg file so i can't edit it.
This is the code my dag file:
from datetime import datetime, timedeltafrom airflow import DAGfrom airflow.operators.email_operator import EmailOperator# Define the default_args dictionary to set the default parameters of the DAG.default_args = {'owner': 'my_owner','depends_on_past': False,'start_date': datetime(2024, 3, 13),'email_on_failure': False,'email_on_retry': False,'retries': 1,'retry_delay': timedelta(minutes=5),}# Instantiate the DAG with the provided default_args.dag = DAG('example_email_dag', default_args=default_args, description='An example DAG sending an email using an Airflow connection', schedule_interval=timedelta(days=1),)# Define the task within the DAG.# Task: Send an email using the specified SMTP connectionsend_email_task = EmailOperator( task_id='send_email_task', to='max.al97@hotmail.com', subject='Airflow Email Example', html_content='<p>This is the body of the email.</p>', dag=dag, # Specify the connection ID created in Admin > Connections conn_id='smtp_gmail_conn', # Replace with your connection ID)# Define the task dependencies.send_email_taskfrom datetime import datetime, timedeltafrom airflow import DAGfrom airflow.operators.email_operator import EmailOperator# Define the default_args dictionary to set the default parameters of the DAG.default_args = {'owner': 'Max Alejandro','depends_on_past': False,'start_date': datetime(2024, 3, 13),'email_on_failure': False,'email_on_retry': False,'retries': 1,'retry_delay': timedelta(minutes=5),}# Instantiate the DAG with the provided default_args.dag = DAG('example_email_dag', default_args=default_args, description='An example DAG sending an email using an Airflow connection', schedule_interval=timedelta(days=1),)# Define the task within the DAG.# Task: Send an email using the specified SMTP connectionsend_email_task = EmailOperator( task_id='send_email_task', to='max.al97@hotmail.com', subject='Airflow Email Example', html_content='<p>This is the body of the email.</p>', dag=dag, # Specify the connection ID created in Admin > Connections conn_id='smtp_gmail_conn', # Replace with your connection ID)# Define the task dependencies.send_email_task
I'll really appreciate any advice, help, suggestion.