I want to have a input box, where the visitor can enter their email address, and then a pre-made email will be sent to them AND their email address will be saved to a text file.
#Show errors in web browser
use CGI::Carp qw(fatalsToBrowser);
# Get the input
read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
$buffer =~ tr/+/ /; # Un-Webify plus signs and %-encoding
$buffer =~ s/\r/ /g;
$buffer =~ s/\n/ /g;
@pairs = split(/&/,$buffer); # Split the name-value pairs
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}
$email=$formdata{'email'}; # Variables
$emailsToFile=">>$ENV{'DOCUMENT_ROOT'}/www/emails.txt";
# Open State Link File to Output for appending
open(INFO, "$emailsToFile");
print INFO "$email, ";
close (INFO);
print <<End_of_Doc;
<html>
<head><title>Email Confirmation</title></head>
<body>
<form method="POST" action="/cgi/formmail">
<input type=hidden name="recipient" value="$email">
<input type=hidden name="subject" value="Here's your the information you requested...">
Thank you!<BR>
Your email has been sent to $email.<BR>
</body>
</html>
End_of_Doc
Basically what I tried doing here is capturing their email address in the emails.pl file, which would then generate a list of emails (emails.txt) but then also utilize the $email address using formmail to send them an email. This isn't working. Does anyone have any ideas?
I would like to be able to do what I'm doing by using two actions in one form, but I know that isn't possible.
There may be more problems, but just at a glance you are asking the user to submit a second form to post an email.
If you can't use sendmail or Net::SMTP, then just send a redirect to the browser.
There are hundreds of good books on perl and CGI programming which you should look at for learning the basics. Or search for scripts on the web and sites like http://www.scriptsearch.com/ for example code.
In answer to your question a redirect to the browser means sending a command or HTML page back to the browser (e.g. Internet Explorer) telling the browser not to view the current page, but instantly fetch a new page, such as the URL for the formmail.
In your case do the following;
# create a new CGI object (you really should be doing this
# anyway or loading the CGI module is pointless
$query = new CGI;
# then send a noparse redirect
print $query->redirect(- uri=>'http://yourdomain.com/cgi/formmail?recipient=$email&subject=$subject',
-nph=>1);
I'm not sure if this encodes the subject/email so include
use URI::Escape;
and use the following instead;
uri_escape($email) and uri_escape($subject)
DO NOT SEND ANY OTHER HEADERS such as Content-Type or this won't work.
You can alse replace the line;
print "Content-type:text/html\n\n"; #Content Header
with
print "Location:http://yourdomain.com/cgi/formmail?recipient=$email&subject$subject";
and send back a status code to 302 in the first line of your headers e.g.
print "HTTP/1.0 302 Moved Temporarily\n";
print <<END_OF_HTML;
<HTML>
<HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<SCRIPT language="Javascript">
<!--
top.location.replace('/cgi/formmail?subject=$subject&email=$email');
// -->
</SCRIPT>
</HEAD>
<BODY>
<a href="/cgi/formmail?subject=$subject&email=$email">Please click here if you are not redirected to another page</A>
</BODY>
</HTML>
END_OF_HTML
Bookmarks