Search code examples
linuxubuntuemailsshpostfix-mta

Is there any way to setup alarm for ubuntu's /var/mail via SSH or else?


Is there any frameworks / GUI to setup an alarm that triggers whenever mail is sent to /var/mail on ubuntu server? I'm currently using localhost because of closed network connection, and only way that I can access the server is using ssh. My current way of using this is to just use mutt whenever I care, but if there is an alarm trigger it would be very helpful for me - via SSH or something else.

Restrictions:

  • I cannot use SMTP with gmail via postfix server because of the closed server environment
  • Seems like thunderbird supported to see localhost mails, but it is deprecated. Also, I would prefer not to use Remote Connection (via X.11) due to lack of internet speed.

Any ideas or frameworks? Thanks.


Solution

  • #!/bin/bash
    
    # Monitor /var/mail directory for changes
    inotifywait -m /var/mail -e create -e moved_to |
    while read path action file; do
        # When a new file (mail) is created or moved into /var/mail, notify
        notify-send "New mail received in /var/mail" "Check your mailbox."
    done
    

    Save this script, for example as mail_notification.sh, and make it executable (chmod +x mail_notification.sh). Setting up Notifications:

    Ensure notify-send is installed on your server. It’s usually part of the libnotify-bin package, which you can install via sudo apt-get install libnotify-bin if not already installed. Adjust the script path and permissions as needed (chmod +x mail_notification.sh). Running the Script:

    Run the script in a detached session or within a screen session on your server to keep it running even after you disconnect from SSH.