Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Contact Form

Featured Replies

Hello! It's me again... :)

 

I have looked everywhere, but I cannot seem to work this out at all!

 

Okay, so I have my PHP contact form which works just fine, except I cannot work out how to get it to send to different mailboxes dependent on the subject.

 

For example, the user fills out their name, email etc.. Then chooses what subject the matter is about. I would like the script to automatically send to different mailboxes depending on what is chosen so if 'Finance' is chosen, it will send to finance@something.com, if 'general' is chosen it will send to general@something.com.

 

I am not 100% sure this can be done, but apparently it can.

 

Any help at all would be much appreciated!

 

Thank you.

<select name="subject">
 <option value="Email1@email.com">Department #1</option>
 <option value="Email1@email.com">Department #2</option>
 <option value="Email1@email.com">Department #3</option>
</select>

 

Then for the php simple grab the value of the select

$to = $_POST['subject'];

  • Author

<select name="subject">
 <option value="Email1@email.com">Department #1</option>
 <option value="Email1@email.com">Department #2</option>
 <option value="Email1@email.com">Department #3</option>
</select>

 

Then for the php simple grab the value of the select

$to = $_POST['subject'];

 

So all I have to do is enter those two snippets of code and it will work?

  • Author

I don't understand how you do that sorry. Can you explain a bit more please?

 

Thanks.

in your contact form you have a funcation like so

mail("email@email.com", $subject, $message);

 

instead of email@email.com you remove it including the quotes and simply put in $to to send it to the choosen department email. If you still dont get it post me your entire code :)

 

Adam

  • Author

in your contact form you have a funcation like so

mail("email@email.com", $subject, $message);

 

instead of email@email.com you remove it including the quotes and simply put in $to to send it to the choosen department email. If you still dont get it post me your entire code :)

 

Adam

 

Would this be the part you mean?

 

// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers);  
?>   

  • Author

yes, change $email_to

 

make it: $to

 

test and tell me if its working.

 

Okay, it won't work.

 

Here is my original code:

<!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">
<head>
<meta http-equiv="Refresh" content="5;url=http://www.richardrathbone.co.uk/contact.html" />
<link rel="stylesheet" href="css/master.css" type="text/css" media="all" />
</head>
<body>
 <div class="container">

   <div class="header">
     <h1><img src="images/rathbone.png" alt="Rathbone Web Design" /></h1><a class=
     "structural" href="#content">Skip to main content</a>
   </div>
 </div>
 <div class="content-container">
     <div class="section-nav">
       <h3 class="structural">Section navigation</h3>
     </div>

     <div class="content gutter">
       <a name="content" id="content"></a>

<?php
if(isset($_POST['email'])) {

          // EDIT THE 2 LINES BELOW AS REQUIRED    
		$email_to = "contact@richardrathbone.co.uk";    
		$email_subject = "General Contact";                 

	function died($error) {
		// your error code can go here        
		echo "We are very sorry, but there were error(s) found with the form you submitted. ";         
		echo "These errors appear below.<br /><br />";         
		echo $error."<br /><br />";         
		echo "Please press back and fix these errors.<br /><br />";         
		die();    
	}           

	// validation expected data exists    
	if(!isset($_POST['first_name']) ||
		!isset($_POST['last_name']) ||        
		!isset($_POST['email']) ||         
		!isset($_POST['telephone']) ||         
		!isset($_POST['comments'])) {        
		died('We are sorry, but there appears to be a problem with the form you submitted.');            
	}           

	$first_name = $_POST['first_name']; // required     
	$last_name = $_POST['last_name']; // required     
	$email_from = $_POST['email']; // required    
	$telephone = $_POST['telephone']; // not required     
	$comments = $_POST['comments']; // required           

	$error_message = "";     
	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";   
if(!eregi($email_exp,$email_from)) {    
	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';   
}     
	$string_exp = "^[a-z .'-]+$";  
if(!eregi($string_exp,$first_name)) {     
	$error_message .= 'The First Name you entered does not appear to be valid.<br />';   
}  
if(!eregi($string_exp,$last_name)) {     
	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';   
}   
if(strlen($comments) < 2) {     
	$error_message .= 'The Comments you entered do not appear to be valid.<br />';   
}   
if(strlen($error_message) > 0) {     
	died($error_message);   
}     
	$email_message = "Form details below.\n\n";           

		function clean_string($string) {       
			$bad = array("content-type","bcc:","to:","cc:","href");       
			return str_replace($bad,"",$string);     
		}           

		$email_message .= "First Name: ".clean_string($first_name)."\n";     
		$email_message .= "Last Name: ".clean_string($last_name)."\n";     
		$email_message .= "Email: ".clean_string($email_from)."\n";    
		$email_message .= "Telephone: ".clean_string($telephone)."\n";     
		$email_message .= "Comments: ".clean_string($comments)."\n";             

// create email headers 
$headers = 'From: '.$email_from."\r\n". 
'Reply-To: '.$email_from."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers);  
?>   

<!-- include your own success html here -->   


       <h1 style="font-family: comic sans ms; color: #09f; font-size: 15px;">
Thank you for getting in touch. I will reply as soon as possible.</h1>
     </div>
   </div>
 </div>
</body>


<? 
}
?> 

</html>

 

HTML:

<form name="contactform" method="post" action="send_form_email.php" id=
       "contactform">
         <fieldset>

           <!-- Your Name -->

           <div>
             <label for="first_name">First name <abbr title=
             "Required field">*</abbr></label> <input type="text" name="first_name" id=
             "name" />
           </div>

           <div>
             <label for="last_name">Last name <abbr title=
             "Required field">*</abbr></label> <input type="text" name="last_name" id=
             "sname" />
           </div>

           <!-- Email -->

           <div>
             <label for="email">Your email address <abbr title=
             "Required field">*</abbr></label> <input type="text" name="email" id=
             "email" />
           </div>

           <!-- Your Number -->

           <div>
             <label for="telephone">Telephone</label> <input type="text" name=
             "telephone" id="number" />
           </div>

           <!-- Subject -->

           <div>
             <label for="subject">Subject</label> 
             <select name="subject">
               <option value="#">Please choose</option>
               <option value="complaints@richardrathbone.co.uk">Complaints</option>
               <option value="fianance@richardrathbone.co.uk">Finance</option>
               <option value="General@richardrathbone.co.uk">General Comments</option>
               <option value="quotes@richardrathbone.co.uk">Quotes</option>
               <option value="suggestions@richardrathbone.co.uk">Suggestions</option>
               <option value="support@richardrathbone.co.uk">Support</option>
              </select>
           </div>

           <!-- Comments -->

           <div>
             <label for="comments">Your comments <abbr title=
             "Required field">*</abbr></label> 
             <textarea name="comments" id="comment" rows="10" cols="50">
</textarea>
           </div>

           <!-- Controls -->

           <div class="controls">
             <input id="submit" name="submit" type="submit" value="Submit Feedback" />
           </div>
         </fieldset>
       </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">
<head>
<meta http-equiv="Refresh" content="5;url=http://www.richardrathbone.co.uk/contact.html" />
<link rel="stylesheet" href="css/master.css" type="text/css" media="all" />
</head>
<body>
 <div class="container">

   <div class="header">
     <h1><img src="images/rathbone.png" alt="Rathbone Web Design" /></h1><a class=
     "structural" href="#content">Skip to main content</a>
   </div>
 </div>
 <div class="content-container">
     <div class="section-nav">
       <h3 class="structural">Section navigation</h3>
     </div>

     <div class="content gutter">
       <a name="content" id="content"></a>

<?php
if(isset($_POST['email'])) {

          // EDIT THE 2 LINES BELOW AS REQUIRED    
                       $email_to = "contact@richardrathbone.co.uk";    
                       $email_subject = "General Contact";                 

               function died($error) {
                       // your error code can go here        
                       echo "We are very sorry, but there were error(s) found with the form you submitted. ";         
                       echo "These errors appear below.<br /><br />";         
                       echo $error."<br /><br />";         
                       echo "Please press back and fix these errors.<br /><br />";         
                       die();    
               }           

               // validation expected data exists    
               if(!isset($_POST['first_name']) ||
                       !isset($_POST['last_name']) ||        
                       !isset($_POST['email']) ||         
                       !isset($_POST['telephone']) ||         
                       !isset($_POST['comments'])) {        
                       died('We are sorry, but there appears to be a problem with the form you submitted.');            
               }           

               $first_name = $_POST['first_name']; // required     
               $last_name = $_POST['last_name']; // required     
               $email_from = $_POST['email']; // required    
               $telephone = $_POST['telephone']; // not required     
               $comments = $_POST['comments']; // required 
               $dept = $_POST['subject'];          

               $error_message = "";     
               $email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";   
       if(!eregi($email_exp,$email_from)) {    
               $error_message .= 'The Email Address you entered does not appear to be valid.<br />';   
       }     
               $string_exp = "^[a-z .'-]+$";  
       if(!eregi($string_exp,$first_name)) {     
               $error_message .= 'The First Name you entered does not appear to be valid.<br />';   
       }  
       if(!eregi($string_exp,$last_name)) {     
               $error_message .= 'The Last Name you entered does not appear to be valid.<br />';   
       }   
       if(strlen($comments) < 2) {     
               $error_message .= 'The Comments you entered do not appear to be valid.<br />';   
       }   
       if(strlen($error_message) > 0) {     
               died($error_message);   
       }     
               $email_message = "Form details below.\n\n";           

                       function clean_string($string) {       
                               $bad = array("content-type","bcc:","to:","cc:","href");       
                               return str_replace($bad,"",$string);     
                       }           

                       $email_message .= "First Name: ".clean_string($first_name)."\n";     
                       $email_message .= "Last Name: ".clean_string($last_name)."\n";     
                       $email_message .= "Email: ".clean_string($email_from)."\n";    
                       $email_message .= "Telephone: ".clean_string($telephone)."\n";     
                       $email_message .= "Comments: ".clean_string($comments)."\n";             

       // create email headers 
       $headers = 'From: '.$email_from."\r\n". 
       'Reply-To: '.$email_from."\r\n" . 
       'X-Mailer: PHP/' . phpversion(); 
       @mail($dept, $email_subject, $email_message, $headers);  
?>   

<!-- include your own success html here -->   


       <h1 style="font-family: comic sans ms; color: #09f; font-size: 15px;">
Thank you for getting in touch. I will reply as soon as possible.</h1>
     </div>
   </div>
 </div>
</body>


<? 
}
?> 

</html>

 

Use this as your PHP file.

  • Author

Personally, I think that's a a sure fire way to attract span and abuse. I'd change the value of each field to a number (say a department id). In my processing code I'd check the posted number against a whitelist of allowed department id's and use a simple switch statement to set the mail to.

Okay mate, you have completely lost me now haha! I understand what you are saying, but I am new to PHP.

 

Thanks anyway.

  • Author

Sorry, was thinking out loud :)

 

You'd change your form field to following:

 

<select name="subject">
 <option value="1">Department #1</option>
 <option value="2">Department #2</option>
 <option value="3">Department #3</option>
</select>

 

 

Then within your form processing code, where you process all form data you make a slight change (you could make the array multidimensional, but the simplist way of doing things would be below):

 

//Add in checking/sanitizing etc.
$allowed_deps = array('1', '2', '3', '45', '789'); // All allowed department ids. etc.
$posted_dept_id = $_POST['subject'];
if (in_array($posted_dept_id, $allowed_deps)) {
//valid dept id posted, get email via dept id
} else {
//someone is buggering around
}

Okay, thanks. But how does that automatically mail it to the different mailboxes? To me that looks like it just tells me where the person wants it to go, just like it tells me what their name is, or their message; not the actual directing of the email to the different mailboxes.

  • Author

OK, spoon feeding time, say "aaaaaa".

 

<select name="subject">
 <option value="1">Department #1</option>
 <option value="2">Department #2</option>
 <option value="3">Department #3</option>
</select>

 

now the php part:

 

$allowed_deps = array('1', '2', '3'); // All allowed department ids. etc.
$posted_dept_id = $_POST['subject'];

$email1="email1@domain.com";
$email2="email2@domain.com";

//set the rest of the $emails, $subjects, $headers and if you don't,
//I'm gonna gonna beat you with a "Learn PHP" book till you do 

if (in_array($posted_dept_id, $allowed_deps)) {
   switch ($posted_dept_id) {
       case 1: { mail($email1,$subject,$message,$headers); break; }
       case 2: { mail($email2,$subject,$message,$headers); break; }
       case 3: { mail($email3,$subject,$message,$headers); break; }
   }
} else {
//someone is buggering around
}

 

Thanks.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.