Sometimes, we need the mail logs to find out the reason why the mails has been bounced back. The following script will help you to find the same. Actually, this type of script needed for the server which is used for email marketing application.

 

As most of the email marketing servers are using postfix to send out mails, this script will be more suitable for postfix servers.

 

======

#!/bin/bash

#######################

#Script for sending Bounced Back mail address

#Created By Dhanasekaran

########################

NOW=`date | cut -f2,3,4 -d’ ‘| cut -d: -f -1,2`

PREV=`cat /root/.last_run`

sed -n “/$PREV/,/$NOW/p” /var/log/maillog |grep -i status | grep bounce > /root/.mail_log

for i in `cat /root/.mail_log|cut -d = -f2|sed ‘s/relay//g’|sed ‘s/<//g’|sed ‘s/>\,//g’|grep -v master`; do echo $i;cat /root/.mail_log|grep $i|awk -F ‘bounced’ ‘{print $2}’;echo “=============”; echo; done>/root/bouncedmails.txt

##########################

#Sending Notication mail to client email address

##########################

LENGTH=`cat /root/bouncedmails.txt|wc -l`

if [ $LENGTH >=1 ]; then

echo “Bounced Back email address list from `hostname`”| mutt -s “Bounced back email addresses from `hostname`” youremail@domain.tld  -a /root/bouncedmails.txt

fi

echo $NOW>/root/.last_run

======

 

 

Leave a Reply