#!/usr/bin/perl #a wrapper to mimic sendmail #mt 07/01/2005 now added the date in gmt #mt 27/01/2006 now works with qmail #mt 31/01/2006 now parsing command line options #mt 18/02/2006 fixed bug in -t parsing #mt 20/02/2006 subject now taken from body directly # email::simple now used for dealing with headers # now supports Cc header use strict; use Net::SMTP; use Getopt::Std; #for command line options use Email::Simple; #for parsing headers ### editable options start here ### my $debug = 1; #turn on/off debugging my $thisServer = "hosting3.carpediem.fr"; #the hostname of the server this script is installed on my $smtpServer = "smtp-ext.carpediem.fr"; #the smtp server we want to connect to ### editable options end here ### my $VERSION = 2; #script version my %opts; #our options go here if($debug){ open(STDERR, ">>/tmp/sendmail.log"); print STDERR time() . "started###############################\n"; print STDERR "args: " . join(", ", @ARGV) . "\n"; foreach my $key (sort(keys %ENV)) { print STDERR "$key = " . $ENV{$key} . "\n"; } } getopt('tf:F:', \%opts); my $body = ""; #the message body read from STDIN while(){ #get the body of the message if($debug){print STDERR $_;} $_ =~ s/\r{0,1}\n{0,1}$//; #remove line endings $body .= $_ . "\r\n"; } my $email = Email::Simple->new($body); #create new email object with the body read from STDIN if(defined($opts{'f'})){ #from is specified on the command line $email->header_set("From", $opts{'f'}); if($debug){print STDERR "found from on command line '$opts{'f'}'\n";} } if(defined($opts{'F'})){ #from is specified on the command line $email->header_set("From", $opts{'F'}); if($debug){print STDERR "found from on command line '$opts{'F'}'\n";} } if(!exists($opts{'t'})){ #try and get an address from the command line my $temp = shift; if($temp){ #got address $email->header_set("To", $temp); if($debug){print STDERR "found to on the command line '$temp'\n";} } } if(!$email->header("Date")){ #if no date is set $email->header_set("Date", &date()); #set the date } my @tos = $email->header("To"); push(@tos, $email->header("Cc")); my $from = $email->header("From"); my $return = 0; foreach my $to (@tos){ #send an email for each recipient if(!sendmail($to, $from, $email->as_string)){ #mail not sent ok $return = 1; last; } } exit($return); ############################################################################### sub date{ #generate today's date my @months = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); my @days = ("Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"); my @localParts = gmtime(); $localParts[5] += 1900; #fix year for(my $c = 0; $c <= 3; $c++){ if($localParts[$c] < 10){$localParts[$c] = "0" . $localParts[$c];} #format to 2 digits } return "$days[$localParts[6]], $localParts[3] $months[$localParts[4]] $localParts[5] $localParts[2]:$localParts[1]:$localParts[0] -0000"; #7 Jan 2006 03:00:08 -0000 } ############################################################################### sub sendmail{ #send an email my($to, $from, $body) = @_; if(my $smtp = Net::SMTP->new($smtpServer, Hello => $thisServer, Debug => $debug, Timeout => 10)){ $smtp -> mail($from); #send server sending email address $smtp -> to($to); #send server recieving email address $smtp -> data(); $smtp -> datasend($body); $smtp -> dataend(); $smtp -> quit(); return 1; } else{ print "ERROR: Could not connect to SMTP server\n"; } return 0; } ###############################################################################