Custom emails watcher

This commit is contained in:
Andrea Ciceri 2024-09-28 23:59:59 +02:00
parent 60b8c06011
commit af395cafcb
Signed by: aciceri
SSH key fingerprint: SHA256:/AagBweyV4Hlfg9u092n8hbHwD5fcB6A3qhDiDA65Rg
3 changed files with 69 additions and 3 deletions

View file

@ -1,6 +1,8 @@
{
pkgs,
secrets,
fleetFlake,
lib,
...
}:
{
@ -175,9 +177,6 @@
compose = {
no-attachment-warning = "^[^>]*attach(ed|ment)";
};
triggers = {
email-received = ''exec notify-send "New email from %n" "%s"'';
};
filters = {
"text/plain" = "colorize";
"text/html" = "html";
@ -188,6 +187,18 @@
};
};
};
systemd.user.services.emails-watcher = {
Unit.Description = "Send notifications when new emails arrive";
Install = {
WantedBy = [ "default.target" ];
};
Service = {
ExecStart = "${lib.getExe fleetFlake.packages.${pkgs.system}.emails-watcher}";
Environment = [ "INBOX_NEW=~/Maildir/autistici/Inbox/new" ];
};
};
accounts.email = {
accounts.autistici = {
aerc.enable = true;

View file

@ -0,0 +1,12 @@
{
writers,
python3Packages,
...
}:
writers.writePython3Bin "emails-watcher" {
libraries = with python3Packages; [
watchdog
desktop-notifier
];
flakeIgnore = [ ];
} (builtins.readFile ./emails-watcher.py)

View file

@ -0,0 +1,43 @@
import os
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from email import message_from_file
from desktop_notifier import DesktopNotifierSync, DEFAULT_SOUND, Icon
class MaildirHandler(FileSystemEventHandler):
def __init__(self, notifier):
super().__init__()
self.notifier = notifier
def on_created(self, event):
if not event.is_directory:
print(f"New email found: {event.src_path}")
with open(event.src_path, 'r') as email_file:
msg = message_from_file(email_file)
print(f'{msg["From"]}: {msg["Subject"]}')
self.notifier.send(
title=msg["From"],
message=msg["Subject"],
sound=DEFAULT_SOUND,
icon=Icon(name="mail-message-new"),
timeout=20
)
if __name__ == "__main__":
maildir_new = os.path.expanduser(os.environ.get("INBOX_NEW"))
notifier = DesktopNotifierSync(
app_name="Mails",
notification_limit=10
)
event_handler = MaildirHandler(notifier)
observer = Observer()
observer.schedule(event_handler, maildir_new, recursive=False)
print(f"Monitoring {maildir_new} for new emails...")
observer.start()
observer.join()