#!/usr/bin/perl # SSF 071710 send email example for Perlmonks use strict; use warnings; 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 ($from,$to,$subject,$body,@attachments)=@ARGV; my $err=sendmail($from,$to,$subject,$body,@attachments); die $err if $err; exit;