www.webdeveloper.com
Results 1 to 5 of 5

Thread: save and send email with form

  1. #1
    Join Date
    Apr 2003
    Posts
    6

    save and send email with form

    So here's what I'm trying to do...

    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.

    So far here is what I have...

    Here's what's in the HTML page...

    <form method="POST" action="/cgi-bin/emails.pl">
    e-mail : <input name="email" size="44" maxlength="100">
    <input type="submit" value="Submit" id="submit1" name="submit1">


    Here's what's in emails.pl

    #!/usr/bin/perl
    #emails.pl

    print "Content-type:text/html\n\n"; #Content Header

    #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.

  2. #2
    Join Date
    May 2003
    Location
    England
    Posts
    7
    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.

  3. #3
    Join Date
    Apr 2003
    Posts
    6
    "then just send a redirect to the browser."

    What exactly does this mean, and how do I go about doing this? Thanks for your help!

  4. #4
    Join Date
    May 2003
    Location
    England
    Posts
    7
    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";

  5. #5
    Join Date
    May 2003
    Location
    England
    Posts
    7

    HTTP Redirect

    Or with HTTP and Javascript.

    print "Content-Type: text/html\n\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

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles