By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,913 Members | 1,384 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to Send an email using PHP for Beginners

AutumnsDecay
100+
P: 170
Perhaps you've wanted to create a 'Request a Quote' or a 'Contact Us' form for a client you have, but didn't know how to get the information from the user to your client.

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
  1. <form action="send.php" method="post" name="contactForm">
  2.  
  3.     Name: <input type="text" name="name" />  <br />
  4.     Phone Number: <input type="text" name="phone" />  <br />
  5.     Email Address: <input type="text" name="email" />  <br />
  6.  
  7.     <input type="submit" value="Submit!" />
  8.  
  9. </form>
  10.  
The 'action' attribute in the <form> element is what will take place when the submit button is pressed. In this case, when the form is submitted, it will load 'send.php'.

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
  1. $name = $_POST['name'];
  2. $phone = $_POST['phone'];
  3. $email = $_POST['email'];
  4.  
  5. $subject = "New Contact Request Submission";
  6.  
  7. $message = "Name: " . $name . "\n\n" . 
  8.                      "Phone Number: " . $phone . "\n\n" .
  9.                      "Email Address: " . $email;
  10.  
  11. $to = "youremail@yourdomain.com";
  12. $headers = 'From: ' . $name . '<' . $email . '>' .  "\n" .
  13.                    'Reply-To: ' . $email . "\n" .
  14.                    'X-Mailer: PHP/' . phpversion();
  15.  
  16. mail($to, $subject, $message, $headers);
  17.  
  18. header("Location: thankyou.html"); 
The first thing we do in the 'send.php' file is create variables out of our POST'd content. I find it helps keep things a bit more organized.

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
Dec 27 '10 #1
Share this Article
Share on Google+