Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion scraibe_webui/misc/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ mail:
queue_position: null
contact_email: support@mail.com
mail_css_path: scraibe_webui/misc/mail_style.css
use_receiver_whitelist: False
receiver_whitelist: "@mail.com"
advanced:
keep_model_alive: false # for sync interfac only keeps the model alvide during a session
concurrent_workers_async: 1 # number of concurrent working threads in the async interface
concurrent_workers_async: 1 # number of concurrent working threads in the async interface
38 changes: 27 additions & 11 deletions scraibe_webui/utils/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def __init__(self,
error_subject: str = "An error occurred during processing.",
success_template: str = None,
success_subject: str = "Your transcript is ready.",
css_template_path: str = None) -> None:
css_template_path: str = None,
use_receiver_whitelist: bool = False,
receiver_whitelist: str = None) -> None:
"""
Initializes the Mail Service class.

Expand All @@ -45,6 +47,8 @@ def __init__(self,
success_template (str, optional): HTML template for success notifications.
success_subject (str, optional): Subject line for success notifications.
css_template_path (str, optional): Path to a CSS file for email styling.
use_receiver_whitelist (bool): Restrict sending of emails to whitelisted domains
receiver_whitelist (str, optional): unseparated list of domains

Returns:
None
Expand Down Expand Up @@ -75,6 +79,10 @@ def __init__(self,
# Delay mail server setup until needed
self.mailserver = None

# Implement recipient whitelist
self.use_receiver_whitelist = use_receiver_whitelist
self.receiver_whitelist = receiver_whitelist

def setup_context(self, context: Union[None, str, dict, ssl.SSLContext]) -> Optional[ssl.SSLContext]:
"""
Setup the SSL context based on the provided context parameter.
Expand Down Expand Up @@ -140,16 +148,22 @@ def send_mail(self, receiver_email: str, subject: str, message: str, attachments
message (str): The email body.
attachments (list, optional): List of file paths to attach.
"""
_message = self.setup_message(subject, receiver_email, message, attachments)
if not self.mailserver:
self.mailserver = self.setup_mailserver()

receiver_domain = receiver_email.strip().split('@')[-1]
if ((self.use_receiver_whitelist) and
not (receiver_domain.lower() in self.receiver_whitelist.lower())):
raise ValueError(f"Email domain not in whitelist")
else:
_message = self.setup_message(subject, receiver_email, message, attachments)
if not self.mailserver:
warnings.warn("Failed to connect to the mail server. Email not sent.")
return
try:
self.mailserver.sendmail(self.sender_email, receiver_email, _message.as_string())
except Exception as e:
warnings.warn(f"Failed to send email: {e}")
self.mailserver = self.setup_mailserver()
if not self.mailserver:
warnings.warn("Failed to connect to the mail server. Email not sent.")
return
try:
self.mailserver.sendmail(self.sender_email, receiver_email, _message.as_string())
except Exception as e:
warnings.warn(f"Failed to send email: {e}")

def setup_message(self, subject: str, receiver_email: str, message: str, attachments: list = None) -> MIMEMultipart:
"""Prepare the email message.
Expand Down Expand Up @@ -247,6 +261,8 @@ def from_config(cls, config: dict):
success_template=config.get('success_template'),
success_subject=config.get('success_subject', "Your transcript is ready."),
css_template_path=config.get('css_template_path'),
use_receiver_whitelist=config.get('use_receiver_whitelist'),
receiver_whitelist=config.get('receiver_whitelist'),
)

def __repr__(self) -> str:
Expand All @@ -257,4 +273,4 @@ def __repr__(self) -> str:
"""
return (f"MailService(sender_email={self.sender_email}, smtp_server={self.smtp_server}, "
f"smtp_port={self.smtp_port}, default_subject={self.default_subject}, "
f"connection_type={self.connection_type})")
f"connection_type={self.connection_type})")