homepage Welcome to WebmasterWorld Guest from 54.205.254.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Dynamic pages
uncorrect view in e-mail
specter




msg:3362420
 6:57 pm on Jun 8, 2007 (gmt 0)

Hello,

I have a perl script taht generates reports and sends them via e-mail:the problem is that when I open the mail,I see a source-view page instead of the proper html format:
What could be?

That's the code,and the way the page appears to me:

Content-type: text/html

<html><body>
<p><font face=Arial Black size=4><b>bla</b></font></p>
<p><font face=Verdana><b>Your report</b></font></p>
<p><b><font face=Verdana size=2>Today the following tasks have been executed successfully:</font></b></p>
<p><font face=Verdana size=2>bla</font></p>
<p><font face=Verdana size=2>bla</font></p>
<p><font face=Verdana size=2>bla</font></p>

I thought to the ending body and html tags missing but the code works properly if I copy/paste it in a new html page...

Thanks for any useful feedback.

Sincerely

 

rocknbil




msg:3362467
 8:29 pm on Jun 8, 2007 (gmt 0)

Well, the content-type header does not need to be printed before printing mail, You print that before output to the browser. If is part of the mail filehandle you need to remove the two newlines and just use one. I'm presuming this is a linux server.

Open up your script and look where it sends mail. Generally it opens a filehandle (MAIL below) and prints to the filehandle.

You will probably just need to add the bolded headers below. Actually the mime-version probably isn't necessary, but add it anyway. :-)

print MAIL "To: $to_email\r\n";
print MAIL "From: $from\r\n";
print MAIL "Subject: $subject\r\n";
print MAIL "MIME-Version: 1.0\r\n";
print MAIL "Content-Type: text/html; charset=US-ASCII\r\n";

print MAIL "$body";

perl_diver




msg:3364060
 5:28 am on Jun 11, 2007 (gmt 0)

He forgot to mention that the email is from a cron job. The perl script isn't sending the email, the cron job is.

rocknbil




msg:3364207
 9:46 am on Jun 11, 2007 (gmt 0)

Really,I receive the report via e-mail because the script is cron-executed daily, and the cron output is sent to the mail address I specified in the control panel.

Do you have access to the actual job being run or is it a shell script? By this I mean a cron job can run any program you point it to, so it's possible this cron job is just another perl script that needs the headers tweaked as discussed.

specter




msg:3364520
 5:22 pm on Jun 11, 2007 (gmt 0)

No.it isn't.
The cron job executes a .cgi script in wich there is the code above.
The script executes some tasks and prints a report each time it is called.
It doesn't send email autonomously:cron sends me an e-mail with the script output,that is probably intended for browsers.
Probably I need to add in that script a suitable e-mailing routine, but I don't know how to proceed...

For your convenience here is the whole script:

#!/usr/bin/perl

# localhost


# Configuration

# pswd

$password = 'blah';

full URL of script.cgi

$admin_url_base = 'http://www.blahblah.net/script.cgi';

# minimum number of pages you want to create each day.

$minpages = 1;

# maximum number of pages you want to create each day.

$maxpages = 6;

# path to the script (needed for cronning)
# because the default probably won't work.

$fullpath = '/home/user/public_html/';

##### Script begins #####

# modules

use CGI::Carp qw (fatalsToBrowser);
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
$ua = LWP::UserAgent->new;

# variables

$keywords = 'pages.txt';
$klist = '';
$admin_url = $admin_url_base . '?createpages';

# open source file to read

open (KEYS, "<$fullpath$pages");
@kd = <KEYS>;
close (KEYS);

# creates the random algo
srand();
$range = $maxpages - $minpages;
$random_number = int(rand($range)) + $minpages;

# formats the arrays

for ($a=1;$a<=$random_number;$a++){
$klist .= shift(@kd);
}
chomp($klist);

@completed = split(/\n/,$klist);

# opens source file to overwrite current status

open (KEYS, ">$fullpath$pages");
print KEYS @kd;
close (KEYS);

# uploads pages in the form

$req = (POST $admin_url,
[
'pass' => $password,
'checkdata' => '1',
'pagenames' => $klist
]);

$response = $ua->request($req);

# prints report

print "Content-type: text/html; charset=iso-8859-1\n\n";

print "<html><body>\n";

print "<p><font face=Arial Black size=4><b>mail test</b></font></p>\n";
print "<p><font face=Verdana><b>Your report</b></font></p>\n";
print "<p><b><font face=Verdana size=2>Today the following new pages have been created successfully:</font></b></p>\n";
foreach(@completed){
print "<p><font face=Verdana size=2>$_.htm</font></p>\n";
print "</body></html>\n";
}
exit;

rocknbil




msg:3364627
 7:13 pm on Jun 11, 2007 (gmt 0)

No.it isn't.

Isn't a shell script I'm presuming.

It doesn't send email autonomously:cron sends me an e-mail with the script output, that is probably intended for browsers.

I don't know how a cron would do this - grab output from a script an email it - so I'll suggest something else. My experience with a cron job or cron tab is to point it at a script in timed intervals. So I'm *presuming* this is what you are doing, and the script you've provided is what's running as a cron job.

Initially I'd prepared a revise of this script for you but I'm a little concerned about what this script actually does.
$keywords = 'pages.txt';

Additionally, I don't have the full picture:
open (KEYS, "<$fullpath$pages");

The value for $pages is never set.

Anyway what you want to do is try adding this near the top

$mail_program = '/usr/sbin/sendmail';
$send_to = 'your_email@example.com';
$send_from = 'your_email_example.com';
$mail_subj = 'Page Update Report';

And replace the line 'print "content-type . . . . ' and everything after it with this. Be sure to change out the character with an unbroken pipe, this message board software turns it into this .

open (MAIL," $mail_program -t") or die("Cannot start $mail_program $!");
print MAIL "To: $send_to\r\n";
print MAIL "From: $send_from\r\n";
print MAIL "Subject: $mail_subj\r\n";
print MAIL "MIME-Version: 1.0\r\n";
print MAIL "Content-Type: text/html; charset=US-ASCII\r\n";

print MAIL "<html><body>\n";
print MAIL "<p><font face=Arial Black size=4><b>mail test</b></font></p>\r\n";
print MAIL "<p><font face=Verdana><b>Your report</b></font></p>\r\n";
print MAIL "<p><b><font face=Verdana size=2>Today the following new pages have been created successfully:</font></b></p>\r\n";
foreach(@completed){
print MAIL "<p><font face=Verdana size=2>$_.htm</font></p>\r\n";
}
print MAIL "</body></html>\r\n";
close (MAIL);

exit;

perl_diver




msg:3364647
 7:34 pm on Jun 11, 2007 (gmt 0)

I don't know how a cron would do this - grab output from a script an email it

This is very common. He needs to tell cron to not send an email and have the perl script send the email instead. Otherwise he will continue to get the cron email and the script email. He was directed to use MIME::Lite on another forum but your suggestion looks like it could work.

specter




msg:3364781
 9:20 pm on Jun 11, 2007 (gmt 0)

I don't know how a cron would do this - grab output from a script an email it

Well,really cron simply executes the script by reading it like a browser and e-mails the result of that execution.
If I run the script via browser it is executed and prints me on screen the results.
It's that output that is sent me from cron.

Anyway,I'll try the proposed suggestion,and I'll let you know.

Thanks very much!

rocknbil




msg:3365205
 10:06 am on Jun 12, 2007 (gmt 0)

Yeah, I've never used crons to do anything but to run scripts. :-)

specter




msg:3369650
 6:52 pm on Jun 16, 2007 (gmt 0)

...well,

That's simply wonderful!

It runs fine!

Rock'n bill, your suggestion was excellent.
Now I have my daily report correctly formatted whenever I want and wherever I want.

Thanks very much for your precious help.

Sincerely

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved