Mail 550 filter

If you make a project with huge amount of email notification, you MUST control number of 550 reply from mail servers. Because if you skip this step, and continue sending to deleted mail boxes, big mail providers such as gmail.com, mail.ru, mail.ua, etc. will ban you domain at 0.5 – 1% “user unknown” reply.
So mail.log parsing is only solution.
In our project we add bad email addresses to database table (we use postgress)

1. make database replace rule, if email already added, (email is primary key) it is fastest way to prevent errors on INSERT duplicate email addresses.

CREATE OR REPLACE RULE email_stop_list_ignore_duplicate_inserts AS
ON INSERT TO email_stop_list
WHERE (EXISTS ( SELECT 1
FROM email_stop_list
WHERE email_stop_list.esl_address = NEW.esl_address)) DO INSTEAD NOTHING;

2. Parse your postfix (yes, I prefere postfix) logs
you can do daemon
or just sleep in while true loop at screen, up 2 u 🙂

screen -S roboban
while true; do ./roboban; sleep 10; done

it`s doesn`t metter much I prefere demonize process.

3. Script

#!/bin/bash


while true; do

tmpfile=$(mktemp /tmp/`basename $0`.XXXXXX) || exit 1

>$tmpfile

tail -n 2000 /var/log/mail.log | grep 'said: 550' | awk -F'<' '{print $2}' | awk -F'>' '{print $1}' >> $tmpfile

for i in `cat $tmpfile`; do

#echo $i; #debug
#For mysql:
#/usr/bin/mysql -h db1-2.it.randomthemes.com -u USERNAME -pPASSWORD -e "INSERT INTO egenerator.generator_mail_stoplist SET email='$i';"

#For postgress
psql -U dbuser -h dbase3.it.randomthemes.com -d database  -c "insert into email_stop_list (esl_address, esl_created, esl_reason) values ('$i', DEFAULT, 'said 505 not exist')"

done

rm -f $tmpfile

sleep 30

done

4. How to run it as daemon:
Add following string to your /etc/rc.local

/sbin/start-stop-daemon  --start --quiet --make-pidfile  --pidfile /var/run/roboban-egen.pid --background --exec /home/scripts/roboban-egen.sh

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>