Skip to content

Commit

Permalink
api.email_sender: catch SMTP related exception
Browse files Browse the repository at this point in the history
Add `try-catch` block to `EmailSender._send_email` method
to catch exception while sending emails.

Signed-off-by: Jeny Sadadia <[email protected]>
  • Loading branch information
Jeny Sadadia authored and nuclearcat committed Dec 28, 2023
1 parent 3744ee6 commit d494208
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions api/email_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import email
import email.mime.text
import smtplib
from fastapi import HTTPException, status
from .config import EmailSettings


Expand Down Expand Up @@ -47,10 +48,17 @@ def _create_email(self, email_subject, email_content, email_recipient):

def _send_email(self, email_msg):
"""Method to send an email message using SMTP"""
smtp = self._smtp_connect()
if smtp:
smtp.send_message(email_msg)
smtp.quit()
try:
smtp = self._smtp_connect()
if smtp:
smtp.send_message(email_msg)
smtp.quit()
except Exception as exc:
print(f"Error in sending email: {str(exc)}")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to send email"
)from exc

def create_and_send_email(self, email_subject, email_content,
email_recipient):
Expand Down

0 comments on commit d494208

Please sign in to comment.