Lesson 7 | A mail form for the Web |
Objective | Learn how to make an email Form |
$sendmail
variable with the command line for your mail-transfer program.
<!-- $filename --> <table> <!-- for medicinal purposes --> <tr> <td width=20 height=20> <td width=300> <h2>Edit Email</h2> <p>Make the changes to your email here: </table> $$form.htmlp
<!-- $filename --> <table> <tr><td width=20 height=30> <tr><td><td> <h3>Error:</h3> <p>$error <form action="$callback" method=POST> <p> <input type=submit value=" Continue "> <input type=hidden name=name value="$name"> <input type=hidden name=email value="$email"> <input type=hidden name=subject value="$subject"> <input type=hidden name=message value="$message"> <input type=hidden name=referer value="$referer"> <input type=hidden name=state value=edit> </form> <tr><td height=30> </table>
<!-- $filename --> <html> <title>$title</title> <body> <center> <img src=email.gif width=305 height=98> </center> <br clear=all>
<!-- $filename --> <table width=450> <tr> <td width=20 height=20> <td colspan=2> <h2>Email Me!</h2> <p>Here's the message you entered: <tr> <td width=20 height=20> <tr valign=top> <td width=20 height=20> <td><strong>Your Name:</strong> <td><code>$name</code> <tr valign=top> <td width=20 height=20> <td><strong>Your Email Address:</strong> <td><code>$email</code> <tr valign=top> <td width=20 height=20> <td><strong>Subject:</strong> <td><code>$subject</code> <tr valign=top> <td width=20 height=20> <td><strong>The Message:</strong> <td><code>$disp_message</code> <tr> <td width=20 height=20> <tr> <td width=20 height=20> <td colspan=2> <form action="$callback" method=POST> <input type=hidden name=name value="$name"> <input type=hidden name=email value="$email"> <input type=hidden name=subject value="$subject"> <input type=hidden name=message value="$message"> <input type=submit value=" Send It "> <input type=submit name=oops value=" Oops! "> <input type=hidden name=referer value="$referer"> <input type=hidden name=state value=sent> </form> <tr> <td width=20 height=20> </table>
$disp_message =~ s/\r\n/\n/g; #fold the cr/lf pairs $disp_message =~ s/\n{2}/<p>\n/g; #format it for the screen
\r\n
) into a single newline. The next line takes two newlines in a row, and converts them to an HTML paragraph tag. Next, we check the email address for validity. We do not really want mail from anyone who uses a bogus email address.
return error("'$email' is not a valid email address") unless ($email =~ /^[a-z][\w-.+]*\@[\w-]*[.][\w-.]*$/);
valid.htmlp
file has a useful little trick in it: there are two submit buttons:
<input type=submit value="Send It"> <input type=submit name=oops value=" Oops!">
oops
which in turn creates a Perl variable named $oops
,
which we can test for in the program:
sub mailsent { if ($oops) { mailedit } else { mailsend() or return 0; htmlhead("Email Sent"); htmlp("sent.htmlp"); htmlfoot(); } }
mailsent
routine, if the $oops
variable is defined, then the mailedit
routine
is called instead of executing the rest of the mailsent
routine. This is a convenient way of creating separate buttons to navigate different states. Finally, the mail is sent with this routine:
sub mailsend { (-x $sendmail) or return error(qq(cannot find $sendmail)); open(MAIL, "| $sendmail $switches") or return error(qq(cannot open "$sendmail $switches": $!)); print MAIL <<SENDMAIL; X-Mailer: $xmagic [$referer] To: $mailto From: $name <$email> Subject: [$subject] Referer: [$referer] Remote Host: [$remote_host] Remote Addr: [$remote_addr] User Agent: [$user_agent] $message --- This message was sent by $myname SENDMAIL close MAIL; }
mailsend
routine is a very simple routine which first checks to make sure that the mail program exists and then opens a pipe to it and sends the mail. The expression (-x $sendmail)
checks to see if the mail program can be found and is executable, and the
error()routine is used to report errors to the user so that you can be notified if the script does not work for some reason.