Web Design Forum: TUTORIAL: Multiple Recipient Contact Form [PHP5] - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: Multiple Recipient Contact Form [PHP5] Rate Topic: -----

#1 User is offline   dt17 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 450
  • Joined: 22-May 09
  • Reputation: 14
  • Gender:Male
  • Location:Scotland
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 11 June 2009 - 07:17 PM

This is my first tutorial so hopefully it's not to bad. I created this to assist Badmotherz in this thread, so I hope this helps Badmotherz!

Multiple Recipient Contact Form

DEMO
All files used are attached

In this tutorial, we are going to look at a simple way of selecting a recipient for your contact form. This can be useful if you have different contacts within a small company such as Billing, Support etc. This is not the most secure way of doing this as we will be storing the recipients emails in the php file we post to, however, for people without mySQL looking for a quick way of achieving this functionality, it's ideal.

What will you need?
  • HTML knowledge
  • Server which supports PHP5 and the mail() function
  • Your favorite text editor!
Step 1: The Contact Form!
So obviously, the first thing we are going to need is a form, so create a text document and call it contactForm.html. For the sakes of this tutorial, it's going to be extremely simple with 5 fields. These 5 fields will be the Name of the sender, the senders email address, the person/department he would like to send to, the subject of the message and the message it's self. Here is the code I am going to use:

contactForm.html
<html>
<head>
<title>Multiple Recipient Contact Form</html>
</head>
<body>
	<form id="contactForm" method="post" action="[b]send.php[/b]">
		<table>
			<tr>
				<td><label for="name">Name:</label></td>
				<td><input id="name" name="name" class="tb"/></td>
			</tr>
			<tr>
				<td><label for="yourEmail">Your E-Mail:</label></td>
				<td><input id="yourEmail" name="yourEmail" class="tb"/></td>
			</tr>
			<tr>
				<td><label for="sendTo">Send To:</label></td>
				<td>
					<select id="sendTo" name="sendTo">
						<option id="dt17" value="dt17">dt17</option>
						<option id="badmotherz" value="badmotherz">badmotherz</option>
						<option id="php_penguin" value="php_penguin">php_penguin</option>
					</select>
				</td>
			</tr>
			<tr>
				<td> <label for="subject">Subject:</label></td>
				<td><input id="subject" name="subject" class="tb" /></td>
				</tr>
			<tr>
				<td><label for="message">Message:</label></td>					
				<td><textarea id="message" name="message" rows="4" cols="20"></textarea></td>
			</tr>
			<tr>
				<td></td><td><input type="submit" value="Submit"/></td>
			</tr>
		</table>
	</form>
</body>
</html>

This is simply a form inside a table which will be posted to "send.php" when the user clicks submit.The only thing which differs this from a standard contact form is the addition of the sendTo select box which gives us the functionality to chose a recipient.

contactForm.html
<select id="sendTo" name="sendTo">
		<option id="dt17" value="dt17">dt17</option>
		<option id="badmotherz" value="badmotherz">badmotherz</option>
		<option id="php_penguin" value="php_penguin">php_penguin</option>
</select>

You can modify this as you see fit. These are the possible recipients of the email we will send from this form.

Step 2: Dealing with send.php
We now need a new text file called send.php. After the user has filled out the form and submitted it, the contents of the form will be sent to "send.php" in the POST array (because our form method was set to POST if you remember). We can identify each element within the form by the name attribute we assigned to it. So for example, if we wanted to retrieve the name only, we can do so in "send.php" by the following code:

send.php
<?php
$name = $_POST['name'];
?>

$name is a variable I've declared to store the value posted across
$_POST['name'] is where the contents of our name form element are currently stored
So this code simply transfers the value of name into $name which makes it much easier to reference and re-use

However, we don't only want the name, we want all the details that were posted from our form. In this case, that can be achieved like so:

send.php
<?php
$name = $_POST['name'];
$yourEmail = $_POST['yourEmail'];
$sendTo = $_POST['sendTo'];
$subject = $_POST['subject'];
$message = $_POST['message'];
?>

So now we have all of the information we need to send to the selected recipient in our "send.php" file. What we need to do now, is determine which email address to send it to based on the option the user selected from the drop down box in our form. The name of the person/department lies within our $sendTo variable at the moment. To do this, we will use the following switch statement:

send.php
switch ($sendTo)
{
case "dt17":
  $sendTo = "dt17@wdf.co.uk";
  break;
case "badmotherz":
  $sendTo = "badmotherz@wdf.co.uk";
  break;
 case "php_penguin":
  $sendTo = "php_penguin@wdf.co.uk";
  break;
default:
  $sendTo = "defaultemail@wdf.co.uk";
}

This switch statement acts according to the value of the $sendTo variable. If you remember, $sendTo was set by the option the user selected in our contact form. So if the user selected the option with a value of dt17, the switch statement will re-assign the $sendTo value my email address. In this case, $sendTo will now be dt17@wdf.co.uk. After the switch statement has successfully assigned $sendTo my email address, it can now break; out of the switch statement. If it can't assign $sendTo an email address based on the users selection in the form, it will be assigned a default email address (defaultemail@wdf.co.uk).

Step 3: Sending the email!
To send the email, we are going to use PHP5's mail() function. For this tutorial, we are going to pass in 4 parameters which will be:

  • The recipients email address - $sendTo
  • The subject of the message - $subject
  • The message it's self - $message
  • And a From header*

*A From header is required in order to invoke the PHP mail() function. You can change this to whatever you like as it doesn't really matter. This will simply appear in the From: part of the email.

The beauty of this step is, we already have all the information. However, to make it a bit easier to send and interpret when we recieve it, there is no harm in formatting it slightly.

$subject = "Subject: $subject";
$message = " Message: $message \r \n From: $name  \r \n Reply to: $yourEmail";

This is quite self-explanatory. $subject now holds "Subject: Whatever". $message now holds the message, the From header and reply to header. All we need to do is shove it into the function like so:
mail($sendTo, $subject, $message, "From: mysitescontactform@mysite.com");
And away it goes to the email of the person the user selected on contactForm.html. Just to be nice, I gave some user feedback;

$todayis = date("l, F j, Y, g:i a");
echo "Email succesfully sent to $sendTo on $todayis";

But you can re-direct them or whatever you see fit.

So there you have it, hope this has been not too bad!

Attached File(s)


0

#2 User is offline   MrBrightside 

  • Expert
  • PipPipPipPip
  • Group: Members
  • Posts: 855
  • Joined: 05-May 09
  • Reputation: 25
  • Gender:Male
  • Location:In the Diary of Jane
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 12 June 2009 - 12:19 AM

Nice tutorial! It does something tricky but it's well explained, even for PHP beginners :D
0

#3 User is offline   dt17 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 450
  • Joined: 22-May 09
  • Reputation: 14
  • Gender:Male
  • Location:Scotland
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 12 June 2009 - 11:37 AM

Thanks mate :)
0

#4 User is offline   Badmotherz 

  • Not dead, just not very active!
  • PipPipPipPip
  • Group: Members
  • Posts: 857
  • Joined: 26-October 07
  • Reputation: 2
  • Gender:Male
  • Location:Tewkesbury, UK
  • Experience:Intermediate
  • Area of Expertise:Designer/Coder

Posted 14 June 2009 - 10:07 AM

This is great. Huge thanks for this, I will be using it very soon ;)
0

#5 User is offline   dt17 

  • Advanced Member
  • PipPipPip
  • View gallery
  • Group: Members
  • Posts: 450
  • Joined: 22-May 09
  • Reputation: 14
  • Gender:Male
  • Location:Scotland
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 14 June 2009 - 12:48 PM

Glad I can help! :)
0

#6 User is offline   paula 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 19-August 09
  • Reputation: 0
  • Gender:Female
  • Experience:Advanced
  • Area of Expertise:Designer

Posted 19 August 2009 - 04:16 PM

Massive thanks for this - it saved my life today when i needed to fix 5 of my clients contact forms which were not PHP5 compatible.
0

#7 User is offline   MolotovRuss 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,336
  • Joined: 30-December 07
  • Reputation: 14
  • Gender:Male
  • Location:Liphook, Hampshire
  • Experience:Advanced
  • Area of Expertise:Web Designer

Posted 19 August 2009 - 04:22 PM

Ah what a great help! Thanks geekoo!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users