#!/usr/bin/perl -w # ########################################################################## # # # sendmail, a perl-replacement for the original, it just collects the # # data, and sends them to the specified eMailserver. # # diposition to enable mailing for servers, who don't have a dinosaur :) # # # # No relay by smtp possible with this script! # # No commands the original knows are recognized! # # # # put it to /usr/lib/sendmail, the standard place for the original or # # the place it is expected to satisfy other applications. # # # ########################################################################## use Net::SMTP; # module which does the mailing # has to be installed use strict; my @textdata = ; # fetch incoming data undef my $hclf; # variables initialised for use strict my $textdata; # other mys are spread over the script my $loop; my @loop; undef my $mail_to; undef my $mail_subject; undef my $mail_from; my $mail_text; ############################ # # # user setting start here: # $mailhost = "host.domain.where"; # the mailserver, which delivers mail $maileruser = "username\@domain.where"; # the user, who is allowed to use the mailserver # the user, who gets errormessages, too #$hclf = "/tmp/sendmail.log" # file, where to pipe all mailcontent at. # use only for debugging! # if used by cgi, webserver's user needs # rights to write there (apache). #my $mdebug = "1"; # debug the module on stdout, default value is "1" # higher values are possible but not useful my $mdebug = "0"; # don't debug the module on stdout # user settings stop here! # # # ############################ if ($hclf) { open (GUN,">>$hclf") || die "cant write to $hclf"; print GUN scalar localtime(); } undef my $counter; # init helpers, just to be safe undef my $body; foreach $textdata (@textdata) { print GUN $textdata if $hclf; if ($counter < 3) { $loop[$counter] = $textdata; # collect "To:", "From:" and "Subject:" # "To:" and "From:" are required, # "Subject:" is optional, see later } else { $body = $body . $textdata; # collect bodytext } $counter++; } print GUN "\n###################################\n" if $hclf; if ($hclf) { close (GUN) || die "cant close $hclf"; } undef my $count; for my $i (0 .. 2) { if ($loop[$i] =~ /to:\s*([\w\W]*)/i) { # case insensitive is required here! $mail_to = $1; # to match all clients using this :) $count++; } if ($loop[$i] =~ /subject:\s*([\w\W]*)/i) { $mail_subject = $1; $count++; } if ($loop[$i] =~ /from:\s*([\w\W]*)/i) { $mail_from = $1; $count++; } } if ( # cases, if mail is undeliverable (! $mail_to) or # if so, alarm defined $maileruser (! $mail_from) ) { my $whereat = `/bin/uname -n`; chomp $whereat; $mail_to = $maileruser; $mail_from = "perlsendmail <$maileruser>"; $mail_subject = "perlsendmail has found an error at $whereat!"; $mail_text = "0: $loop[0]1: $loop[1]2: $loop[2]$body"; } else { # if ok, init remaining vars if ($count lt 3) { $mail_text = $loop[2] } $mail_text = $mail_text . $body; $mail_subject = "\n" if (! $mail_subject); # catch empty subjects even not given # any other header informatin should # be at the top of the $body } my $time = "Date: " . localtime(); chomp $mail_from; chomp $mail_to; chomp $mail_subject; my $smtp = Net::SMTP->new("$mailhost", Debug => $mdebug, ); $smtp->mail("$mail_from"); $smtp->to("$mail_to"); $smtp->data(); $smtp->datasend("From: $mail_from $time To: $mail_to Subject: $mail_subject $mail_text "); $smtp->quit; exit; # bye-bye, baby :)