Trigger Script When Mail Received

Configuration of Mail Server for Trigger Script When Mail Received

Sometimes we need to execute any shell script or php script by mail. There is an option with mail delivery on linux server which executed by procmail. Here i am defining procmail for exim mail server which will trigger script when mail received. The script check from mailĀ  address defined from mail address and done what is defined in that script. This configuration is also tested in whm/cpanel server.

First we need to install procmail to our server

yum install procmail

Next we need to create a filter for the domain for which we want to execute the script.

vi /etc/vfilters/
# Exim Filter
if $header_to: contains "[email protected]"
pipe "/usr/bin/procmail -Y -a /home/youraccount/.procmailrc"

Above script checks that if address contains [email protected] then execute procmailrc script which means if the mail comes from [email protected] mail address then this script will execute. This condition can be change to from address,to address, subject contains or body text contains.

Now we will configure our script path in procmailrc script path.

vi /home/
#Whatever recipes you'll use
#The order of the recipes is significant
:0 c
| /home/domain/

Here we can create our script which we want to execute while receiving the mail.

# vi /home/

Here we can write our code to execute…….


Now we need to set the permission properly to execute all scripts.

mkdir /home/domain/mailt
chmod -R 777 /home/domain/mailt
chmod -R 755 /home/domain/.procmail
Create mail id for same domainname (mailscript@domain)

Now finally we can test the script by sending mail from [email protected] to mailscript@domain and test that script was executed after receiving the mail.

Balvinder Singh

Hello, I am Balvinder Singh - DevOps Engineer with 2.5+ year of working experience with different server environments. Tag Line:-Linux | AWS| WHM |Monitoring | Virtualization | Optimization | Performance | Security | Release & Deployment. I love helping companies / clients to deploy their code / applicateions to well managed, optimized, secure server and can go extra mile to satisfy.

You may also like...

Leave a Reply

Your email address will not be published.