Contact Form Multiple Emails

Discussion in 'PHP' started by Adnan959, May 3, 2011.

  1. #1
    There is a small functionality I want to implement to my existing contact.php script.

    This is the form:
    http://i52.tinypic.com/b3rlox.jpg
    [​IMG]

    Right now the entire form is being sent to a single email address.

    I would to be able to send the form to multiple emails depending on which "Clinic of Interest" is chosen as illustrated above.

    E.g: If I select islington. I want the email to go out to .
    E.g: Or if I select Brixton I would want the email to go out to .

    Is this possible? Any assistance would be greatly appreciated.

    Here's the code I'm using right now:
    <?php
       session_start();
       if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
          // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
          unset($_SESSION['security_code']);
    	
    $message = "
    Clinic of Interest: {$_POST['centre']} 
    First Name: {$_POST['firstname']} 
    Sur Name: {$_POST['surname']} 
    Email: {$_POST['email']} 
    Phone: {$_POST['phone']} 
    Additional Comments: {$_POST['comments']}
    ";
    		
    $recipient = "email@email.com";
    $subject = "Email Subject";
    $mailheader = "From: {$_POST['email']}";
    
    mail($recipient, $subject, $message, $mailheader) or die ("Failure");
    		
       } 
       
       else {
    		sleep(2); 
    		header("location:failure.html");
       }
         
    ?>
    Code (markup):
    Thanks :D
     
    Last edited: May 3, 2011
    Adnan959, May 3, 2011 IP
  2. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #2
    Really simple, just check the value of the $_POST['centre'] variable.

    
    switch ( $_POST['centre'] ) {
    	
    	case 'islington' : // change it to the value of the option tag in the contact form
    		$recipient = 'islingtonemail@example.com';
    		break;
    	case 'brixton' :
    		$recipient = 'brixtonemail@example.com';
    		break;
    	// continue adding more cases and don't forget to add a default case like this...
    	default : 
    		$recipient = 'defaultemail@example.com';
    		break;
    }
    
    PHP:
    Put the above code before it sends out the mail and remove the line where you set the $recipient variable.

    I hope it helps.
     
    bogi, May 3, 2011 IP
    Adnan959 likes this.
  3. Adnan959

    Adnan959 Member

    Messages:
    894
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Thats brilliant! :D Worked like a charm. Life saver mate. Thanks a lot.
     
    Adnan959, May 3, 2011 IP
  4. bogi

    bogi Well-Known Member

    Messages:
    482
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    140
    #4
    No problem ;) It's good to know it works :)
     
    bogi, May 3, 2011 IP