Updated smtp.py to support Office 365 SMTP

pull/22009/head
vcpandya 11 months ago committed by GitHub
parent 1760179093
commit 8a3a2d76c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -19,16 +19,14 @@ class SMTPClient:
def send(self, mail: dict):
smtp = None
try:
if self.use_tls:
if self.opportunistic_tls:
# Always use STARTTLS for ports like 587 (Office365/modern SMTP)
if self.port == 465:
smtp = smtplib.SMTP_SSL(self.server, self.port, timeout=10)
elif self.use_tls or self.opportunistic_tls:
smtp = smtplib.SMTP(self.server, self.port, timeout=10)
# Send EHLO command with the HELO domain name as the server address
smtp.ehlo(self.server)
smtp.starttls()
# Resend EHLO command to identify the TLS session
smtp.ehlo(self.server)
else:
smtp = smtplib.SMTP_SSL(self.server, self.port, timeout=10)
else:
smtp = smtplib.SMTP(self.server, self.port, timeout=10)
@ -43,10 +41,10 @@ class SMTPClient:
msg.attach(MIMEText(mail["html"], "html"))
smtp.sendmail(self._from, mail["to"], msg.as_string())
except smtplib.SMTPException as e:
except smtplib.SMTPException:
logging.exception("SMTP error occurred")
raise
except TimeoutError as e:
except TimeoutError:
logging.exception("Timeout occurred while sending email")
raise
except Exception as e:

Loading…
Cancel
Save