I wrote this function a while back and it works perfectly well.
It requires the MIME::Lite module to be installed on your system - not sure if this will be a stumbling block (it certainly was at my place)
Apologies if the code doesn't follow the latest standards, it is about 3 years old and runs on Perl 5.6.1 I believe.
sub emailer($$$$$$;$);
use MIME::Lite;
sub emailer($$$$$$;$)
{
#-------------------------------------------------------------------------#
# Get incoming parameters #
#-------------------------------------------------------------------------#
my ( $exchange_svr, $to, $cc, $from, $subject, $message, $attachment ) = @_;
#-------------------------------------------------------------------------#
# create a new message to be sent in HTML format #
#-------------------------------------------------------------------------#
my $msg = MIME::Lite->new(
From => $from,
To => $to,
Cc => $cc,
Subject => $subject,
Type => 'text/html',
Data => $message
);
#-------------------------------------------------------------------------#
# Check if there is an attachment and that the file actually does exist #
# Only plain text documents are supported in this functioN. #
#-------------------------------------------------------------------------#
if ( $attachment )
{
#---------------------------------------------------------------------#
# if the attachment does not exist then show a warning #
# The email will arrive with no attachment #
#---------------------------------------------------------------------#
if ( ! -f $attachment )
{
print "WARNING - Unable to locate $attachment";
}
else
{
#-----------------------------------------------------------------#
# add the attachment #
#-----------------------------------------------------------------#
print "ATTACH", "$attachment";
$msg->attach(
Type => "text/plain",
Path => $attachment,
Disposition => "attachment"
);
}
}
#-------------------------------------------------------------------------#
# send the email #
#-------------------------------------------------------------------------#
MIME::Lite->send( 'smtp', $exch_svr, Timeout => 20 );
$msg->send() or die "SENDMAIL ERROR - Error sending email";
}
It looks like this when it is used
emailer( $exchange_server,
"someone@somewhere.com",
"someoneelse@somewhere.com",
"me@mydesk.com",
"Subject in here",
"The Message in here",
"/full/path/to/attachment" );
Optionally you can add the 7th parameter which is the attachment (you need to provide the full path) and the attachment must be a Text file (I'm sending a CSV file)
EDIT
I just re-read your post and saw that you do want to send an attachment so I added that part to the example