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

Loading…
Cancel
Save