Send email with php

In this tutorial we will go trough the process of creating a simple php code that takes the values from a html form and sends them to an email address. Let’s called it contacts form.

First we need to create a html form with few fields. We need first and last name of person, e-mail, topic of message, main text of message and as addition – department that’s going to be contacted. When submitted, our form will redirect to another page, where all the php magic will happen. We will use as method POST which is safer than GET.

<form action="sent-mail.php" method="post">
	Client Details Name:
	<input type="text" name="firstName" size="30" value="" />

	Last Name:
	<input type="text" name="lastName" size="30" value="" />

	Email:
	<input id="email" type="text" name="mail" size="30" value="" />

	Department:
	<select id="dropDeperment" name="departement">
		<option>Shopping department</option>
		<option>Technical support</option>
	</select>

	Topic:
	<input type="text" name="topic" value="" />

	<div>Message:</div>
	<textarea cols="30" name="message" rows="4"></textarea>

	<input type="Submit" value="Send messages" />
</form>

All the name attributes are the names that will help us create the php variables. When submitted, those names will be available to our sent-mail.php page trough the POST method.
To send email, we will need to use the build-in function mail(). That function will expect us to parse some variables to it. Here is how we first create our variables:

$message = $_POST['message'];
$name = $_POST['firstName'];
$lastName = $_POST['lastName'];
$department = $_POST['departement'];
$topic = $_POST['topic'];
$subject =  $department. ": " . $topic;
$from = $_POST['mail'];
$headers = $name. " " . $lastName . " <" . $from . ">";

Notice that $subject is combination of $department and $topic. It helps the receiver understand easier what’s the message all about. The $headers variable contains the To: part of an email, that’s why we put there the variables for first, last name and email.

Don’t forget to specify, to whom the message should be send.

$to = "your@mail.here";

The only thing left after we have all variables is to put them in the right way in mail() function.

mail($to, $subject, $message, $headers);

After this line, your mail function should do it’s job. You can add an echo function with message “Your message was sent” to make a better user experience. It’s also nice to keep a link to the home page, or implement everything into your web site design so the user is not hanging in the middle of nowhere.

This code cannot be tested on localhost if you don’t have mail service set up, and it’s a long story setting up one. Best way to test is to upload to web hosting somewhere. Be aware, if you refresh too many times your sent-mail.php page, your hosting provider might suspend your account for spamming.

DOWNLOAD CODE

Adding validation is a step farther in creating good email forms. At the moment anybody can submit the form without actually adding any information in it. Nobody would want that to happen and a good way to get protected is by using jQuery validation plugin.

Plug-ins for creating contacts form

It’s much easier creating contact forms if you use CMS. In WordPress the most popular plug-in is called Contact Form 7. This plug-in includes validation and is very easy to combine with Really Simple CAPTCHA to block spam bots.

You have plenty of choices for Joomla. There are many extensions, that you can download and customize to make them meet your needs. Here are some of the most popular extensions for creating forms, and more specifically for contact forms. If you find it too hard to chose an extension see what others might advice you on.

Useful links

Beautiful Contact Forms for your Inspiration

An online form builder – Wufoo

Really Simple CAPTCHA

3 Comments

    1. Menian

      Thanks! Well, I’m beginner, and this is just something small that I learned and wanted to share. There will be more tutorials in that same area, some for SQL, php and some for javascript. Stay tuned!

  1. Pingback: New home « Design А-Z

Leave a Reply

Your email address will not be published. Required fields are marked *