Lesson 18 - Confirming email to requester
Actions
 |
Click to see Lesson18_Confirmation.php in action on this tutorial site. Note: if you enter name an email in this form, you will send me an email. |
Download this file: Lesson18_Confirmation.txt to your PC. Change the txt extension to php. Change the recipient address in the Lesson18 file and the pointers to the style sheet and then upload all to your website. The thank yous should be there from the previous lesson.
Surf to the Lesson18_Confirmation.php file on your website with your browser. You should see results similar to the image above on the right.
After completing the form and clicking Send me more info, you should receive an email with this in the body:
Please send me more information about Acme products
First Name = your first name
Last Name = your last name
Email = submitter@sendersdomain.com
And, submitter@sendersdomain.com should receive a confirming email.
And, you should be sent to the Thank You page.
Gotchas
None.
Source Code
Nothing really new here. We create a new message for the requester and then send them an email.
| |
<?php
ob_start(); /* Start buffer so we can use header anywhere in the page */
?>
<html>
<head>
<title>A Request Info Form with a confirming email.</title>
<link rel="stylesheet" href="/PHP_Tutorials/Styles/PHP_Tutorials_Forms_Style.css" type="text/css">
</head>
<body>
<h1>Request Information from the Acme Company</h1>
<form method="POST" name="FirstEmailForm" action="<?php echo($PHP_SELF); ?>" >
<table width="550" border="0" cellspacing="0" cellpadding="0" bgcolor="#DFDFDF">
<tr>
<td colspan="2" class="FormCategoryName">About You</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
First Name:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactFirstName" value="<?php print $_POST['contactFirstName']; ?>" >
<span class="Footer" style="color:red">* = Required</span>
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
Last Name:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactLastName" value="<?php print $_POST['contactLastName']; ?>" >
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
Email:<span style="color:red">*</span>
</td>
<td width="350">
<input type="text" name="contactEmail" value="<?php echo $_POST['contactEmail']; ?>" >
</td>
</tr>
<tr>
<td width="200" class="FormVariableName" align="right">
</td>
<td width="350">
<input type="hidden" name="firstPass" value="No">
<input type="submit" name="subRequestButton" value="Send me more info">
<br>
<span class="Footer">Please click only once.</span>
</td>
</tr>
</table>
</form>
<?php
/* Set variables equal to their "posted" values". We need to do this because "register_globals = off" is the default. */
$contactFirstName = $_POST['contactFirstName'];
$contactLastName = $_POST['contactLastName'];
$contactEmail = $_POST['contactEmail'];
$firstPass = $_POST['firstPass'];
if($firstPass == "No")
{
/* If the user skipped required fields or entered invalid values, write an appropriate error message. */
if ($contactFirstName == "")
exit("<p class='PhpError'>Your First Name is missing.</p>");
if ($contactLastName == "")
{
exit("<p class='PhpError'>Your Last Name is missing.</p>");
}
if (!(eregi("^[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,4}$",$contactEmail))):
exit("<p class='PhpError'>Email Address appears to be invalid.</p>");
endif;
/* Construct the email messages */
$messageToAcme = "Please send me more information about Acme products";
$messageToAcme .= "\n\n First Name = " . $contactFirstName;
$messageToAcme .= "\n Last Name = " . $contactLastName;
$messageToAcme .= "\n Email = " . $contactEmail;
$fullName = $contactFirstName ." " .$contactLastName;
$mailAcmeHeaders = "From: \"".$fullName."\"<".$contactEmail.">\n";
$mailAcmeHeaders .= "Reply-to: ".$contactEmail;
$messageToRequestor = "Thanks, " .$contactFirstName .", for requesting information about Acme products";
$messageToRequestor .= "\n Your email will totally ignored just like 38% of the F500 ignore emails.";
$fullName = $contactFirstName ." " .$contactLastName;
$mailRequestorHeaders = "From: \"Dave Barnes\"<dave@marketingtactics.com>\n";
$mailRequestorHeaders .= "Reply-to: " ."dave@marketingtactics.com";
/* You need to change dave@marketingtactics.com to YOUR email address */
if (mail("dave@marketingtactics.com", "Please Send Acme Info", $messageToAcme, $mailAcmeHeaders))
{
mail ($contactEmail, "Acme Request received", $messageToRequestor, $mailRequestorHeaders);
header("Location: ../../ThankYous/Thanks_Request_Info_Success.html"); /* Redirect browser */
exit; /* Make sure that code below does not get executed when we redirect. */
}
else
{
header("Location: ../../ThankYous/Thanks_Request_Info_Failure.html");
mail("webmaster@marketingtactics.com", "Acme, Request Info Failure", $messageToAcme, $mailAcmeHeaders);
exit;
}
}
?>
</body>
</html>
<?php
ob_end_flush();
?>
|
Final Thoughts
None.
|