Lawrence Esguerra Lawrence Esguerra - 1 month ago 7
Linux Question

is it possible to have fetchmail trigger a script when it finds an unread email

I have a GMail account that is set to only receive an email with a certain subject.
I need some linux commands to trigger when it finds unread versions of this email.
The commands are just application calls so I could record my desktop while it runs.

xrandr --size 1360x768
timeout 2h recordmydesktop
xrandr --size 1366x768


I already created a filter on gmail for the email itself, but now I have no idea what to do next. I was told to set up fetchmail that would fetch unread emails in the folder I set-up.(and mark the newly fetched email as read on gmail so it wouldn't read the same mail over and over). This is the script I got, but i'm not sure if ths script does what I need it to.

poll imap.gmail.com protocol IMAP
user "l**********@gmail.com"
password '*****'
folder 'Pic*******'
fetchmail -c
keep
ssl


Next is that I was told to set up a procmail script to trigger my linux commands whenever fetchmail says that it found an unread version of the email. but I'm not exactly sure how to do it. I also need to set up cronjob to have this script trigger every few minutes.

Answer

The requirement to run X commands somewhat complicates matters. Cron is not really suitable -- even if your computer is only turned on when you are logged in, the general X architecture is based on privilege separation -- commands running in your current X session have access to the GUI, others do not.

Running Fetchmail and Procmail from within your X session is kind of awkward as well, but if you have a dedicated email account for this task, I don't suppose you mind accidentally losing an email message occasionally.

Thus, instead of a Cron job, I would suggest a simple background script running from your .xsession or similar.

#!/bin/sh
while true; do
    fetchmail -N -d0 -f $HOME/.fetchmailrc -m "/usr/bin/procmail $HOME/.procmailrc"
    sleep 600
done

The Procmail recipe could look something like

:0
* ^Subject: whatever
| xrandr --size 1360x768 ;\
  timeout 2h recordmydesktop; \
  xrandr --size 1366x768