Skip to content

How To Trigger A Script When An Email Is Received

3 months ago

508 words

This week I needed to have a script run on a linux box whenever an email was received by a certain user. The user in my case is just a bot that is taking some action whenever a certain git repo on GitHub receives a push but this solution would work for any user on a *.nix system that needed a script to run when their account receives mail.

So first of all, if you don’t already have a user then create one: (my user will be named “robot”)

?> useradd robot

Then give the user a password with:

?> passwd robot

and enter and confirm your user’s password.

You will need to make sure that your firewall allows Port 25 inbound for incoming mail. To check this use the netstat tool:

?> netstat -an | grep 25
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN

If all you see for port 25 is the loopback ip address (127.0.0.1) then you need to append a line to your /etc/sysconfig/iptables file. Yours may look a little different than mine but just look at the other entries in your iptables for guidance. Here is the line I had to add which I basically copied from a different port in my iptables file, just changing the port number to 25.

-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 25 -j ACCEPT

Once that is added you need to save the file and then restart the iptables with:

?> /etc/rc.d/init.d/iptables restart

Now check port 25 using netstat again:

?> netstat -an | grep 25
tcp        0      0 64.128.39.175:25            0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN

We now have the loopback and the one from the outside so we are good to go.

With that all squared away, create a .forward file in your home directory and add a line to it that pipes the email to your script:

|/path/to/your/script/yourscript.sh

You also need to set the permissions on the .forward file:

?> chmod 644 .forward

Because Sendmail is restrictive about what it can run you need to add a simlink to your script in /etc/smrsh/ to enable Sendmail to access the script:

?> cd /etc/smrsh
?> ln -s /path/to/your/script/yourscript.sh yourscript.sh

Note that I am using a shell script in this example but you could use a Perl, Python, Ruby, PHP or whatever kind of script you like.

You may also need to edit your /etc/mail/sendmail.cf by finding the line that begins with Mprog and replacing P=/bin/sh with P=/usr/sbin/smrsh

You then need to restart sendmail with:

?> /etc/rc.d/init.d/sendmail restart

You should now be able to test your script by sending an email to your user’s email account. Depending on what you are trying to achieve you will probably want to parse the email with your script and then take some action, but just to test you should do something simple like have your script create a file in your home directory. This will give you a quick indication that everything is working.

Leave a Reply

Your email address will not be published. Required fields are marked *