Solved

Email Article to friend - PART TWO (!)

Posted on 2000-04-12
15
339 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
  • 8
  • 4
  • 3
15 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 150 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction:   Welcome to my first article ever. To begin with, the reason I write this article.  I participated in a question on Experts Exchange about the start command in Windows and there were some discussion about the usage. The discussio…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now