Smtp dual delivery script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Smtp dual delivery script
# 1  
Old 10-31-2014
Smtp dual delivery script

Hello Peoples,

I have a problem wrapping my head around a script that I modified, essentially it uses the postfix smtp line in master.cf to send out a message to two or more email servers, It is a nice way to test different email servers and platforms. Problem with the original script is that it did not handle multiple recipients properly, I got that to work but now the code is all messed up, messages put in that queue sequence don't delete themselves and I feel that the use of getopt could shrink this script to 50 ish lines but I can't figure out how.
Original Script can be found on pjrlost blogspot canada (cant post links yet)
my modded script:
Code:
#!/bin/bash

MSMTP="/usr/bin/msmtp"
TMPDIR="/var/tmp/smtpdd"
TMPFILENAME="mailqfile.$RANDOM.`date +%s`.$$"
LOCKFILE="$1/.smtpdd.lck"

EX_TEMPFAIL=75

printUsage()
{
    echo "Usage: `basename $0` queue-directory sender recipient hostname|ip:port:mode [hostname|ip:port:mode ...]"
    echo "     : `basename $0` queue-directory qrun";
    echo "Where mode is q for queuing delivery, d for just drop it or o to just queue the mail - defaults to q"
}

attemptDelivery()
{
    qd=$1
    mail=$2
    host=`basename $1`
    
    from=`cat $mail|cut -f 1 -d " "`
    to=`cat $mail|cut -f 2 -d " "`
    
    hostname=`echo $host|cut -f 1 -d :`
    port=`echo $host|cut -f 2 -d :`
    
    if [ ! -f $mail.body ]
    then
        echo "Cannot delivery mail, mail body is missing"
        exit $EX_TEMPFAIL
    fi
    
    #echo "attmpting delivery of $2 from $1 as $from and $to to host $hostname with port $port"
    $MSMTP --host $hostname --port $port -f $from $to < $mail.body
    if [ $? == 0 ]
    then
        echo "Message delivered, deleting"
        rm -f $mail $mail.body
    else
        echo "Message delivery failed, leaving in place"
    fi
}

queueRun()
{
    for dirs in $1/*
    do
        if [ -d $dirs ]
        then
            old_pwd=$PWD
            cd $dirs
            for mail in *.qf
            do
                if [ -f $mail ]
                then
                    attemptDelivery "$dirs" "$mail"
                fi
            done
            cd $old_pwd
        fi
    done
}

mainRun()
{
    if [ ! -d $TMPDIR ]
    then
        mkdir $TMPDIR
        if [ $? != 0 ]
        then
            echo "Tempdirectory configuration problem with $TMPDIR - cannot create directory"
            logger -p mail.error -t smtpdd "Temp directory configuration problem with $TMPDIR - cannot create directory"
            exit $EX_TEMPFAIL
        fi
    fi
    
    chmod 0700 $TMPDIR
    
    queuedir=$1
    from=$2
    to=$3
    
    # should loop until it finds a unique filename
    while [ -f $TMPDIR/$TMPFILENAME ]
    do
        TMPFILENAME="$TMPFILENAME.$RANDOM"
        # echo "File exists already, generating new one, $TMPFILENAME"
    done
    
    cat > $TMPDIR/$TMPFILENAME
    
    for host in ${@:4}
    do
        #echo "attempting $2 to $3 for $host"
        
        hostname=`echo $host|cut -f 1 -d :`
        port=`echo $host|cut -f 2 -d :`
        mode=`echo $host|cut -f 3 -d :`
        if [ "x$mode" == "x" ]
        then
            mode="q"
        fi

        queueit="no"
        
        logger -p mail.info -t smtpdd "Attempting delivery of mail from $2 to $3 for host $hostname on $port with mode of $mode" 
        
        if [ "$mode" != "o" ]
        then
            # attempt real delivery
            $MSMTP --host $hostname --port $port -f $from $to < $TMPDIR/$TMPFILENAME > /dev/null 2>&1
            RET=$?
            if [ $RET != 0 ]
            then
                if [ $RET != "75" ]
                then
                    logger -p mail.info -t smtpdd "Mail delivery for $3 has failed"
                    echo "Mail delivery for $3 has failed (unknown user?)"
                    exit $RET
                fi
                # we failed to deliver ....
                # in queue mode, we deliver to the queue
                if [ "$mode" == "q" ]
                then
                    #echo "Delivery of mail to $hostname on $port from $from to $to has failed, but will be queued ($RET)" 
                    logger -p mail.warning -t smtpdd "Delivery of mail to $hostname on $port from $from to $to has failed, but will be queued ($RET)"
                    queueit="yes"
                fi
                
                # delete mode
                if [ "$mode" == "d" ]
                then
                    logger -p mail.info -t smtpdd "Delivery of mail to $hostname on $port from $from to $to has failed and will not be re-tried (delete mode)"
                    randomtext="asdf"
                fi
            else
                logger -p mail.info -t smtpdd "Delivery of mail to $hostname on $port from $from to $to has succeeded"
                randomtext="asdf"
            fi
        else
            echo "In queue only mode, will queue"
            logger -p mail.info -t smtpdd "In queue-only mode, will queue mail until a queue run is performed"
            queueit="yes"
        fi
        
        if [ $queueit == "yes" ]
        then
            echo "queueing"
            mkdir -p $queuedir/$hostname:$port
            i=0
            while [ -f $queuedir/$hostname:$port/mailf.$$.$i.qf ]
            do
                i=$(( $i + 1 ))
            done
            cp $TMPDIR/$TMPFILENAME $queuedir/$hostname:$port/mailf.$$.$i.qf.body
            echo $from $to > $queuedir/$hostname:$port/mailf.$$.$i.qf
        fi
    done
    
    rm -f $TMPDIR/$TMPFILENAME
}


if [ "x$1" == "x" ]
then
    printUsage
    exit $EX_TEMPFAIL
fi

if [ ! -d $1 ]
then
    echo "queue-directory specified, $1, must exist"
    exit $EX_TEMPFAIL
fi

if ! touch $1/.fml
then
    echo "queue-directory specified, $1, must exist and be writable"
    exit $EX_TEMPFAIL
else
    rm -f $1/.fml
fi

chmod 0700 $1

exec 8>> $LOCKFILE

if [ "x$2" == "xqrun" ]
then
    if flock -n -e 8
    then
        queueRun "$1"
        exit 0
    else
        logger -p mail.warning -t smtpdd "Queue directory locked, must exit from qrun"
        exit $EX_TEMPFAIL
    fi
fi

if [ "x$4" == "x" ]
then
    printUsage
    exit $EX_TEMPFAIL
fi

if flock -n -s 8
then
    mainRun $@
else
    logger -p mail.warning -t smtpdd "Cannot obtain shared lock - will not deliver right now"
    exit $EX_TEMPFAIL
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX and Linux Applications

Qmail mail delivery problem

Dear Concern, As per below article, we have configured qmail in our system. THE LINUX STUFF: qmail Installation Steps on Linux But when we try to send any mail in own domain, got below error message. Please advise. Apr 17 17:01:20 BLAUDITSCPTEST sendmail: alias database /etc/aliases... (0 Replies)
Discussion started by: makauser
0 Replies

2. Shell Programming and Scripting

Configuring smtp settings and then sending the mail through shell script

I have make an menu in which first option is to start and second is to stop the services echo "Please enter the appropriate choice for doing the operations" echo " 1) STOP Services 2) START Services case $choice in 1) ... (1 Reply)
Discussion started by: punpun66
1 Replies

3. AIX

UNIX script with smtp and telnet

hi, i have a problem , I'm creating a script that send a mail with telnet . via command line it's ok,but i create the .sh i have problem . this a .sh telnet open mysmtp 25 HELO sleep 15 MAIL FROM:<Email> sleep 15 RCPT TO:<email> sleep 15 DATA SUBJECT : PROVA (3 Replies)
Discussion started by: cescofran76
3 Replies

4. Hardware

Fedora 16 dual monitor - dual head - automatic monitor shutdown

Hi, I am experiencing troubles with dual monitors in fedora 16. During boot time both monitors are working, but when system starts one monitor automatically shut down. It happend out of the blue. Some time before when I updated system this happend but then I booted older kernel release and... (0 Replies)
Discussion started by: wakatana
0 Replies

5. UNIX for Advanced & Expert Users

Shell script to check the SAN dual path.

Hello, I would like to create a shell script which would check whether the hosts have SAN dual path or not. If yes, then I should get a success message, else should get a list of hosts on which SAN dual path is not working. Thanks.. (4 Replies)
Discussion started by: mahive
4 Replies

6. Programming

TCP/IP, how to verify delivery?

When I successfully write data to a TCP/IP socket, as I understand it, I am only guaranteed the data gets to the TCP/IP stack's buffer. However, a successful write doesn't guarantee that the data actually gets to the recipient. Since data can linger in the TCP/IP stack's buffer "indefinately," it... (7 Replies)
Discussion started by: DreamWarrior
7 Replies

7. AIX

Issues on email delivery

Hello, there is a problem when using sendmail to certain destinations, basically the recipient will reject the incoming message because the user@local.domain.com is used as the sender (Return-Path), they would verify local.domain.com is not a valid DNS record which is true because it is a local... (11 Replies)
Discussion started by: neil_is_ere
11 Replies

8. Shell Programming and Scripting

telnet smtp script

Hi, I have a mysql backup script located in crontab. I need to inform system administrator if the backup fail by telnet to smtp to send failure notification. ANyone got clue on how to achieve that? cheers. (4 Replies)
Discussion started by: bulkbiz
4 Replies

9. UNIX for Dummies Questions & Answers

Mail delivery confirmation

If I am sending mail with this command: mail .......@whatever.com < filename, is it possible to get delivery confirmation? Thanks (3 Replies)
Discussion started by: CSGUY
3 Replies
Login or Register to Ask a Question

Featured Tech Videos