?
Solved

Email Article to friend - PART TWO (!)

Posted on 2000-04-12
15
Medium Priority
?
424 Views
Last Modified: 2013-12-25
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
Comment
Question by:Rowby Goren
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
15 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 600 total points
ID: 2710058
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
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2710489
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2711944
definately...

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

Rgds
0
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
LVL 9

Author Comment

by:Rowby Goren
ID: 2714427
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2716098
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
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2716537
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
 
LVL 16

Expert Comment

by:maneshr
ID: 2716772
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
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2719087
Thanks!

Rowby
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2719123
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
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2719126
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
 
LVL 1

Expert Comment

by:lambda
ID: 2719241
/web/guide/mdsutton/cgibin/email_articles/email-article.cgi

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


£.
0
 
LVL 1

Expert Comment

by:lambda
ID: 2719247
/mnt/web/guide/mdsutton/wwwfp/email_articles/test-article.txt

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


£.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2719264
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
 
LVL 1

Expert Comment

by:lambda
ID: 2719280

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
 
LVL 9

Author Comment

by:Rowby Goren
ID: 2719290
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question