2

I'm trying to set up an Airflow DAG that is able to send emails through the EmailOperator in Composer 2, Airflow 2.3.4. I've followed this guide. I tried running the example DAG that is provided in the guide, but I get an HTTP 400 error. The log looks like this:

[2023-01-20, 10:46:45 UTC] {taskinstance.py:1904} ERROR - Task failed with exception
Traceback (most recent call last):
  File "/opt/python3.8/lib/python3.8/site-packages/airflow/operators/email.py", line 75, in execute
    send_email(
  File "/opt/python3.8/lib/python3.8/site-packages/airflow/utils/email.py", line 58, in send_email
    return backend(
  File "/opt/python3.8/lib/python3.8/site-packages/airflow/providers/sendgrid/utils/emailer.py", line 123, in send_email
    _post_sendgrid_mail(mail.get(), conn_id)
  File "/opt/python3.8/lib/python3.8/site-packages/airflow/providers/sendgrid/utils/emailer.py", line 142, in _post_sendgrid_mail
    response = sendgrid_client.client.mail.send.post(request_body=mail_data)
  File "/opt/python3.8/lib/python3.8/site-packages/python_http_client/client.py", line 277, in http_request
    self._make_request(opener, request, timeout=timeout)
  File "/opt/python3.8/lib/python3.8/site-packages/python_http_client/client.py", line 184, in _make_request
    raise exc
python_http_client.exceptions.BadRequestsError: HTTP Error 400: Bad Request

I've looked at similar threads on Stackoverflow but none of those suggestions worked for me.

  • I have set up and verified the from email address in Sendgrid and it uses a whole email address including the domain.
  • I also set this email address up in Secret Manager (as well as the API key).
  • I haven't changed the test DAG from the guide, except for the 'to' address.
  • In another DAG I've tried enabling 'email_on_retry' and that also didn't trigger any mail.

I'm at a loss here, can someone provide me with suggestions on things to try?

ramoniazzz
  • 43
  • 2
  • Nope, since this post I've only managed to verify that it is not due to an invalid API key as the key works if I do the API call using curl – ramoniazzz Jan 23 '23 at 08:24
  • Can you confirm whether your [`html_content`](https://cloud.google.com/composer/docs/composer-2/configure-email#test_your_sendgrid_configuration) is empty or not, for example: html_content='' . If it is empty, can you try again after adding something? – kiran mathew Jan 23 '23 at 10:30
  • 1
    Hi Kiran, I've verified that html content is not empty. Additionally, I've found out that the problem is likely due to Cloud Composer not reading the airflow-variables-from_email secret I've set up in secret manager. I've added the SENDGRID_MAIL_FROM environment variable (which I assumed should be obsolete due to having set up the secret in secret manager) and now it sent out the email. However, I also store the API key in secret manager and it appears Composer is able to retrieve that secret, but not the 'from mail'. Any ideas why this might happen? – ramoniazzz Jan 23 '23 at 10:46
  • Does this [article](https://cloud.google.com/composer/docs/secret-manager#configure-backend) by Google help you to solve your problem? – kiran mathew Jan 23 '23 at 14:07
  • No it doesn't unfortunately. I have successfully enabled Secret Manager for my cloud composer environment, it's just that for some reason it's not picking up on the 'from mail' secret, it's only noticing the api key. I have to provide the from mail as an environment variable to make it work and I feel like that's not supposed to happen based on Google's documentation. – ramoniazzz Jan 23 '23 at 14:20
  • I have the same issue. I didn't find the solution yet. Regarding SENDGRID_MAIL_FROM variable, it might be the same as smtp_mail_from which can't be retrieved from the connection. Source: https://github.com/apache/airflow/issues/19805#issuecomment-977875502 Could you please share the solution if you resolved it? – HumanoVirtual Apr 20 '23 at 12:58
  • @HumanoVirtual did you find the solution? – Javier Lopez Tomas Aug 07 '23 at 18:01

0 Answers0