I am trying to program a work-related report system. The reports are generated in php, and I have been requested to have the results sent via email to the individual who needs to review them. (That is, one person generates the reports to look at via an external program, but another person also needs to review them and gets the results in email.)

I found a mailing script we use for something else, and thought I'd managed to modify it for my purposes, but I keep running into a rather strange problem. I'm trying to send an html message to the user's inbox; instead of being the body of the email, the message is being added as an attachment. The original program I modified was designed to send a message and a file; I thought I'd gotten rid of the file-sending portion, but apparently I am incorrect in this.

The code below is a copy of the script I'm using:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use Net::SMTP;
use MIME::Lite;


my $from_address = '******@*******.com';
my $to_address = '******@*******.com';
my $mail_host = 'mail.hiwaay.net';
my $msg;

        print "Sending email from $from_address\nto $to_address via $mail_host\n\n";

        my $subject = 'Test Report';
        my $message_body = '<TABLE BORDER=2 RULES=2 GROUPS WIDTH=90%><CAPTION><B><FONT SIZE=+1></FONT></B></CAPTION><TR><TD ALIGN=RIGHT>Company Totals</TD><TD ALIGN=RIGHT FONT SIZE=-2>Pest #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Pest $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Term #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Term $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Vent #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Vent $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Other #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Other $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Total #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Total $</TD></TR></TBODY></TABLE>';

        $msg = MIME::Lite->new (
                From => $from_address,
                To => $to_address,
                Subject => $subject,
                Type => 'text/html',
                Data => $message_body
                 ) or die "$!\n";

        MIME::Lite->send('smtp', $mail_host, Timeout=>60);
        $msg->send;

Can anyone see what I've done wrong in here, that it keeps sending the message as an attachement instead of as the email body?

Please note: the email addresses aren't listed correctly here. I starred them out since I'm testing on my own work email and want to leave as few instances of that out on the web to become spambait as possible. The actual addresses are valid.

I am trying to program a work-related report system. The reports are generated in php, and I have been requested to have the results sent via email to the individual who needs to review them. (That is, one person generates the reports to look at via an external program, but another person also needs to review them and gets the results in email.)

I found a mailing script we use for something else, and thought I'd managed to modify it for my purposes, but I keep running into a rather strange problem. I'm trying to send an html message to the user's inbox; instead of being the body of the email, the message is being added as an attachment. The original program I modified was designed to send a message and a file; I thought I'd gotten rid of the file-sending portion, but apparently I am incorrect in this.

The code below is a copy of the script I'm using:

#!/usr/bin/perl

use strict;
use warnings;
use diagnostics;
use Net::SMTP;
use MIME::Lite;


my $from_address = '******@*******.com';
my $to_address = '******@*******.com';
my $mail_host = 'mail.hiwaay.net';
my $msg;

        print "Sending email from $from_address\nto $to_address via $mail_host\n\n";

        my $subject = 'Test Report';
        my $message_body = '<TABLE BORDER=2 RULES=2 GROUPS WIDTH=90%><CAPTION><B><FONT SIZE=+1></FONT></B></CAPTION><TR><TD ALIGN=RIGHT>Company Totals</TD><TD ALIGN=RIGHT FONT SIZE=-2>Pest #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Pest $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Term #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Term $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Vent #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Vent $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Other #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Other $</TD><TD ALIGN=RIGHT FONT SIZE=-2>Total #</TD><TD ALIGN=RIGHT FONT SIZE=-2>Total $</TD></TR></TBODY></TABLE>';

        $msg = MIME::Lite->new (
                From => $from_address,
                To => $to_address,
                Subject => $subject,
                Type => 'text/html',
                Data => $message_body
                 ) or die "$!\n";

        MIME::Lite->send('smtp', $mail_host, Timeout=>60);
        $msg->send;

Can anyone see what I've done wrong in here, that it keeps sending the message as an attachement instead of as the email body?

Please note: the email addresses aren't listed correctly here. I starred them out since I'm testing on my own work email and want to leave as few instances of that out on the web to become spambait as possible. The actual addresses are valid.

I use Net::SMTP to send HTML email for a client, but you don't seem to, even though you 'use' it. Also, you don't seem to have a legal HTML document, but just a table. You need to wrap everything in HTML tags, as if it were a stand-alone HTML document.

Try a simple test of sending HTML email to yourself using something like this:

my $smtp = Net::SMTP->new('localhost');	
$smtp->data();
my $msgformat = "text/html";
$smtp->datasend("Content-Type: $msgformat\n");
$smtp->datasend("To: $e\n");
$smtp->datasend("From: $From\n");
$smtp->datasend("Subject: $Subject\n\n");
$smtp->datasend("$HeaderHTML");
$smtp->datasend("$ContentHTML");
$smtp->datasend("$FooterHTML");
$smtp->dataend();
$smtp->quit();

where you have defined the variables elsewhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.