mirror of
https://github.com/draga79/NotiMail.git
synced 2024-09-19 19:14:19 +02:00
Added support for more ntfy servers/topics
This commit is contained in:
parent
7ec65747fb
commit
483b169577
2 changed files with 26 additions and 20 deletions
15
NotiMail.py
15
NotiMail.py
|
@ -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:
|
|
||||||
ntfy_url = config['NTFY']['NtfyURL']
|
|
||||||
# Check if mail_subject and mail_from are None and replace them with default strings
|
# 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_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"
|
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')
|
||||||
|
|
||||||
|
# Iterate over all notification URLs in the configuration and send notifications
|
||||||
|
for key, ntfy_url in config['NTFY'].items():
|
||||||
|
try:
|
||||||
response = requests.post(
|
response = requests.post(
|
||||||
ntfy_url,
|
ntfy_url,
|
||||||
data=sanitized_from.encode(encoding='utf-8'),
|
data=sanitized_from.encode(encoding='utf-8'),
|
||||||
headers={"Title": sanitized_subject}
|
headers={"Title": sanitized_subject}
|
||||||
)
|
)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
print("Notification sent successfully!")
|
print(f"Notification sent successfully to {ntfy_url}!")
|
||||||
time.sleep(5) # Ensure a delay between notifications
|
|
||||||
else:
|
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:
|
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:
|
class IMAPHandler:
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue