I've had many problems with mail clients marking email as potential spam due to changes in the RFC related to the date and time stamp functions. Moy code has to work on Win32 and Linux boxes, too, and sendmail doesn't. So I wrote the following sub that uses NET::SMTP. It auto detects the missing SASL and Base64 pods on Activestate Win32 machines so it can authenticate to remote servers.
sub SMTPMail
{
use Net::SMTP; # Not all versions of SMTO support authentication.
my ($to, $from, $subj, $msg, $smtpserver) = @_;
my $body = "";
my $error;
eval {
require MIME::Base64;
require Authen::SASL;
};
if ($@)
{
$error = system("ppm install MIME::Base64");
$error = system("ppm install Authen::SASL");
} ;
my @dayofweek = (qw(Sun Mon Tue Wed Thur Fri Sat));
my @monthnames = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov De
+c));
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime();
$year += 1900;
#Create an RFC compliant time stamp
my $Date = sprintf("%s, %02d %3s %04d %02d:%02d:%02d CST",$dayofwee
+k[$wday],$mday,$monthnames[$mon],$year,$hour,$min,$sec);
# Make $to into an array if it's not one already
if (!ref($to))
{ @$to = split(/[,;]\s*/, $to); }
# Make Email Header
$body = "MIME-Version: 1.0\n"
. "From: $from\n"
. "To: " . join(',', @$to) . "\n"
. "Date: ".$Date."\n"
. "Subject: $subj\n\n" # Double \n
. $msg . "\n";
my $smtp;
# Open a SMTP session
if(!($smtp = Net::SMTP->new($smtpserver)))
{
return "Could not connect to SMTP Server: $smtpserver ";
}
if (length($EmailAuth) > 0 && length($EmailPassword) > 0 )
{
&LogFile("Email","Auth $EmailAuth");
my $error = $smtp->auth($EmailAuth,$EmailPassword) ;
if (! $error)
{
return "Cannot authenticate as user $EmailAuth and/or b
+ad Password for Email - Error $error - Check System Settings ";
}
}
# Pass the 'from' email address, exit if error
if (! ($smtp->mail( $from ) ) ) { return "Bad 'From', check your Ac
+count Information"; }
# Pass the recipient address(es)
if (!($smtp->recipient(@$to))) { return "Bad 'EmailTo'"; }
# Send the msg
$smtp->data( $body );
$smtp->quit;
return 0;
}
|