Sending Mail via shell script

I am in need of a script that will send out emails while going through a NAT. What I have that works is as follows:

display_remote_IP | sort | uniq | while read i
do
        FOUND=0
        for IP in `echo $ACCEPTABLEIP`
        do
                if [ "$i" == "$IP" ]; then
                        FOUND=1
                        continue
                fi
        done
        if [ $FOUND -eq 0 ]; then
                echo "IP address $i NOT in acceptable IP list to access server  ......  sending email"
                echo "IP Address $i NOT in acceptable IP list to access server, please investigate" | mail -s "Unauthorized Connection To My_Server Alert" someone@mail.com
        fi
done
 
 

Now I have to run this in an environment where the mail server is not in DNS. I therefore have to go through a NAT, 100.100.100.100

How do I add that to my script so that it goes through the IP to send the email?

Short answer: you shouldn't.

Long version: it is simply not the task of a script to "know how to deliver mails". This should be the knowledge of the mailing daemon and be configured there. "mail" (which you use in your script) is a client program which acts as a frontend to this delivery system: it takes a mail message with all relevant information (addressee, content, subject line, etc.) and passes this to a daemon (sendmail, whatever), which should deliver this to an MTA (mail transfer agent) or to the addressees systems mail service directly. In any case your script should not be concerned with how mail gets there.

To use an analogy: if you want to use any other IP service (say, FTP) in your script, it shouldn't be concerned in dealing with IP routing or something such either - this would be the job of routers, so let them do it.

Bottom line: configure your "sendmail" or whatever you use to send mails properly and leave your perfectly written script alone.

I hope this helps.

bakunin

Thanks bakunin for the response. That makes a lot of sense now. I got the server guys to check that out and you were right, sendmail was not configured correctly in that environment.

Appreciate your help!!