#!/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 mailin +g # has to be installed use strict; my @textdata = <STDIN>; # fetch incoming data undef my $hclf; # variables initialised for us +e strict my $textdata; # other mys are spread over th +e 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 delive +rs mail $maileruser = "username\@domain.where"; # the user, who is allowed to +use the mailserver # the user, who gets errormess +ages, too #$hclf = "/tmp/sendmail.log" # file, where to pipe all mail +content at. # use only for debugging! # if used by cgi, webserver's +user needs # rights to write there (apach +e). #my $mdebug = "1"; # debug the module on stdout, +default value is "1" # higher values are possible b +ut not useful my $mdebug = "0"; # don't debug the module on st +dout # 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 saf +e 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 requir +ed, # "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 undelivera +ble (! $mail_to) or # if so, alarm defined $mailer +user (! $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 eve +n 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 :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sendmail.pl - a Sendmail replacement
by chromatic (Archbishop) on Sep 10, 2001 at 06:57 UTC | |
by khippy (Scribe) on Sep 11, 2001 at 18:27 UTC |