#===================================================================== # Sendmail.pm : Send a mail by sendmail command (MIME support) # # Copyright(c) N.Oishi (BSC CONSULTING CGI-Laboratory). # # e-mail : bigstone@my.email.ne.jp # support: http://www.din.or.jp/~bigstone/cgilab/index.html # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED. # # ver 1.0.1 : updated last on 2005/03/08 # #===================================================================== package Sendmail; use integer; use strict; use Symbol; use MIME; use vars qw($VERSION $MAX_RECIPIENTS); #--------------------------------------------------------------------- $MAX_RECIPIENTS = 100; #--------------------------------------------------------------------- $VERSION = '1.0.1'; sub new { my $class = shift; my $path = shift || return undef; my $mail = Symbol::gensym(); open($mail, "| $path") or return undef; return bless $mail, ref $class || $class; } sub request { my $mail = shift; my(%parm, $k, $v); while (($k, $v) = splice(@_, 0, 2)){ $parm{lc($k)} = $v; } my @recipient = (); foreach ('to', 'cc', 'bcc'){ next unless $parm{$_}; if(ref $parm{$_}){ push(@recipient, @{$parm{$_}}); } else { push(@recipient, split(/ *[,;] */, $parm{$_})); } } if(@recipient == 0 || @recipient > $MAX_RECIPIENTS){ return undef; } return undef unless $parm{from}; if(defined $parm{attach}){ if(scalar keys(%{$parm{attach}}) > 0){ foreach (values %{$parm{attach}}){ return undef unless -r $_; } } else { delete $parm{attach}; } } return undef unless($parm{content} || $parm{attach}); my $mime = MIME->new( sendmail => $mail ); my $stat = $mime->encode_message(\%parm); $mail->close(); return $stat; } sub close { my $mail = shift; close($mail) if defined fileno($mail); } sub DESTROY { shift->close(); } 1; __END__