Modified the code to deal with a no-sender or no-subject situation

This commit is contained in:
Stefano Marinelli 2023-10-12 08:46:43 +02:00
parent f6b161d0ac
commit 7ec65747fb

View file

@ -146,6 +146,9 @@ class Notifier:
def send_notification(mail_from, mail_subject): def send_notification(mail_from, mail_subject):
try: try:
ntfy_url = config['NTFY']['NtfyURL'] ntfy_url = config['NTFY']['NtfyURL']
# Check if mail_subject and mail_from are None and replace them with default strings
mail_subject = mail_subject if mail_subject is not None else "No Subject"
mail_from = mail_from if mail_from is not None else "Unknown Sender"
# Sanitize mail_subject and mail_from to ensure they only contain characters that can be encoded in 'latin-1' # Sanitize mail_subject and mail_from to ensure they only contain characters that can be encoded in 'latin-1'
sanitized_subject = mail_subject.encode('latin-1', errors='replace').decode('latin-1') sanitized_subject = mail_subject.encode('latin-1', errors='replace').decode('latin-1')
sanitized_from = mail_from.encode('latin-1', errors='replace').decode('latin-1') sanitized_from = mail_from.encode('latin-1', errors='replace').decode('latin-1')
@ -162,6 +165,7 @@ class Notifier:
except requests.RequestException as e: except requests.RequestException as e:
print(f"An error occurred: {str(e)}") print(f"An error occurred: {str(e)}")
class IMAPHandler: class IMAPHandler:
def __init__(self, host, email_user, email_pass): def __init__(self, host, email_user, email_pass):
self.host = host self.host = host