#!/usr/bin/perl ############################################################ ## ## Mail Latency - Send Out To In Test Email ## ## v1.0 - 10/15/2007 - Walt Howd ## ## This script is designed to send email messages from an off ## campus mail server to a local mail server. ## ## Alternatively you can use a local mail server to send mail ## to an off site address that is configured to bounce mail back ## to a local mailserver. ## ## Another script will check for these messages and measure ## latency. ## ## Copyright Walt Howd ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ## ############################################### ############################################################ ############################################### ## Configuration Variables ############################################### @serverList = qw( mail spam-firewall ); $dnsDomain = "domain.com"; @modes = qw( absolute relative ); $mailTo = 'MailLatencyTest@gmail.com'; $mailServer = 'mail.domain.com'; $mailSubject = 'MailLatency'; $mailFrom = 'username'; $LOG = "/var/log/emailout.log"; ############################################### ## Pick a username and connect ############################################### login(); ############################################### ## Send out message ############################################### sendMessages(); sub sendMessages { SERVER: foreach (@serverList) { $server = $_; foreach (@modes) { eval { $mode = $_; print "Sending message from $mailFrom - $mailSubject - $server - $mode\n"; $smtp->mail($mailFrom); $smtp->to($mailTo); $smtp->data(); $smtp->datasend("Subject: $mailSubject - $server - $mode\n"); $smtp->datasend("From: $mailFrom\n"); $smtp->datasend("To: $mailTo\n"); $smtp->datasend("\n"); $smtp->datasend(time()); $smtp->dataend(); }; ############################################### ## Catch any errors ############################################### if($@) { $error = $@; chomp($error); ############################################### ## Log errors then exit ############################################### bailOut("Problem sending message - $mailFrom - $error"); } } } } $smtp->quit; ############################################################ # Print closing banner ############################################################ print "SUCCESS - All test messages sent. . . \n"; print "Done\n"; appendfile($LOG, returndate() . " - SUCCESS - Sent test messages from $mailFrom"); ############################################################ # Shared Functions ############################################################ sub returndate { ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time); $date = sprintf "%4d-%02d-%02d %02d:%02d:%02d",$year+1900,$mon+1,$mday,$hour,$min,$sec; return($date); } sub bailOut { my ($message) = @_; appendfile($LOG, returndate() . " - ERROR - $message"); print "$message\n"; die(); } sub appendfile { my ($fp, $msg) = @_; if (open(FILE, ">>$fp")) { print FILE ("$msg\n"); close FILE; } } sub login { use Net::SMTP; $smtp = Net::SMTP->new( $mailServer, Hello => $dnsDomain ); if($@) { $error = $@; chomp($error); bailOut("Could not create connection to $mailServer as $mailFrom - $error"); } }