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/yourdomain.com
# Exim Filter
if $header_to: contains "[email protected]"
then
pipe "/usr/bin/procmail -Y -a /home/youraccount/.procmailrc"
endif

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/domain.com/.procmailrc
#Whatever recipes you'll use
#The order of the recipes is significant
:0 c
mailt
:0
| /home/domain/scripttoexecute.sh

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

# vi /home/domain.com/scripttoexecute.sh

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.