Akshay Lande
Akshay Lande

Reputation: 87

Getting error while sending email from GCP composer airflow

I am trying to configure email but getting following error. when running task getting following error: - I am using Python3 with airflow 10.3 versions in GCP Composer need help.

My airflow.cfg

[email]  
email_backend = airflow.contrib.utils.sendgrid.send_email  

[smtp]  
smtp_host = smtp.gmail.com  
smtp_starttls = True  
smtp_ssl = False  
smtp_user = airflow  
smtp_port = 587  
smtp_password = mypassword  
smtp_mail_from = [email protected]  

in my dag file I have created task :- dag.py file:-

from airflow.operators.email_operator import EmailOperator

email_task=EmailOperator(task_id='email_task',to="[email protected]", subject="test", html_content="<h1>Most important heading here</h1>", files=None, cc=None, bcc=None, mime_subtype='mixed', mime_charset='us_ascii', dag=dag)

Getting error:

ERROR - HTTP Error 401: Unauthorized python_http_client.exceptions.UnauthorizedError: HTTP Error 401: Unauthorize

Upvotes: 2

Views: 4072

Answers (2)

Stefano G.
Stefano G.

Reputation: 309

With Google Cloud Composer, you must set the: SENDGRID_MAIL_FROM and the: SENDGRID_API_KEY Composer "ENVIRONMENT VARIABLES"

Upvotes: 0

Tlaquetzal
Tlaquetzal

Reputation: 2850

The error is an Authorization error, so you need to check that you're setting your Sendgrid API Key (or password, if using a smtp-server) correctly.

Based on your airflow.cfg, it seems to me that you are trying to use both Sendgrid and a third-party smtp server.

The email flag determines which one to use. In this case, the flag email_backend = airflow.contrib.utils.sendgrid.send_email is specifying that you are going to use Sendgrid.

To configure Sendgrid as your email server, you need to obtain your SENDGRID_API_KEY and set both the key and SENDGRID_MAIL_FROM as environment variables.

On the other hand, if you want to use another smtp server, you have to change the email flag to email_backend = airflow.utils.email.send_email_smtp. In this case, you need to override the smtp user and password.

Upvotes: 2

Related Questions