How to Create Email Confirmation in PHP

Sending an email confirmation to an end user is one way to communicate an aura of involvement to your customers. Whether they sign up for a newsletter or complete a survey, an email confirmation allows the end user to know that their concerns are noted and will be processed by the owner of the website.

Instructions

  1. Build Form

    • 1

      Type the following XHTML compliant code to create the foundation webpage for the form:
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
      <title></title>
      </head>
      <body>
      </body>
      </html>
      EXPLANATION
      This is the standard code for a properly compliant, XHTML document. The first line is the DOCTYPE, which follows the XHTML Transitional document type declaration. Then there is the <html> beginning tag, with the xml namesake and the two required language declarations. These two lines declare that the document is going to use the 1999 document rules for a Transitional type of XHTML document, which is easier to work with than 'Strict' XHTML. complete the required tags for a normal HTML document: the <head></head> tags, <title></title> tags, the <body></body> tags, and the end </html> tag.

    • 2

      Type the following code between the <body></body> tags to create the form, declare that the processing instructions are located in this document, and the method of transmitting the information is the 'post' method:
      <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
      </form>

    • 3

      Type the following between the <form></form> tags to create the elements of the form:
      <b>First Name:</b><br />
      <input type="text" name="firstname" size="60" /><br /><br />
      <b>Last Name:</b><br />
      <input type="text" name="lastname" size="60" /><br /><br />
      <b>Email Address:</b><br />
      <input type="text" name="email" size="60" /><br /><br />
      EXPLANATION
      Surround the labels to each form element with the <b></b> tags, which will make the text bold. Include a <br /> tag after the label to place the form element below the label. Skip two lines with two <br /> tags in between each form element.
      The form elements are the parts of the form that the user interacts with to communicate to the webmaster or owner of the website. The elements used in this example are text boxes, created by the 'input' tag. To create a text box, set the 'type' attribute to the value called 'text'. Name the form element a compound word that is close to the label to distinguish it from the other form elements during validation. Last, set the 'size' attribute to '60' characters, giving the end user plenty of room to enter their data.

    • 4

      Type the following to create the submit button:
      <input type="submit" value="Submit" name="submit" />
      EXPLANATION
      The 'type' attribute, when set to 'submit', creates a button that will execute the action of whatever the 'action' attribute equals. The 'value' attribute inscribes whatever text it is set to equal. The name, 'submit' in this case, can be any combination of letters and numbers, but must correlate to the instructions that will be covered in the second section of this tutorial.

    • 5

      Save and upload your document. The completed form should look like the attached screen shot.

    Validate Form

    • 6

      At the very top of the document, above the DOCTYPE declaration, type the opening and closing tags for PHP:
      <?php
      ?>
      EXPLANATION
      PHP processes information from top to bottom. When the submit button is clicked, the page will refresh and the instructions to process the form will be the first information that the browser and server see.

    • 7

      Type the following validation for the submit button between the opening and closing PHP tags:
      if (isset($_POST['submit'])) {
      }
      EXPLANATION
      The isset() function is used to determine if a checkbox, radio button, or submit button has been clicked. The curly braces {} contain the instructions if the statement is true. If the statement is not true, then the page will load normally. The $_POST is a special variable called a superglobal variable. If the 'method' of the form is set to 'post', then this superglobal variable is used to collect the information from the form.

    • 8

      Type the following to validate the 'firstname' text box:
      if (!empty($_POST['firstname'])) {
      $firstname = $_POST['firstname'];
      } else {
      $firstname = FALSE;
      echo "<p><font color='red'>Please enter your first name!</font></p>";
      }
      EXPLANATION
      The empty() function checks to see if the user entered any data in a text box or text area. Placing an exclamation point before the function tells PHP to check to make sure that the form field is NOT empty. If the statement is true, then create a variable by placing a dollar sign in front of the form element's name and set the variable equal to the posted information collected from the 'firstname' form field. Otherwise (else) set the 'firstname' variable to FALSE and ask the end user to enter their first name.

    • 9

      Type the following to validate the 'lastname' text box:
      if (!empty($_POST['lastname'])) {
      $lastname = $_POST['lastname'];
      } else {
      $lastname = FALSE;
      echo "<p><font color='red'>Please enter your last name!</font></p>";
      }
      EXPLANATION
      Validate the 'lastname' form field using the same process as the 'firstname' form field.

    • 10

      Type the following to validate the 'email' text box:
      if (!empty($_POST['email'])) {
      $email = $_POST[' email '];
      } else {
      $email = FALSE;
      echo "<p><font color='red'>Please enter your email address!</font></p>";
      }
      EXPLANATION
      Validate the email field the same way as the first two fields. There is a better way to validate emails, which involves using regular expressions. Since we are using the email to communicate to the customer, however, and not entering it into a database, it is not necessary to check to see if it is in a valid format. I do recommend that a regular expression is used to validate emails that go into a database, to protect against malicious end users.

    • 11

      Type the following if/else statement to make sure that all fields were entered properly before sending the email:
      if ($firstname != FALSE && $lastname != FALSE && $email != FALSE) {
      } else {
      echo "<p><font color='red'><b>The email could not be sent due to an error. See above.</b></font></p>";
      }
      Save and upload your file.

    Email Confirmation

    • 12

      Type the following between the curly braces {} of the if ($firstname != FALSE && $lastname != FALSE && $email != FALSE) statement:
      $to = $email;
      EXPLANATION
      Set the variable $email to the variable called 'to'. This will automatically harvest the posted information that the end user submitted through your form. 'To' is the first required parameter of the mail() function.

    • 13

      Type the following after the $to variable:
      $subject = "Thank you for becoming a member of 'yoursite.com'!";
      EXPLANATION
      The second required parameter of the mail function is 'subject'. Set the variable $subject to equal what the subject line of the email should be.

    • 14

      Type the following after the $subject variable:
      $body = "<html><head></head><body>Thank you for joining 'yoursite.com'!<br /><br />We have the following details that you submitted:<br /><br /><b>First Name:</b> " . $firstname . "<br /><b>Last Name:</b> " . $lastname . "<br /><b>Email Address:</b> " . $email . "<br /><br />We welcome you to your team! Please contact us if this information is inaccurate.<br /><br />Thanks you, <br /><br /> Site Owner<br />email address</body></html>";
      EXPLANATION
      The third required parameter of the mail function is 'body'. Set the variable $body equal to whatever you want to say to the customer. Be sure to include their information so the customer can verify that they typed their information correctly. You can choose not to place the HTML tags in the body. If you choose to, then Step 4 is required, if you do not wish to include the HTML tags, then skip to Step 5. Be sure to substitute the newline character (\n) within the quotation marks, instead of the <br /> tag.

    • 15

      Type the following to create the HTML headers of the email:
      $headers = "MIME-Version: 1.0\n" . "Content-type: text/html; charset=iso-8859-1\n" .
      "From: Your Company[donotreply@yourdomain.com]\n" . "Cc: Your Name[youremail@yourdomain.com]\n";
      EXPLANATION
      The MIME type and the content type establish that this email will be in HTML format. The three sections listed here: MIME, Content-type, From, and Cc are all enclosed in a pair of double quotation marks and connected to each other with a space and a period and a space. Make sure when entering the information after 'From' that you include an email address. I used a donotreply, but you can substitute it with your contact email address.

    • 16

      Type the following to send the email and display a confirmation statement:
      mail($to,$subject,$body,$headers);
      echo "<p><font color='navy'>Your information has been sent to the owner of the website! You should be receiving an email shortly!</font></p>";
      Save and upload your document.

Tips & Warnings

  • Test your validation section by just hitting the submit button without entering any data. You should have four red error messages at the top of the page.

  • Replace all of the 'yoursite' and 'yourdomain' entries with the actual information before you upload your document, or the script will not send the email.

Related Searches:

References

Resources

Comments

Related Ads

Featured