Added support for more ntfy servers/topics

This commit is contained in:
Stefano Marinelli 2023-10-12 12:29:21 +02:00
parent 7ec65747fb
commit 483b169577
2 changed files with 26 additions and 20 deletions

View file

@ -144,26 +144,29 @@ class EmailProcessor:
class Notifier: class Notifier:
@staticmethod @staticmethod
def send_notification(mail_from, mail_subject): def send_notification(mail_from, mail_subject):
try: # Check if mail_subject and mail_from are None and replace them with default strings
ntfy_url = config['NTFY']['NtfyURL'] mail_subject = mail_subject if mail_subject is not None else "No Subject"
# Check if mail_subject and mail_from are None and replace them with default strings mail_from = mail_from if mail_from is not None else "Unknown Sender"
mail_subject = mail_subject if mail_subject is not None else "No Subject" # Sanitize mail_subject and mail_from to ensure they only contain characters that can be encoded in 'latin-1'
mail_from = mail_from if mail_from is not None else "Unknown Sender" sanitized_subject = mail_subject.encode('latin-1', errors='replace').decode('latin-1')
# Sanitize mail_subject and mail_from to ensure they only contain characters that can be encoded in 'latin-1' sanitized_from = mail_from.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') # Iterate over all notification URLs in the configuration and send notifications
response = requests.post( for key, ntfy_url in config['NTFY'].items():
ntfy_url, try:
data=sanitized_from.encode(encoding='utf-8'), response = requests.post(
headers={"Title": sanitized_subject} ntfy_url,
) data=sanitized_from.encode(encoding='utf-8'),
if response.status_code == 200: headers={"Title": sanitized_subject}
print("Notification sent successfully!") )
if response.status_code == 200:
print(f"Notification sent successfully to {ntfy_url}!")
else:
print(f"Failed to send notification to {ntfy_url}. Status Code:", response.status_code)
except requests.RequestException as e:
print(f"An error occurred while sending notification to {ntfy_url}: {str(e)}")
finally:
time.sleep(5) # Ensure a delay between notifications time.sleep(5) # Ensure a delay between notifications
else:
print("Failed to send notification. Status Code:", response.status_code)
except requests.RequestException as e:
print(f"An error occurred: {str(e)}")
class IMAPHandler: class IMAPHandler:

View file

@ -4,4 +4,7 @@ EmailPass = YourPassword
Host = mail.server.com Host = mail.server.com
[NTFY] [NTFY]
NtfyURL = https://ntfy.sh/YOUR_TOPIC NtfyURL1 = https://ntfy.sh/YOUR_TOPIC1
#NtfyURL2 = https://yourserver.sh/YOUR_TOPIC2
#NtfyURL3 = https://otherserver.sh/YOUR_TOPIC3