You are not logged in.

#1 2015-03-01 08:40:30

kristopher004
Member
Registered: 2014-06-30
Posts: 33

Offlineimap "New Email" notification scripts

I recently started using offlineimap/mutt to keep track of three email accounts. I wanted notifications when new email arrived so I tried some notification programs that worked with Maildir but didn't find any that I liked. So I came up with this script:

#!/bin/sh
MAILDIR1=$HOME/Email/maildir1/INBOX/new
MAILDIR2=$HOME/Email/maildir2/INBOX/new
MAILDIR3=$HOME/Email/maildir3/Inbox/new

echo "Watching $MAILDIR1"
while MAILNAME1=$(inotifywait -qe MOVED_TO --format '%f' $MAILDIR1) 
do
	echo $MAILNAME1
	mailpop $MAILDIR1/$MAILNAME1
done&

echo "Watching $MAILDIR2"
while MAILNAME2=$(inotifywait -qe MOVED_TO --format '%f' $MAILDIR2) 
do
	echo $MAILNAME2
	mailpop $MAILDIR2/$MAILNAME2 
done&

echo "Watching $MAILDIR3"
while MAILNAME3=$(inotifywait -qe MOVED_TO --format '%f' $MAILDIR3) 
do
	echo $MAILNAME3
	mailpop $MAILDIR3/$MAILNAME3 
done

I modified a script for procmail notifications I found here http://miek.nl/posts/2008/Jun/09/a-popu … rocmailrc/ for nicely formatted notifications and added two lines to speak the notification as well.

mailpop

#!/bin/bash

# popup a small notification with 'notify-send'

from=`formail -X From: < $1`
sub=`formail -X Subject: < $1`

# tweaks < > are special
from=${from//</\(}
from=${from//>/\)}
from=${from//&/\.}
sub=${sub//</\(}
sub=${sub//>/\)}
sub=${sub//&/\.}

sub=${sub:0:200}
from=${from:0:75}
TM=2000

/usr/bin/notify-send -u normal -t $TM "$from" "$sub"

while pgrep speech2 > /dev/null; do sleep 1; done
speech2 New Email ${from%(*}, With $sub, has arrived
#echo "New Email $from, With $sub has arrived" | festival --tts

At first I used festival for speech, but Google's voice sounded so much better. This script will speak up to 100 characters, just requires mplayer:

speech2

#!/bin/bash
say() { local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "http://translate.google.com/translate_tts?tl=en&q=$*"; }
say $*

For messages with longer subjects: http://danfountain.com/2013/03/raspberr … to-speech/

Anyone else have a script/program they like for maildir notifications? Also I would be interested in making these work as a systemd user service.

Offline

#2 2015-03-01 08:44:15

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Offlineimap "New Email" notification scripts

eml(){
  maildirs="$HOME/Mail/*/INBOX/new/"
  ml="$(find $maildirs -type f | wc -l)"
  if (( ml > 0 )); then
    echo -en "\x03$ml\x01"
  else
    echo "0"
  fi
}

Prints to my statusbar...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2015-03-01 09:00:48

kristopher004
Member
Registered: 2014-06-30
Posts: 33

Re: Offlineimap "New Email" notification scripts

jasonwryan wrote:
eml(){
  maildirs="$HOME/Mail/*/INBOX/new/"
  ml="$(find $maildirs -type f | wc -l)"
  if (( ml > 0 )); then
    echo -en "\x03$ml\x01"
  else
    echo "0"
  fi
}

Prints to my statusbar...

Nice, I'm going to use that if I can figure how to append it to i3status.

Offline

#4 2015-03-01 16:31:55

kristopher004
Member
Registered: 2014-06-30
Posts: 33

Re: Offlineimap "New Email" notification scripts

For any other i3 users, jasonwryan script works great with i3status and i3bar useing the example from i3status manual:

i3status | while :
do
	read line
	echo "Email: $(eml) | $line" || exit 1
done

Edit: This is better, works with colors:

i3status | (read line && echo $line && read line && echo $line && while :
do
  read line
  dat=$(eml)
  dat="[{ \"full_text\": \"Email: ${dat}\" },"
  echo "${line/[/$dat}" || exit 1
done)

Last edited by kristopher004 (2015-03-01 18:19:00)

Offline

#5 2015-03-01 17:16:37

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,524
Website

Re: Offlineimap "New Email" notification scripts

Using inotify would prevent needing a timed loop that either eats the cpu due to looping too fast or lags due to looping to slow.  I think inotify watches can be set up in bash, but I've only used it in C.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2015-03-01 17:18:41

kristopher004
Member
Registered: 2014-06-30
Posts: 33

Re: Offlineimap "New Email" notification scripts

I used inotify watches in mine, the while loops only run when inotifywait returns a value.

EDIT: I'm no bash wizard though, I'm sure there are better ways to use inotifywait, I just went by the examples I saw in the man page.

Last edited by kristopher004 (2015-03-01 17:21:02)

Offline

#7 2015-03-01 17:21:26

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,524
Website

Re: Offlineimap "New Email" notification scripts

Oops!  I need to learn to read more carefully.  That looks good.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2017-10-08 05:59:50

Hund
Member
From: Sweden
Registered: 2010-03-22
Posts: 479
Website

Re: Offlineimap "New Email" notification scripts

Is it possible to remove the e-mail adress from the notification and just have the name?

Offline

#9 2017-10-08 11:39:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,524
Website

Re: Offlineimap "New Email" notification scripts

Yes.  Did you make any attempt to do so?  It's also possible to start a new thread for a completely unrelated question as opposed to necrobumping something from 2 and a half years ago.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2017-10-08 11:49:23

Hund
Member
From: Sweden
Registered: 2010-03-22
Posts: 479
Website

Re: Offlineimap "New Email" notification scripts

Trilby wrote:

Yes.  Did you make any attempt to do so?  It's also possible to start a new thread for a completely unrelated question as opposed to necrobumping something from 2 and a half years ago.

Yes, but libnotify didn't really work that well with sed. `s/From://` worked, but `s/(.*)//` didn't? sad Completely unrelated would be asking about this in a thread about trains. And I understand that bumping old threads can sometimes be a bad idea, but I fail to see why I would be a bad idea now. smile

Last edited by Hund (2017-10-08 11:49:54)

Offline

#11 2017-10-08 11:57:52

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,524
Website

Re: Offlineimap "New Email" notification scripts

Your question is simply about string manipulation in the shell.  The fact that the string you want to extract from came from an email is irrelevant.  There are not special tools for extracting a substring from when the source string came from an email rather than somewhere else.  There are just shell commans for extracting substrings (${source% <*}).


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#12 2017-10-08 13:44:55

WorMzy
Forum Moderator
From: Scotland
Registered: 2010-06-16
Posts: 11,857
Website

Re: Offlineimap "New Email" notification scripts

Trilby wrote:

It's also possible to start a new thread for a completely unrelated question as opposed to necrobumping something from 2 and a half years ago.

This. Please don't necrobump Hund.

Closing.


Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD

Making lemonade from lemons since 2015.

Offline

Board footer

Powered by FluxBB