Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api.email_sender: catch SMTP related exceptions #443

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions api/email_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import email
import email.mime.text
import smtplib
from fastapi import HTTPException, status
from .config import EmailSettings


class EmailSender:

Check warning on line 18 in api/email_sender.py

View workflow job for this annotation

GitHub Actions / Lint

Too few public methods (1/2)
"""Class to send email report using SMTP"""
def __init__(self):
self._settings = EmailSettings()
Expand Down Expand Up @@ -47,10 +48,17 @@

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
Loading