Solved

Email Article to friend - PART TWO (!)

Posted on 2000-04-12
15
356 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS CLI - Instances, Volumes & Tagging 2 134
troubleshoot a python script 8 78
what should i learn? javascript or typescript? 3 41
Powershell- Registry HIVE 6 27
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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 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