use strict; use vars qw/$cmdMail/; use LWP::MediaTypes qw(guess_media_type read_media_types); use MIME::Entity; BEGIN { $cmdMail='/usr/lib/sendmail'; # location of your sendmail read_media_types('/etc/mime.types'); # location of your mime.types file } ... sub sendmail { # returns undef or error my ($from,$to,$subject,$body,@attachments)=@_; my $ret; my $top=build MIME::Entity From => $from, 'Reply-to' => $from, To => $to, Subject => $subject, Data => $body; if (@attachments) { for my $path (@attachments) { my $type=guess_media_type($path); $top->attach( Path => $path, Type => $type, Encoding => '-SUGGEST' ); } } $top->sync_headers(Length=>'COMPUTE') if @attachments; if (open(MAIL,"| $cmdMail -t -i")) { $top->print(\*MAIL); close MAIL; } else { $ret="Mail to $to failed: $!"; } $ret; } ... my $err=sendmail('server@wacky.com','programmer@wacky.com','Your turkey is done!',"Come get some! Don't forget the beer!",'/home/programmer/Pictures/beer.jpg'); die $err if $err; ...