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:
@staticmethod
def send_notification(mail_from, mail_subject):
try:
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'
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
for key, ntfy_url in config['NTFY'].items():
try:
response = requests.post(
ntfy_url,
data=sanitized_from.encode(encoding='utf-8'),
headers={"Title": sanitized_subject}
)
if response.status_code == 200:
print("Notification sent successfully!")
time.sleep(5) # Ensure a delay between notifications
print(f"Notification sent successfully to {ntfy_url}!")
else:
print("Failed to send notification. Status Code:", response.status_code)
print(f"Failed to send notification to {ntfy_url}. Status Code:", response.status_code)
except requests.RequestException as e:
print(f"An error occurred: {str(e)}")
print(f"An error occurred while sending notification to {ntfy_url}: {str(e)}")
finally:
time.sleep(5) # Ensure a delay between notifications
class IMAPHandler:

View file

@ -4,4 +4,7 @@ EmailPass = YourPassword
Host = mail.server.com
[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