Saturday, January 8, 2011

Distributing your outgoing mails from Multiple IP addresses

Some mail providers like hotmail, yahoo restrict the number of connections from a single IP and the number of mails that can be delivered in an hour from a single IP. To increase your ability to deliver large number of genuine emails from your users to such sites, you may want to send out mails from multiple IP addresses.

IndiMail has the ability to call a custom program instead of qmail-local(8) or qmail-remote(8). This is done by defining the environment variable QMAILLOCAL or QMAILREMOTE. qmail-remote(8) can use the environment variable OUTGOINGIP to set the IP address of the local interface when making outgoing connections. By writing a simple script and setting QMAILREMOTE environment variable pointing to this script, one can randomly chose an IP address from the control file

/var/indimail/control/outgoingip

The script below also allows you to define multiple outgoing IP addresses for a single host. e.g. you can create the control file to send out mails from multiple IPs only for the domain hotmail.com

/var/indimail/control/outgoingip.hotmail.com

Let us name the below script balance_outgoing

% su
# echo "/var/indimail/bin/balance_outgoing" > /service/qmail-send.25/variables/QMAILREMOTE
# svc -d /service/qmail-send.25
# svc -u /service/qmail-send.25
# exit
%

Finally the balance_outgoing script can be placed with execute bit in /var/indimail/bin

#!/bin/sh
# This scripts expects qmail-remote arguments on command line
# argv0          - qmail-remote
# argv1          - host   (host)
# argv2          - sender (sender)
# argv3          - qqeh   (qmail queue extra header)
# argv4          - size
# argv5 .. argvn - recipients
#
#
host=$1
sender=$2
qqeh=$3
size=$4
shift 4

cd /var/indimail
if [ " $CONTROLDIR" = " " ] ; then
    FN=/var/indimail/control/filterargs
else
    FN=$CONTROLDIR/filterargs
fi
if [ -n "$SPAMFILTER" -o -n "$FILTERARGS" -o -f $FN ] ; then
    # execute spawn-filter if you have filters defined for remote/local deliveries
    PROG="bin/spawn-filter"
else
    PROG="bin/qmail-remote"
fi
if [ " $CONTROLDIR" = " " ] ; then
    if [ -f /var/indimail/control/outgoingip.$host ] ; then
        IP=(`cat /var/indimail/control/outgoingip.$host`)
    elif [ -f /var/indimail/control/outgoingip ] ; then
        IP=(`cat /var/indimail/control/outgoingip`)
    else
        exec -a qmail-remote $PROG "$host" "$sender" "$qqeh" $size $*
    fi
else
    if [ -f $CONTROLDIR/outgoingip.$host ] ; then
        IP=(`cat $CONTROLDIR/outgoingip.$host`)
    elif [ -f $CONTROLDIR/outgoingip ] ; then
        IP=(`cat $CONTROLDIR/outgoingip`)
    else
        exec -a qmail-remote $PROG "$host" "$sender" "$qqeh" $size $*
    fi
fi
IP_COUNT=${#IP[*]}
if [ $IP_COUNT -gt 1 ] ; then
    i=`expr $RANDOM % $IP_COUNT`
    export OUTGOINGIP=${IP[$i]}
fi
exec -a qmail-remote $PROG "$host" "$sender" "$qqeh" $size $*

No comments:

IndiMail Queue Mechanism

Indimail has the ability of configuring multiple local and remote queues. A queue is a location on your hard disk where email are deposited ...