From e1b9e9ecc1fd5aed666f08fa098aecf94aef50f2 Mon Sep 17 00:00:00 2001 From: Stefano Marinelli Date: Fri, 13 Oct 2023 09:07:59 +0200 Subject: [PATCH] Added Pushover support and documentation fixes --- NotiMail.py | 33 ++++++++++++++++++++++++++++++++- README.md | 12 +++++++----- config.ini | 7 ++++++- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/NotiMail.py b/NotiMail.py index 0377b86..2bbd170 100644 --- a/NotiMail.py +++ b/NotiMail.py @@ -1,6 +1,6 @@ """ NotiMail -Version: 0.8 +Version: 0.8 - Alpha Author: Stefano Marinelli License: BSD 3-Clause License @@ -171,6 +171,32 @@ class NTFYNotificationProvider(NotificationProvider): finally: time.sleep(5) # Ensure a delay between notifications +class PushoverNotificationProvider(NotificationProvider): + def __init__(self, api_token, user_key): + self.api_token = api_token + self.user_key = user_key + self.pushover_url = "https://api.pushover.net/1/messages.json" + + def send_notification(self, mail_from, mail_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" + + message = f"From: {mail_from}\nSubject: {mail_subject}" + + data = { + "token": self.api_token, + "user": self.user_key, + "message": message + } + + try: + response = requests.post(self.pushover_url, data=data) + if response.status_code == 200: + print("Notification sent successfully via Pushover!") + else: + print(f"Failed to send notification via Pushover. Status Code:", response.status_code) + except requests.RequestException as e: + print(f"An error occurred while sending notification via Pushover: {str(e)}") class Notifier: def __init__(self, providers): @@ -246,6 +272,11 @@ if 'NTFY' in config: ntfy_urls = [config['NTFY'][url_key] for url_key in config['NTFY']] providers.append(NTFYNotificationProvider(ntfy_urls)) +if 'PUSHOVER' in config: + api_token = config['PUSHOVER']['ApiToken'] + user_key = config['PUSHOVER']['UserKey'] + providers.append(PushoverNotificationProvider(api_token, user_key)) + # Initialize Notifier with providers notifier = Notifier(providers) diff --git a/README.md b/README.md index 0621eb2..24ff5be 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ **Development is ongoing, and the project is in the early alpha stage - things may break!** -Stay connected without the constant drain on your battery. Introducing **NotiMail** - the future of server-side email notifications using the renowned `ntfy` service! +Stay connected without the constant drain on your battery. Introducing **NotiMail** - the future of server-side email notifications supporting multiple push providers! Mobile devices often use IMAP IDLE, maintaining a persistent connection to ensure real-time email notifications. Such continuous connections rapidly consume battery power. The modern era demanded a smarter, more energy-efficient solution. Meet NotiMail. @@ -12,22 +12,24 @@ Mobile devices often use IMAP IDLE, maintaining a persistent connection to ensur - **Processes and Notifies**: Once a new email is detected, NotiMail swiftly processes its details. -- **Leverages 'ntfy' for Alerts**: Rather than having your device always on alert, NotiMail sends notifications via the `ntfy` service, ensuring you're promptly informed. +- **Leverages Multiple Push Providers for Alerts**: Rather than having your device always on alert, NotiMail sends notifications via multiple push providers, ensuring you're promptly informed. - **Database Integration**: NotiMail uses an SQLite3 database to store and manage processed email UIDs, preventing repeated processing. - **Built for Resilience**: With connectivity hiccups in mind, NotiMail ensures you're always the first to know. + +- **Multiple And Different Push providers supported**: You can use one or more of the supported Push providers, which currently include ntfy and Pushover. ## Benefits 🚀 - **Extended Battery Life**: Experience a noticeable improvement in your device's battery lifespan. -- **Swift Notifications**: With the `ntfy` service, NotiMail provides real-time notifications without delay. +- **Swift Notifications**: Using one of the supported push providers, NotiMail provides real-time notifications without delay. - **Reduced Data Consumption**: With notifications being the primary data exchange, you can save on unnecessary data usage. -- **Always in the Loop**: Whether it's a new email or a server glitch, NotiMail and `ntfy` guarantee you're always informed. +- **Always in the Loop**: Whether it's a new email or a server glitch, NotiMail and push notifications guarantee you're always informed. ## Implementation Details 🔧 @@ -109,7 +111,7 @@ bash **5. Configure NotiMail:** -Open the `config.ini` file in a text editor. Update the `[EMAIL]` section with your email credentials and host, and the `[NTFY]` section with the appropriate `NtfyURL`. For more information about ntfy and its configuration, please read the [official website](https://docs.ntfy.sh/) +Open the `config.ini` file in a text editor. Update the `[EMAIL]` section with your email credentials and host, and the configuration of one (or more) of the supported push providers. **6. Run NotiMail:** diff --git a/config.ini b/config.ini index 51cb10a..55de23d 100644 --- a/config.ini +++ b/config.ini @@ -3,8 +3,13 @@ EmailUser = your@address.com EmailPass = YourPassword Host = mail.server.com -#If your provider is NTFY, uncomment the following lines +#If your provider is NTFY, uncomment the following lines and configure #[NTFY] #Url1 = https://ntfy.sh/TOPIC1 #Url2 = https://ntfy.sh/TOPIC2 +#If your provider is PUSHOVER, uncomment the following lines and configure +#[PUSHOVER] +#ApiToken = YOUR_PUSHOVER_API_TOKEN +#UserKey = YOUR_PUSHOVER_USER_KEY +