ITJamie commented on code in PR #30531:
URL: https://github.com/apache/airflow/pull/30531#discussion_r1240510568


##########
airflow/utils/email.py:
##########
@@ -19,22 +19,69 @@
 
 import collections.abc
 import logging
-import os
 import re
-import smtplib
 import warnings
-from email.mime.application import MIMEApplication
-from email.mime.multipart import MIMEMultipart
-from email.mime.text import MIMEText
-from email.utils import formatdate
 from typing import Any, Iterable
 
 from airflow.configuration import conf
-from airflow.exceptions import AirflowConfigException, AirflowException, 
RemovedInAirflow3Warning
+from airflow.exceptions import AirflowConfigException, RemovedInAirflow3Warning
+from airflow.models import Connection
+from airflow.providers.smtp.hooks.smtp import SmtpHook
 
 log = logging.getLogger(__name__)
 
 
+class _SmtpHook(SmtpHook):
+    @classmethod
+    def get_connection(cls, conn_id: str) -> Connection:
+        try:
+            connection = super().get_connection(conn_id)
+        except Exception:
+            connection = Connection()
+
+        extra = connection.extra_dejson
+
+        # try to load some variables from Airflow config to update connection 
extra
+        from_email = conf.get("smtp", "SMTP_MAIL_FROM") or conf.get("email", 
"from_email", fallback=None)
+        if from_email:
+            extra["from_email"] = from_email
+
+        smtp_host = conf.get("smtp", "SMTP_HOST", fallback=None)
+        if smtp_host:
+            connection.host = smtp_host
+        smtp_port = conf.getint("smtp", "SMTP_PORT", fallback=None)
+        if smtp_port:
+            connection.port = smtp_port
+        smtp_starttls = conf.getboolean("smtp", "SMTP_STARTTLS", fallback=None)
+        if smtp_starttls is not None:
+            extra["disable_tls"] = not smtp_starttls
+        smtp_ssl = conf.getboolean("smtp", "SMTP_SSL", fallback=None)
+        if smtp_ssl is not None:
+            extra["disable_ssl"] = not smtp_ssl
+        smtp_retry_limit = conf.getint("smtp", "SMTP_RETRY_LIMIT", 
fallback=None)
+        if smtp_retry_limit is not None:
+            extra["retry_limit"] = smtp_retry_limit
+        smtp_timeout = conf.getint("smtp", "SMTP_TIMEOUT", fallback=None)
+        if smtp_timeout is not None:
+            extra["timeout"] = smtp_timeout
+
+        # for credentials, we use the connection if it exists, otherwise we 
use the config
+        if connection.login is None or connection.password is None:
+            warnings.warn(

Review Comment:
   This warning assumes incorrectly that it has to fallback to cfg settings to 
find a username or password for smtp. Not all smtp servers require 
authentication to send mail. 
   
   It can not be assumed that if a connection has no username or password that 
the connection config is wrong and thus falling back to legacy config



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to