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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.