[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

Email Article to friend - PART TWO (!)

Hello everyone.  The following is the code provided to my earlier CGI question. It works fine.  But I would like to know if insteasd of embedding the text file which is to be emailed to the recipient within the cgi file, if the file name could be included in the text submit form on the HTML page.  (The file name that I would like to keep in the form -- in a hidden field (and not in the cgi bin is "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt")

That way I could keep one emailing program in my cgi bin and not have to modify it every time I add a new text file to be emailed.  This particular web site will be adding emailed articles every few days and I'd rather not have to create a new CGI file for every article.

Again, here is the script by which works nicely.  

#! /usr/local/bin/perl
use CGI;

$q=new CGI;
$friendname=$q->param("friend_name");
$friendmail=$q->param("friend_mail");
$sendername=$q->param("sender_name");
$sendermail=$q->param("sender_mail");
$return_to=$ENV{'HTTP_REFERER'};    
$articlename = "I asked M.D. Sutton to send you this Informative article";

#The article name can be passed as hidden field.
$filePath = "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt";
# the absolute path to the file.
open (FILE, $filePath) || die "cant open $filePath...$!";
while (<FILE>)
{
  $articlebody .= $_;
}
close (FILE);

$mail_program = "/usr/sbin/sendmail -t";
open (MAIL, "|$mail_program") || die ("Could Not Open Mail Program");
print MAIL "To: $friendname <$friendmail>\n";
print MAIL "From: $sendername <$sendermail>\n";
print MAIL "Subject: $articlename\n\n";
print MAIL $articlebody;
close (MAIL);

print "Content-type: text/html\n\n";
open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant open the html file";
@Success_Html = <HTML_FILE>;
close(HTML_FILE);

foreach (@Success_Html)
{
  s/PREV\_PAGE\_LINK/$return_to/g;
  print $_;
}


0
Rowby Goren
Asked:
Rowby Goren
  • 8
  • 4
  • 3
1 Solution
 
maneshrCommented:
you can do that as follows.

1) declare a hidden variables in your HTML from which has the filename in it.

<input type=hidden name=fp value="/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt">

Next make the following changes to your CGI program.

$filePath=$q->param("fp");

Comment out ...
##$filePath = "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt";

And you are ready to go.

if you dont want to store the entire path to your file in the HTML variable you could send the relative path and create the full path in the CGI script as follows...

<input type=hidden name=fp value="email_articles/test-article.txt">

$filePath="/mnt/web/guide/mdsutton/cgibin/".$q->param("fp");

Again Comment out ...
##$filePath = "/mnt/web/guide/mdsutton/cgibin/email_articles/test-article.txt";


Hope that helps
0
 
Rowby GorenAuthor Commented:
Hey, thanks,

Looks simple enough.

I assume I can also do:

<input type=hidden name=fp value="test-article.txt">

$filePath="/mnt/web/guide/mdsutton/cgibin/email_articles/".$q->param("fp");



0
 
maneshrCommented:
definately...

that would be the best thing to do, since you are hiding the entire path to the file!!

Rgds
0
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
Rowby GorenAuthor Commented:
Great  Thanks.  

Now I'll add another fifty points if you could tell me how to modify the code so that instead of emailing the text file, when you push the button, it prints it out on the person's computer.  (Hopefully at least compatible with Browsers 4 and newer. )

Thanks
0
 
maneshrCommented:
so if i understand you correctly, you dont want to send out the contents of the "hidden" text file as email, but want to instead print it on the screen, right??

if that is indeed the case you need to modify your code as follows...

$filePath="/mnt/web/guide/mdsutton/cgibin/email_articles/".$q->param("fp");
.............................

## Comment out all the following lines...

#$mail_program = "/usr/sbin/sendmail -t";
#open (MAIL, "|$mail_program") || die ("Could Not Open Mail Program");
#print MAIL "To: $friendname <$friendmail>\n";
#print MAIL "From: $sendername <$sendermail>\n";
#print MAIL "Subject: $articlename\n\n";
#print MAIL $articlebody;
#close (MAIL);


print "Content-type: text/html\n\n";

## Print the content of the text file to the screen
print $articlebody;

open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant
                    open the html file";
                    @Success_Html = <HTML_FILE>;
                    close(HTML_FILE);

...............


That should do it.

Let me know how it goes.
0
 
Rowby GorenAuthor Commented:
Sorry, I want the text file to print out on the person's home printer -- not on the screen.

And it needs to be compatible with at least level 4 or newer browsers.

If additional modification is too different from my original question, and requires a separate question let me know -- and I'll award the current points.

Thanks


Rowby
0
 
maneshrCommented:
i dont think you can do that using PERL.

i would suggest that you post this question to the Javascript forum(
http://www.experts-exchange.com/Computers/Programming/Languages/JavaScript/). There you might get an answer.

Rgds

0
 
Rowby GorenAuthor Commented:
Thanks!

Rowby
0
 
Rowby GorenAuthor Commented:
Ooops,  is it too late to request a clarification.


I get the following error message in my error log:

headers: /web/guide/mdsutton/cgibin/email_articles/email-article.cgi
[Sat Apr 15 11:34:12 2000] [error] [client 128.227.141.205] File does not exist

When I use this version of the script:

#! /usr/local/bin/perl
use CGI;

$q=new CGI;
$friendname=$q->param("friend_name");
$friendmail=$q->param("friend_mail");
$sendername=$q->param("sender_name");
$sendermail=$q->param("sender_mail");
$return_to=$ENV{'HTTP_REFERER'};      
$articlename = "I asked M.D. Sutton to send you this Informative article";

#The article name can be passed as hidden field.
$filePath="/mnt/web/guide/mdsutton/wwwfp/".$q->param("fp");
# the absolute path to the file.
open (FILE, $filePath) || die "cant open $filePath...$!";
while (<FILE>)
{
  $articlebody .= $_;
}
close (FILE);

$mail_program = "/usr/sbin/sendmail -t";
open (MAIL, "|$mail_program") || die ("Could Not Open Mail Program");
print MAIL "To: $friendname <$friendmail>\n";
print MAIL "From: $sendername <$sendermail>\n";
print MAIL "Subject: $articlename\n\n";
print MAIL $articlebody;
close (MAIL);

print "Content-type: text/html\n\n";
open (HTML_FILE, "/mnt/web/guide/mdsutton/cgibin/email_articles/success_msg.html") || die "cant open the html file";
@Success_Html = <HTML_FILE>;
close(HTML_FILE);

foreach (@Success_Html)
{
  s/PREV\_PAGE\_LINK/$return_to/g;
  print $_;
}



0
 
Rowby GorenAuthor Commented:
Here is what I have in my form in the html page:

<input type=hidden name=fp value="email_articles/test-article.txt"> </form>
0
 
lambdaCommented:
/web/guide/mdsutton/cgibin/email_articles/email-article.cgi

Is this the correct path & name of the cgi file?


£.
0
 
lambdaCommented:
/mnt/web/guide/mdsutton/wwwfp/email_articles/test-article.txt

And is this the correct path & name of the text file?


£.
0
 
Rowby GorenAuthor Commented:
The path to the text file is: (did a cut and paste in my ftp program:

/mnt/web/guide/mdsutton/cgibin/email_articles

The article name (again a cut and paste from within the email_article directory:

test-article.txt

And it works fine when I don't include the subdirectory in the perl script and include it in the HTML form script.

It's only when I modified the script so that I don't have to include the subdirectory in the HTML form (to save time and errors every time I add a new *.txt file that this pops up.

Note how the error log says that the script seems to change the text file name to:  "test-article.cgi"  instead of test-article.txt

And again, here is a cut and paste of the relevent HTML code:

<input type=submit name="Send" value="CLICK to email!">
    </p>
</td></tr> </table>   <input type=hidden name=fp value="email_articles/test-article.txt">


0
 
lambdaCommented:

In ur comment posted on April 12 2000 - 09:15PM PDT, u had given the path as...

$filePath="/mnt/web/guide/mdsutton/cgibin/email_articles/".$q->param("fp");

And in the comment posted on April 15 2000 - 08:43AM PDT, it is


$filePath="/mnt/web/guide/mdsutton/wwwfp/".$q->param("fp");

I assume the first is correct.

£.
0
 
Rowby GorenAuthor Commented:
Yes.  The first is correct.  The text file is in the below directory.  Sorry.

$filePath="/mnt/web/guide/mdsutton/cgibin/email_articles/".$q->param("fp");


(In trying to resolve the error message, I was seeing if I could keep the text files out of the cgibin and in the regular section of the server (wwwfp).   (In fact I still have that text file located in the wwwfp directory, but I am no longer attempting to reference it at that location.)

Rowby

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now