Script to identify the reason for bounced back emails

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

======

 

 

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top