@@ -146,14 +146,36 @@ def _cleanup_resources(self, msg, msgdata):
146146 def _get_smtp_connection (self ):
147147 """Get a new SMTP connection with proper configuration."""
148148 try :
149- conn = smtplib .SMTP (mm_cfg .SMTPHOST , mm_cfg .SMTPPORT , timeout = 30 )
150- if mm_cfg .SMTP_USE_TLS :
151- conn .starttls ()
152- # Add SMTP authentication if configured
153- if hasattr (mm_cfg , 'SMTP_USER' ) and hasattr (mm_cfg , 'SMTP_PASSWORD' ):
154- if mm_cfg .SMTP_USER and mm_cfg .SMTP_PASSWORD :
155- mailman_log ('debug' , 'OutgoingRunner._get_smtp_connection: Authenticating with SMTP server' )
156- conn .login (mm_cfg .SMTP_USER , mm_cfg .SMTP_PASSWORD )
149+ conn = smtplib .SMTP ()
150+ conn .set_debuglevel (mm_cfg .SMTPLIB_DEBUG_LEVEL )
151+ conn .connect (mm_cfg .SMTPHOST , mm_cfg .SMTPPORT )
152+
153+ if mm_cfg .SMTP_AUTH :
154+ if mm_cfg .SMTP_USE_TLS :
155+ try :
156+ conn .starttls ()
157+ except smtplib .SMTPException as e :
158+ mailman_log ('error' , 'SMTP TLS error: %s' , str (e ))
159+ conn .quit ()
160+ return None
161+ try :
162+ helo_host = mm_cfg .SMTP_HELO_HOST or socket .getfqdn ()
163+ conn .ehlo (helo_host )
164+ except smtplib .SMTPException as e :
165+ mailman_log ('error' , 'SMTP EHLO error: %s' , str (e ))
166+ conn .quit ()
167+ return None
168+ try :
169+ conn .login (mm_cfg .SMTP_USER , mm_cfg .SMTP_PASSWD )
170+ except smtplib .SMTPHeloError as e :
171+ mailman_log ('error' , 'SMTP HELO error: %s' , str (e ))
172+ conn .quit ()
173+ return None
174+ except smtplib .SMTPAuthenticationError as e :
175+ mailman_log ('error' , 'SMTP AUTH error: %s' , str (e ))
176+ conn .quit ()
177+ return None
178+
157179 return conn
158180 except Exception as e :
159181 mailman_log ('error' , 'SMTP connection failed: %s' , str (e ))
0 commit comments