Using PHP's mail() function, we take the user's inputted data and send it to our client's email address with minimal effort.
Let's assume we have a contact form with the following markup:
Expand|Select|Wrap|Line Numbers
- <form action="send.php" method="post" name="contactForm">
- Name: <input type="text" name="name" /> <br />
- Phone Number: <input type="text" name="phone" /> <br />
- Email Address: <input type="text" name="email" /> <br />
- <input type="submit" value="Submit!" />
- </form>
The 'method' attribute is the way the form's data will be sent to the action page (in our case: send.php). There are two options:
1) POST - All the name value pairs are submitted in the Message Body of the request, which is not seen in the location bar of the browser. POST strings have no limitations in size.
2) GET - All name values pairs are entered as a string into the browser's location bar. GET strings are limited in size and characters available for usage when the form is submitted.
Summarizing, POST is the secure method of sending data from a form. It should be used when handling sensitive information such as passwords, creditcard numbers, email addresses, etc. GET is less secure, but is widely used in CMS designs dealing with products and categories. Typically seen in online store usage when browsing products.
The only other important thing about our form at this time is making sure all of our text fields have unique 'name' attributes. Use relevant naming conventions to keep it simple.
Now, once the form is submitted, the values the user entered into the form are sent to 'send.php' using the 'post' method. Our PHP script will have the following:
Expand|Select|Wrap|Line Numbers
- $name = $_POST['name'];
- $phone = $_POST['phone'];
- $email = $_POST['email'];
- $subject = "New Contact Request Submission";
- $message = "Name: " . $name . "\n\n" .
- "Phone Number: " . $phone . "\n\n" .
- "Email Address: " . $email;
- $to = "youremail@yourdomain.com";
- $headers = 'From: ' . $name . '<' . $email . '>' . "\n" .
- 'Reply-To: ' . $email . "\n" .
- 'X-Mailer: PHP/' . phpversion();
- mail($to, $subject, $message, $headers);
- header("Location: thankyou.html");
The $subject variable contains the message that will appear as the subject of the email.
The $message variable contains what the body of the message will contain. In our case, it will output like this:
-----------------------------------
Name: Johnny Appleseed
Phone Number: 555-555-5555
Email Address: someone@somewhere.com
-----------------------------------
Each \n is a line break, similar to HTML's <br /> tag. YES, the '\n' IS SUPPOSED to go INSIDE the string, not outside of it, which is a common novice mistake with PHP scripters.
The $to variable contains the email address that the email will be sent to.
The $headers variable contains information about how the email was sent, what the Reply To email address is, etc.
Now, we see PHP performing the mail() function. It takes our data and sends it off to the recipient.
Finally, once the script has executed, we redirect our users to a landing page. In this example the will be taken to 'thankyou.html'.
And that's that. I hope this helps some of you gain a better grasp as to some of the fairly basic features of how a static website can gain some dynamic features
Please note that this is a basic tutorial. If you're using this in a large scale environment, or want to practice safety with your scripts, you WILL want to validate all your form's information before it is sent. You can also adjust the send.php script to require a CAPTCHA code to be entered, etc, in order to stop spam.
Regards,
Mathew Cartmill