I’m trying to set up a Perl script to send out a multipart email consisting of text and html. I’m using Perl and the Mime::Lite module.
The script that I have is as follows:
#!/usr/bin/perl
use strict;
use warnings;
use MIME::Lite;
use Net::SMTP;
my $msg;
my $MAIL_RELAY = 'my.mail.relay.com';
my $MAIL_TIMEOUT = 60;
my $text;
my $html;
my $from = 'from@me.com';
my $to = 'to@me.com';
$text = "Mary had a little lamb with mint jelly";
$html = '<table><tr><td>cell 1</td><td>cell2</td></tr><tr><td>Cell 3</td><td>Cell 4</td></tr></table>';
$msg = MIME::Lite->new(
From =>$from,
To =>$to,
Subject =>'test',
Type =>'multipart/alternative',
);
## text portion
$msg->attach(Type =>'text/plain',
Data =>$text
);
## html portion
$msg->attach(Type =>'text/html',
Data =>$html
);
my $smtp;
for (my $counter=1; $counter<=3; $counter++) {
$smtp = Net::SMTP->new($MAIL_RELAY);
if (defined $smtp) {last}
else {warn "=>Could not connect to mail host \"$MAIL_RELAY\" on attempt No. $counter. Info: $!
" };
sleep 5; # wait a few seconds before trying again
}
## if a valid connection to a mail server exists, email the log file
if ($smtp) {
MIME::Lite->send('smtp', $MAIL_RELAY, Timeout=>$MAIL_TIMEOUT) || die "Could not send mail!!";
$msg->send || die "Could not send mail!!";
}
The problem is that it’s not sending both parts of the message. In fact, it’s only sending the HTML version, not the text. The proof of this is that if I switch around the two $msg->attach statements, then I only get the text part. I’ve been at this and cannot figure out what I’m doing wrong. Can anyone here help me? Please?
Zev Steinhardt