Click to See Complete Forum and Search --> : maintaining the same email address in a form


cedric813
11-26-2007, 05:59 PM
Hi everyone,

I have this code below that will email the contents of a web page in HTML to a person of my choosing. Right now there are 2 fields to fill in: recipient's email address and sender's email address. Since the sender's email address (my email address) will always be the same, I would like to remove this field from the form and add something to the code so that it will automatically use my email address as the sender's email address when sending the email. The only field that I would like visable is the recipient's email address field. Can anyone help me please?

Thanks,
Cedric



#!/usr/bin/perl

$scriptname = "MailPage";
$version = "2.0";

use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("$ENV{'HTTP_USER_AGENT'}");
use CGI::Carp qw(fatalsToBrowser); # Provides you with fatal error message if they occur.
use CGI qw/:standard/;
require 'mailpage.conf';
require $mtvwebdesignlib;

main: {
&getinput;
&GetCookies;

if (lc $FORM{'action'} eq "send") { &getbase($FORM{'url'}); &send; }
elsif (lc $FORM{'action'} eq "privacy") {&privacy;}
else { &moreinfo }

exit;
}


sub moreinfo {
$FORM{'url'} = $ENV{'HTTP_REFERER'} unless $FORM{'url'};
if ($FORM{'url'} !~ m/^http/ ) { $FORM{'url'} = $ENV{'HTTP_REFERER'}; }

print "Content-type: text/html\n\n";
&pageheader("Email Hits");
foreach $line (@temptoken) {
if ($line =~ m/$token/i) {
print qq~
<form action="$ENV{'SCRIPT_NAME'}" method="post">
<input type="hidden" name="action" value="send">
<input type="hidden" name="url" value="$FORM{'url'}">
<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=0 >
<TR>
<TD>Sender's Email address<input name="senderemail" value="$Cookies{'email'}"></TD>
</TR>
<TR>
<TD>Recipient's Email address<input type="text" name="recipientemail"></TD>
<td><INPUT TYPE="submit" VALUE="SEND"></td>
</TR>
</TABLE>
</BLOCKQUOTE>
</form>
~;
}
print "$line";
}
&powered_by("nologout");
exit;
}

sub send {

&check_email($FORM{'senderemail'});
&check_email($FORM{'recipientemail'});

&send_body;

&SetCookies("email",$FORM{'senderemail'});
print "Content-type: text/html\n\n";
&pageheader("Send!");
foreach $line (@temptoken) {
if ($line =~ m/$token/i) {
print qq~
<h2>Sent!</h2>
The email has been sent to $FORM{'recipient'}. Please click the back button below to return to the page you were before.<p>
<center><FORM><INPUT type="button" value="Back" onClick="history.go(-2)"></FORM></center>
~;
}
print "$line";
}
&powered_by("nologout");
$JUMP_TO = $ENV{'HTTP_REFERER'};
exit;
}


sub send_body {
$req = new HTTP::Request 'GET' => $FORM{'url'};
$res = $ua->request($req);

if ($res->is_success) {
#print "Content-type: image/gif\n\n";
#print $res->content;


open MAIL, "$mailprog" || die "Cannot open $mailprog so I cannot send email";
print MAIL "To: $FORM{'recipientemail'}\n";
print MAIL "From: $FORM{'senderemail'}\n";
print MAIL "Subject: Info Centres Advertisement Stats\n";
print MAIL "Content-type: text/html\n\n";

foreach $line ($res->content) {
if ($line =~ /\<body/ig) { $line =~ s/<body(.*?)\>/\<body $1\>$header/ig;}
if ($line =~ /\<head/ig) { $line =~ s/<head>/<head><base href=\"$base\">/ig;}
print MAIL $line;
}

close MAIL;

}
else {
&send_plain;
}
}

sub privacy {
print "Content-type: text/html\n\n";
&pageheader("Privacy Policy");
foreach $line (@temptoken) {
if ($line =~ m/$token/i) {
print qq~
<h2>Privacy Policy</h2>
~;

open FILE, $privacy_file || die "Cannot open $privacy_file";
flock(FILE, 2) if $use_flock;
@lines = <FILE>;
flock(FILE, 8) if $use_flock;
close FILE;

print @lines;

}
print "$line";
}
&powered_by("nologout");
}

sub getbase {
local($baseurl) = @_;

if ($baseurl =~ /\.html?$/i) {
@items = split(/\//,$baseurl);

$n = scalar @items;
$n--;

for($i=0;$i<$n;$i++) {
$base .= @items[$i] . "/";
}


}
else { $base = $baseurl; }
}

sub errors {
local($errors) = @_;

&expires_header("now");
print "Content-type: text/html\n\n";
&pageheader('An error occured');
foreach $line (@temptoken) {
if ($line =~ m/$token/i) {
if ($errors eq 'with the email address you gave') {
print qq~
<h2>Invalid email address</h2>
The following email address is invalid: $invalidmail<br>
Please press the back button and try again.
~;
}


# If the error type was not found:
else {
print qq~
Error Undefined! Please contact <a href="mailto:$webmasteremail?subject=Error_Undefined">$webmasteremail</a> and be detailed in what you did what caused the error.
~;
}

# Back button:
print qq~
<center><FORM><INPUT type="button" value="Back" onClick="history.go(-1)"></FORM></center>
~;
}
print "$line";
}
&powered_by("nologout");
$JUMP_TO = $ENV{'HTTP_REFERER'};
exit; # Exit the script when an error occured.
}

Nedals
11-26-2007, 08:26 PM
The easiest way to do it with the minium of code changes...

main: {
&getinput;
&GetCookies;

$FORM{'senderemail'} = 'youremailaddress'; # Add this line here

if (lc $FORM{'action'} eq "send") { &getbase($FORM{'url'}); &send; }
elsif (lc $FORM{'action'} eq "privacy") {&privacy;}
else { &moreinfo }

exit;
}


remove this...
<TR>
<TD>Sender's Email address<input name="senderemail" value="$Cookies{'email'}"></TD>
</TR>

WARNING:
I do not know what the value=$cookie{'email'} is doing nor how the &GetCookies and &SetCookies are used.
It does not appear that your script is setting cookies anywhere.

Try it and see what happens. :)
You can always revert back to your original code if there is a problem.

cedric813
11-26-2007, 10:11 PM
This worked perfectly!!!! Thanks so much for your help Nedals!!!