Run mutt in background / alert on new mail

Kernels & Hardware, configuring network, installing services

Run mutt in background / alert on new mail

Postby bedtime » 2018-07-18 12:43

It's actually neomutt that I'm running—a fork of mutt that runs the exact same (uses same commands and .mutt/muttrc config file...) but allows for a user set command to be run upon a new email:

Opens a popup window with the notification, "New Mail!":

.mutt/muttrc:
Code: Select all
...
set new_mail_command='notify-send -t 0 New Mail!'


This seems to work great if I have a terminal open with neomutt (mutt) running; but, if I choose to run it in the background (via 'exec neomutt' in ~/.config/i3/config in i3), it will not alert me. I can see that the process, neomutt, is indeed running in the background as 'pstree user' confirms it. Strange thing as other processes such as feh and urxvtc work just fine being called by i3 in such manner.

I've managed to call neomutt from .xsession and .profile but with the same results.

Basically I would like to have some type of system of checking email that is very lightweight and which alerts me. A script outline would be great. I used to have one-liner curl script that checked for new email, but I've lost the code for it. I've searched the web and no luck; most curl scripts are for sending mail and not retrieving it. As well, I had neomutt alerting me just fine in i3 before I had to reinstall.

Any ideas? I'm open to new suggestions.
OS: Debian GNU/Linux (Buster—testing); Kernel: 4.16.0-2-amd64; Shell: sh; WM: i3
Machine: HP ProDesk 600 G3—i7-7700, 8GB RAM, 256 SSD, Intel HD Graphics 630

bedtime
 
Posts: 93
Joined: 2012-12-16 19:34

Re: Run mutt in background / alert on new mail

Postby shep » 2018-07-18 14:12

The classic email notification daemons are biff/xbiff. Implementation depends on mail server (imap vs pop3) and if you poll a local or remote mail server.
shep
 
Posts: 222
Joined: 2011-03-15 15:22

Re: Run mutt in background / alert on new mail

Postby bedtime » 2018-07-18 16:18

shep wrote:The classic email notification daemons are biff/xbiff. Implementation depends on mail server (imap vs pop3) and if you poll a local or remote mail server.

Thank for the suggestion, but doesn't mutt have to be running anyways to poll the mail server? Then I'm back at square one.

In any event, I don't know how to get biff working—tried using it before; it's just way over my head.


Here is my config if that helps:

~/.mutt/muttrc:
Code: Select all
set ssl_starttls=yes
set ssl_force_tls=yes
set imap_user = 'user@autistici.org'
set imap_pass = 'mypass'
set from='user@autistici.org'
set realname='My name'
set folder = imaps://mail.autistici.org/
set spoolfile = imaps://mail.autistici.org/INBOX
set postponed="imaps://mail.autistici.org/Drafts"
set header_cache = "~/.mutt/cache/headers"
set message_cachedir = "~/.mutt/cache/bodies"
set certificate_file = "~/.mutt/certificates"
set move = no
set folder = '~/mail'

set mail_check_recent = no
set imap_idle = yes
set imap_keepalive = 1200
set timeout = 60
set mail_check = 30
set new_mail_command='/usr/bin/notify-send -t 0 New Mail!'

set mbox_type = mbox
set sort = date
set sort_aux = last-date-received                           

set quit = yes
OS: Debian GNU/Linux (Buster—testing); Kernel: 4.16.0-2-amd64; Shell: sh; WM: i3
Machine: HP ProDesk 600 G3—i7-7700, 8GB RAM, 256 SSD, Intel HD Graphics 630

bedtime
 
Posts: 93
Joined: 2012-12-16 19:34

Re: Run mutt in background / alert on new mail

Postby arzgi » 2018-07-18 17:05

bedtime wrote:
shep wrote:The classic email notification daemons are biff/xbiff. Implementation depends on mail server (imap vs pop3) and if you poll a local or remote mail server.

Thank for the suggestion, but doesn't mutt have to be running anyways to poll the mail server? Then I'm back at square one.

In any event, I don't know how to get biff working—tried using it before; it's just way over my head.


biff/xbiff or in my case xfce4-mailwatch-plugin are configured on their own, ie. they don't depend on your mail client. You just configure to them your mail-account and password, and how often will they check, and they just react when new mail arrives. And I have configured xfce4-mailwatch-plugin to open my mail client when clicked.
arzgi
 
Posts: 387
Joined: 2008-02-21 17:03
Location: Finland

Re: Run mutt in background / alert on new mail

Postby bedtime » 2018-07-18 18:20

arzgi wrote:
bedtime wrote:
shep wrote:The classic email notification daemons are biff/xbiff. Implementation depends on mail server (imap vs pop3) and if you poll a local or remote mail server.

Thank for the suggestion, but doesn't mutt have to be running anyways to poll the mail server? Then I'm back at square one.

In any event, I don't know how to get biff working—tried using it before; it's just way over my head.


biff/xbiff or in my case xfce4-mailwatch-plugin are configured on their own, ie. they don't depend on your mail client. You just configure to them your mail-account and password, and how often will they check, and they just react when new mail arrives. And I have configured xfce4-mailwatch-plugin to open my mail client when clicked.

I'll have to look into it another day. I appreciate your answer, but my patience will only allow me to do so much today. :|

Anyways, I think I've figured it out, though it may be considered a hack?

~/.mutt/muttrc:
Code: Select all
...
set new_mail_command = 'beep; DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send New Mail!'


~/.config/i3/config:
Code: Select all
exec --no-startup-id "sleep 3; neomutt"

The above is ONLY useful if you run the i3 window manager (if you haven't heard of it, you aren't running it.)

Seems that because it was running in the background it couldn't find what screen it needed to be on. The 'beep' function worked, which gave me the clue. Also, it seems to like some sleep before starting.

As of now, it's SOLVED. Once again, thank you arzgi and shep for your input and ideas!
OS: Debian GNU/Linux (Buster—testing); Kernel: 4.16.0-2-amd64; Shell: sh; WM: i3
Machine: HP ProDesk 600 G3—i7-7700, 8GB RAM, 256 SSD, Intel HD Graphics 630

bedtime
 
Posts: 93
Joined: 2012-12-16 19:34

Re: Run mutt in background / alert on new mail

Postby shep » 2018-07-18 19:52

Looks like you have the concept. It would be worth spending the time to use biff/xbiff from the standpoint of memory footprint; biff/xbiff are much smaller than neomutt.
I do not use either but I recall seeing some online examples regarding configuration.
shep
 
Posts: 222
Joined: 2011-03-15 15:22

Re: Run mutt in background / alert on new mail

Postby bedtime » 2018-07-18 23:58

shep wrote:Looks like you have the concept. It would be worth spending the time to use biff/xbiff from the standpoint of memory footprint; biff/xbiff are much smaller than neomutt.
I do not use either but I recall seeing some online examples regarding configuration.

Yes, memory usage is something that is quite important to me. I've taken some more time to try to find out how to setup biff, but I've yet to see an example that applies, almost all examples pertaining to sending mail (and not retrieving it).

There must be a script somewhere that can quickly find out if there is mail. I made a program in C++ that works great, but each time I reinstall an OS I have to install C++ and curl dev libraries and try to figure out how to get curl libs to work without a shared libraries error. I've done this 3 times, and it's time for something that is less intensive to setup.

Mutt is working okay for now, but it has to be running while the email is sent for the notification to work. Which means that I must manually check mutt each time I start up my computer. You might think this is a trifle pointless. Well, you would be correct. :roll:
OS: Debian GNU/Linux (Buster—testing); Kernel: 4.16.0-2-amd64; Shell: sh; WM: i3
Machine: HP ProDesk 600 G3—i7-7700, 8GB RAM, 256 SSD, Intel HD Graphics 630

bedtime
 
Posts: 93
Joined: 2012-12-16 19:34

Re: Run mutt in background / alert on new mail

Postby shep » 2018-07-19 02:42

Alot of this depends on your email setup. In the OLD days, mails used to be retrieved by a separate application (getmail/fetchmail) which was run as a chron job. Mails were stored locally in /var/mail. biff/xbiff would check /var/mail by default. Without knowing your email setup (there are 4 mail formats), imap vs pop3 and if your using an MTA and email authentication, it is hard to give much advice.


I did some searching and found gnubiff, asmail and mail-notification but I do not have any experience with them.
http://gnubiff.sourceforge.net/
http://asmail.sourceforge.net/features.html
https://packages.debian.org/stretch/mail-notification
shep
 
Posts: 222
Joined: 2011-03-15 15:22

Re: Run mutt in background / alert on new mail

Postby arzgi » 2018-07-19 08:14

SImple
Code: Select all
aptitude search mail
listed many email-notfiers among others.
arzgi
 
Posts: 387
Joined: 2008-02-21 17:03
Location: Finland

Re: Run mutt in background / alert on new mail

Postby bedtime » 2018-07-20 19:23

shep & arzgi,

Thank you so much! With your suggestions I managed to run into offlineimap (# apt install offlinemap) and get a script working. I've posted it below to help any others who may benefit... Feel free to tweak / offer suggestions. :D

~/.offlineimaprc:
Code: Select all
[general]
accounts = accountname
maxsyncaccounts = 3
socktimeout = 60

ui = TTY.TTYUI

[Account accountname]
localrepository = local
remoterepository = remote

[Repository local]
type = Maildir
localfolders = /home/user/Mail

[Repository remote]
type = IMAP
remotehost = mail.myemailaddress.com
remoteuser = bedtime@myemailaddress.com
remotepass = mypassword
cert_fingerprint = 96dd372bb0253b5aad1a248cc607d93d2da6591cf23cd8bf655739741ceb8543900d3d345d39090841557b3692b90a3$
ssl = yes
realdelete = no


checkMail.sh:
Code: Select all
#!/bin/sh

# Check and download all NEW (previously not downloaded) messages in 'INBOX' (and cleanup output garbage for user)
offlineimap -f INBOX >/dev/null 2>&1

# Get an 'ls -la' printout of only the latest file within the offlineimap folder
currentMail=$(ls -la /home/user/Mail/INBOX/new/$(ls -Art /home/user/Mail/INBOX/new | tail -n 1))

# Grab the last latest message info for comparison
lastMail=$(cat ~/scripts/lastmail)

if [ "$currentMail" != "$lastMail" ]; then

        # Sync message info so the alarm doesn't keep going off
        echo $currentMail > ~/scripts/lastmail
        beep
        notify-send -t 0 "<span color='#ffffff' font='26px' face='mono'><b>New Mail!</b></span>"
        echo "New Mail!"
fi


Run the program:
Code: Select all
./checkMail.sh

[u]You will get an error pertaining to a fingerprint missmatch. Replace the proper fingerprint, which is given in the error, in your .offlineimaprc file under cert_fingerprint = .


After you find out you have email, you can run a script to launch an email program and reset the notification:

readMail.sh:
Code: Select all
#!/bin/sh

mutt

# Now that you've finished with your email, sync the email alert file (to turn off the present alert):
currentMail=$(ls -la /home/user/Mail/INBOX/new/$(ls -Art /home/user/Mail/INBOX/new | tail -n 1))
echo $currentMail > ~/scripts/lastmail



I just run ./checkMail every few minutes to be alerted on emails. The beauty of it is that you don't need a persistant program running in the background; it just checks and exits with its results. There are still some kinks, but overall this is much better than before.
OS: Debian GNU/Linux (Buster—testing); Kernel: 4.16.0-2-amd64; Shell: sh; WM: i3
Machine: HP ProDesk 600 G3—i7-7700, 8GB RAM, 256 SSD, Intel HD Graphics 630

bedtime
 
Posts: 93
Joined: 2012-12-16 19:34

Re: Run mutt in background / alert on new mail

Postby shep » 2018-07-20 23:36

Good work. To make it even easier, you could run your script as a cron job:
https://debian-administration.org/article/56/Command_scheduling_with_cron
and potentially with a notification flag
https://gist.github.com/nlamirault/38cfc46682eaeb28709e
shep
 
Posts: 222
Joined: 2011-03-15 15:22

Re: Run mutt in background / alert on new mail

Postby bedtime » 2018-07-21 09:12

shep wrote:Good work. To make it even easier, you could run your script as a cron job:
https://debian-administration.org/article/56/Command_scheduling_with_cron
and potentially with a notification flag
https://gist.github.com/nlamirault/38cfc46682eaeb28709e

Thanks. It took some doing (and a little hair loss), but it was worth it!

I was considering using a cron job, but I prefer to have my dzen2 script run it so it can integrate into the status bar with less resources. This also means that I don't need an extra notification flag.

I bumped into isync (uses mbsync to grab mail), so I've decided to use it instead. It's much faster than offlineimap, being based off of C++ and not python. Also, offlineimap is not going to be given new features and may eventually be discontinued as the dev mentioned on github.

Here is the new—and much improved—setup, incase anyone might benefit and for my own reference later:

~/.mbsync:
Code: Select all
# Change 'mymailname' to whatever you would like to name it
# You could get away with just changing the first 3 variables (i.e., Host, User, Pass) for a simple setup.

IMAPAccount mymailname
# Address to connect to
Host mail.myemail.com
User myemailuser@myemail.com
Pass mypass
# To store the password in an encrypted file use PassCmd instead of Pass
# PassCmd "gpg2 -q --for-your-eyes-only --no-tty -d ~/.mailpass.gpg"
#
# Use SSL
SSLType IMAPS
# The following line should work. If get certificate errors, uncomment the two following lines and read the "Troubleshooting" section.
CertificateFile /etc/ssl/certs/ca-certificates.crt
#CertificateFile ~/.cert/imap.gmail.com.pem
#CertificateFile ~/.cert/Equifax_Secure_CA.pem

IMAPStore mymailname-remote
Account mymailname

MaildirStore mymailname-local
Subfolders Verbatim
# The trailing "/" is important
Path ~/.mail/mymailname/
Inbox ~/.mail/mymailname/Inbox

Channel mymailname
Master :mymailname-remote:
Slave :mymailname-local:
# Exclude everything under the internal [Gmail] folder, except the interesting folders
#Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail"
# Or include everything
Patterns *
# Automatically create missing mailboxes, both locally and on the server
Create Both
# Save the synchronization state files in the relevant directory
SyncState *


To check the mail (could be run by a cron job):

checkmail.sh:
Code: Select all
#!/bin/sh

mbsync -q mymailname

# Count all the new files in the 'new' directory
newMail=$(find ~/.mail/mymailname/Inbox/new/ -type f | wc -l)

echo "Inbox:"$newMail

if [ $newMail -gt 0 ]; then

   # Add an 's' if more than 1 email
   if [ $newMail -gt 1 ]; then addTheS="s"; fi

   notify-send -t 0 "<span color='#ffffff' font='26px' face='mono'><b>Your inbox now has "$newMail" email$addTheS!</b></span>"
   beep

   # Uncomment if you'd like to clear the cache so that this notification doesn't keep running
   #rm -f ~/.mail/mymailname/Inbox/new/*

fi


Point mutt to that mailbox so that you can instantly load the cached email:

readmail.sh:
Code: Select all
#!/bin/sh

urxvt -e mutt -f ~/.mail/mymailname/Inbox/


As you view the new email, mutt will automatically take the viewed emails off the list, lowering the email count; there is no need to reset the count manually. :)
OS: Debian GNU/Linux (Buster—testing); Kernel: 4.16.0-2-amd64; Shell: sh; WM: i3
Machine: HP ProDesk 600 G3—i7-7700, 8GB RAM, 256 SSD, Intel HD Graphics 630

bedtime
 
Posts: 93
Joined: 2012-12-16 19:34


Return to System configuration

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable