nailo2c opened a new pull request, #53554:
URL: https://github.com/apache/airflow/pull/53554

   Closes: #51094 
   
   # Why
   
   Airflow does **not** currently support OAuth for SMTP. Microsoft is removing 
BasicAuth as capability from Exchange Online globally this September 2025 
(Source: [Microsoft 
announcement](https://techcommunity.microsoft.com/blog/exchange/exchange-online-to-retire-basic-auth-for-client-submission-smtp-auth/4114750)).
   
   # How
   
   | **Area** | **Change** |
   |----------|------------|
   | **Core** | `send_email_smtp` / `send_mime_email` now accept 
`auth_type="oauth2"` and an optional `access_token`. |
   | **Provider** | `SmtpHook`, `SmtpNotifier`, and `EmailOperator` accept 
`auth_type`; the hook reads either `access_token` **or** `client_id` + 
`client_secret` from *Connection extra*. |
   | **UI** | Connection form exposes the new fields (`auth_type`, 
`access_token`, `client_id`, …). |
   | **Docs** | Updated **connections/smtp.rst**. |
   | **Tests** | New unit tests cover both Basic Auth and OAuth 2 paths. |
   
   
   # What
   
   ### Step 1
   
   Use the [Goole Oauth 
Playground](https://developers.google.com/oauthplayground) to obtain an 
`access_token`.
   
   ### Step 2
   
   Start Airflow locally with `breeze start-airflow` and add a connection:
   ```bash
   airflow connections add 'smtp_gmail_oauth2' \
     --conn-type smtp \
     --conn-host smtp.gmail.com \
     --conn-port 587 \
     --conn-login '[email protected]' \
     --conn-extra '{
       "from_email": "[email protected]",
       "auth_type": "oauth2",
       "access_token": "ya29.*****",
       "disable_ssl": "true"
     }'
   ```
   
   ### Step 3
   
   Tested by run this DAG
   ```python
   from airflow import DAG
   from airflow.operators.python import PythonOperator
   from airflow.providers.smtp.hooks.smtp import SmtpHook
   from datetime import datetime
   
   def gmail_oauth2_test():
       with SmtpHook(smtp_conn_id="smtp_gmail_oauth2", auth_type="oauth2") as 
hook:
           hook.send_email_smtp(
               to="[email protected]",
               subject="[Airflow→Gmail] OAuth2 OK",
               html_content="<h3>Gmail XOAUTH2 works 🎉</h3>",
           )
   
   with DAG(
       dag_id="test_gmail_oauth2",
       start_date=datetime(2025,7,1),
       schedule=None,
       catchup=False,
   ) as dag:
       PythonOperator(task_id="send_mail", python_callable=gmail_oauth2_test)
   ```
   
   Or add in `airflow.cfg`
   ```console
   [smtp]
   smtp_host = smtp.gmail.com
   smtp_port = 587
   smtp_starttls = True
   smtp_ssl = False 
   ```
   
   Then in a Python shell:
   ```python
   from airflow.utils.email import send_email_smtp
   send_email_smtp(
       to="[email protected]",
       subject="[Airflow utils] XOAUTH2 test",
       html_content="<h3>🎉 utils.send_email_smtp works</h3>",
       conn_id="smtp_gmail_oauth2",
       auth_type="oauth2",
       access_token="ya29.*****"
   )
   ```
   
   ### Step 4
   
   Confirm the message arrived in mailbox.
   <img width="459" height="192" alt="截圖 2025-07-19 下午2 53 06" 
src="https://github.com/user-attachments/assets/4936f4df-6ff8-44b6-8077-ed5aded5e736";
 />
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to