Run a script when an email is received / .procmailrc

Hello there,

Following @ljf’s suggestion, I am posting here a way of enabling the execution of a (bash) script when an email is received by a specific user.

Outside the Yunohost environment I would normally use a local .procmailrc file. However, it seems that the default Yunohost does not use Procmail.

We thus need to use dovecot/sieve to perform this task. Here is what I did for my system. However, I am not a Dovecot expert at all so any improvement/remarks is welcome!

Enable sieve and the sieve_extprograms extension

In /etc/dovecot/dovecot.conf, there is a plugin {...} section that already contains sieve-related parameters. I had to update it like that:

  sieve_extensions = +vnd.dovecot.execute # say we enable the "execute" command
  sieve_plugins = sieve_extprograms
  sieve = /var/mail/sievescript/%n/.dovecot.sieve # a script to be executed each time we receive an email (%n is for the username)
  sieve_execute_socket_dir = sieve-execute # required for exectue to work
  sieve_execute_bin_dir = /var/mail/sievescript/%n/scripts/ # the path where sieve scripts can be executed 
  sieve_dir = /var/mail/sievescript/%n/scripts/ # the path where sieve scripts can be executed 
  sieve_before = /etc/dovecot/global_script/
}

You need to create the empty /var/mail/sievescript/ and /var/mail/sievescript/%n/scripts/ directories (replace %n with a valid username).

Create a .dovecot.sieve script

This script follows the sieve syntax

.dovecot.sieve

require ["vnd.dovecot.execute"]; # load the appropriate module
execute :pipe "test.sh"; # relative to /var/mail/sievescript/%n/scripts

test.sh: this file should have exec (+x) properties. The email to process is piped to test.sh. Note that file permissions are very restrictive, and many information is logged in /var/log/mail

Some useful references.

I hope this can help!

1 Like