Hi
This might help.
The code is wrapped in an eval statement and the sendmail part will print out an error, and has a timeout function.
Hopfully looking at some one elses code will help and this code is easly moved across different servers and is stable.
(This is just cut from my code so you will have to replace variables, add modules etc)
use MIME::LITE;
use strict;
use constant cYES => "yes";
# If the email has parameters that can be read
if ($valid_email eq cYES) {
eval {
# Set up the email variables
$msg = MIME::Lite->new(
From => "$msg_name <$msg_from>",
To => "$msg_to",
Subject => "$msg_subject",
Type => 'multipart/mixed'
) or die "Cannot send the email :\n$!\n";
# Zip and attach reasons file
if (defined($missing) && ($missing =~ /[A-Za-z0-9]/)) {
&attach_text($missing);
}
}
# Send the Message
MIME::Lite->send('smtp', "servername", Timeout=>300, Debug=>1)
or die "Error sending email: $!\n";
$msg->send;
}
## This sub routine compress the file for added effiency
#
sub zip_file {
# This hold the name of the datafile to compress
my $full_file = "";
# This hold the name of the compressed zip file
my $zip_file = "";
# Read in the parameter
$full_file = "@_";
# This hold the name and path of the file to compress
$zip_file = $full_file;
# Add on the .zip extension
$zip_file = $zip_file.".zip";
# Call system zip to create the compressed file the switch -q = qu
+ietly
system ("zip -q $zip_file $full_file");
# attach the zipped file
&attach_file($zip_file);
}
## This sub routine lists and attaches each file
#
sub attach_file {
# Initialise the variable to hold the file to be attached
my $attach_zip = "";
$attach_zip = "@_";
# Add the text message part
$msg->attach (
Type => 'TEXT',
Data => "\n\n\n$attach_zip\n\n\n\t",
) or die "Error adding the zip file : $!\n";
# Open the text file
open (ATTACH_FILE, "$attach_zip")
or die ("Cannot open attachment file: $attach_zip $!\n");
# Attach the text file
$msg->attach(
Type => 'BINARY',
Path => $attach_zip,
Filename => $attach_zip,
) or die "Error attaching zip file: $!\n";
# Close the atachment file
close(ATTACH_FILE);
}
};
#####################
# check out why it failed using eval
######################
if ($@) {
$error = $error."Error : The script failed to send the ema
+il.\n\n";
$error = $error."From - $msg_name <$msg_from>\n";
$error = $error."To - $msg_to\n";
$error = $error."Subject - $msg_subject\n";
$error = $error."File - $attachfile\n\n";
}
}
The code for a perl 5.8 on fedora 6 using sendmail is :
# Send the Message
MIME::Lite->send('sendmail',"/usr/sbin/sendmail -t -oi -oe
+m")
or warn "Error sending email :\n$!\n";