Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question regarding Perl-language mailing script. 1

Status
Not open for further replies.

CHeighlund

Programmer
Joined
Jun 11, 2007
Messages
163
Location
US
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:

Code:
#!/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.
 
There is nothing in the code that sends an attachment. I suspect the problem is on the recieving end.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
this happens when you do not send correct headers and some mail servers see it as a blank message an attached HTML document.

i found you need to get round this by correctly forming your HTML email.

your email code should be as valid as if it was a webpage and run through W3C validator.

the code you have doesn't have a <html> tag nor a <head> or <body> tag etc. etc...

This may solve your problem as mail servers are getting more and more particular about the email they receive to combat spam.

Also MIME:Lite is starting to fail to send email if you use the default virtual SMTP server built into IIS.

Many mail servers require the 'retry check' these days, so you send an email, the receiving server goes 'ok, if you are a real email, i'm not going to accept you unless you send me it again'.

MIME:Lite and the virtual SMTP server in IIS can't handle this, so the email gets blocked.

MIME:Lite on it's own is becoming a useless module unless you pass it through a proper mail server who can handle anti-spam measures such as this re-try mechanism.







"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
1DMF, great explanation, a start for that!
 
Thank you. I believe KevinADC may be right about my own client; I'm still getting the error. But tests to others using the same system revealed that they were getting the message in the email body as intended after I swapped in the necessary html tags, so I believe the problem is solved, at least for now. Thank you all very much for your gracious assistance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top