Page 1 of 1

Need help with sending to multiple email addresses

Posted: Sat Oct 16, 2010 5:18 am
by jools619
Hi

Apologies as this may seem like a trival matter, but im new to coding and just need a bit of help....

I have a form which includes a drop down menu of various departments. Depending on which department is chosen, the form is then sent to different people.

This is the html code for the drop down:

<select name="department" size="1" id="department">
<option value="" selected>Please select</option>
<option value="Health & Safety">Health & Safety</option>
<option value="Property">Property</option>
<option value="Litigation & Insolvancy">Litigation & Insolvancy</option>
<option value="Family Law">Family Law</option>
<option value="Company Law">Company Law</option>
<option value="Wills, Trusts & Probate">Wills, Trusts & Probate</option>
<option value="Employment Law">Employment Law</option>
<option value="General Enquiry">General Enquiry</option>
</select>

So for example the first option mails out to mrjones@email.com mrsmith@email.com and the second would go to mrbloggs@emai.com mr@black@email.com and mrwhite@email.com etc...

I have a separate php file which gathers the variables so i was thinking an if statement would need to be put into this page?

Any help very much appreciated

Thanks
Jools

Re: Need help with sending to multiple email addresses

Posted: Sat Oct 16, 2010 7:14 am
by MindOverBody
My advice is to make array of emails for every option, and in php file which is gathering variables make switch loop. In every option case loop email send function with foreach.

Something like this:

Code: Select all

// Make recipient email list like this for ervery options you have
$healthEmailArray = array( "mrjones@email.com", "mrsmith@email.com" );
$propertyEmailArray  = array( "mrbloggs@emai.com", "mrblack@email.com", "mrwhite@email.com" );

// Make switch loop like to send emails for selected option
// I made just firt two option cases, so you do the rest
switch( $_GET['department'] ) {
     // This is for Health and safety
     case "1":
         foreach ( $healthEmailArray as $email ){
	      // Do send function for email
	 }
     break;

     // This is for Property
     case "2":
          foreach ( $propertyEmailArray as $email ){
	       // Do send function for email
	  }
     break;
}

Re: Need help with sending to multiple email addresses

Posted: Sat Oct 16, 2010 7:47 am
by jools619
Thanks for the reply i will try that out.

Just two quick questions, where you have case '1' and case '2' would that need to match the value in the drop down menu (for example you have case 1 for health & safety but the value in the html form is 'health & safety' and not '1'.

Also im using $email_to as a the variable to identify which email address the form goes to, so would i replace that with where you've put as $email?

Thanks again with your help

Re: Need help with sending to multiple email addresses

Posted: Sat Oct 16, 2010 8:24 am
by jools619
hmm im not getting any luck, here is my php code:

Code: Select all

  
<?php

    $personalinjuryEmailArray = array( "julian.yates1@gmail.com", "jools105@hotmail.com" );
    $credithireEmailArray  = array( "mrbloggs@emai.com", "mrblack@email.com", "mrwhite@email.com" );
    
switch( $_GET['department'] ) {
     // This is for Health and safety
     case "1":
         foreach ( $personalinjuryEmailArray as $email_to ){

         }
     break;

   case "2":
         foreach ( $credithireEmailArray as $email_to ){
  
         }
     break;
}
  

    $email    =   strip_tags($_POST['email']);

    $message  = "Title: " . strip_tags($_POST['title']) . "\r\n";
    $message .= "Name: " . strip_tags($_POST['name']) . "\r\n";
    $message .= "Email Address: " . strip_tags($_POST['email']) . "\r\n";
    $message .= "Address: " . strip_tags($_POST['address']) . "\r\n";
    $message .= "Telephone: " . strip_tags($_POST['telephone']) . "\r\n";
    $message .= "Message: " . strip_tags($_POST['comment']) . "\r\n";
    

    $headers  = "From: $email\r\n";
    $headers .= "Reply-To: $email\r\n";
    $subject = "Website Contact Form Enquiry". $subject;


    if(mail($email_to, $subject, $message, $headers)){
        echo 'sent';     
    }else{
        echo 'failed';  
    }
?>
and the html code for the drop down menu is now:

<select name="department" size="1" id="department">
<option value="" selected>Please select</option>
<option value="1">Personal Injury</option>
<option value="2">ULR & Credit Hire</option>
<option value="3">Consumer Credit</option>
<option value="4">Health & Safety</option>
<option value="5">Property</option>
<option value="6">Litigation & Insolvancy</option>
<option value="7">Family Law</option>
<option value="8">Company Law</option>
<option value="9">Wills, Trusts & Probate</option>
<option value="10">Employment Law</option>
<option value="11">General Enquiry</option>

</select>

I know ive gone wrong somewhere, but not sure where

Re: Need help with sending to multiple email addresses

Posted: Sat Oct 16, 2010 8:37 am
by MindOverBody
jools619 wrote:Just two quick questions, where you have case '1' and case '2' would that need to match the value in the drop down menu (for example you have case 1 for health & safety but the value in the html form is 'health & safety' and not '1'.
Ups, sorry, i forgot to mention that. I replaced your option values with numbers, so you make it whatever you feel it is best. ;)
MindOverBody wrote:Also im using $email_to as a the variable to identify which email address the form goes to, so would i replace that with where you've put as $email?
Lets make method that will look like this:

Code: Select all

function SendMail( $email_to) {
        $email = strip_tags($_POST['email']);

        $message = "Title: " . strip_tags($_POST['title']) . "\r\n";
        $message .= "Name: " . strip_tags($_POST['name']) . "\r\n";
        $message .= "Email Address: " . strip_tags($_POST['email']) . "\r\n";
        $message .= "Address: " . strip_tags($_POST['address']) . "\r\n";
        $message .= "Telephone: " . strip_tags($_POST['telephone']) . "\r\n";
        $message .= "Message: " . strip_tags($_POST['comment']) . "\r\n";

        $headers = "From: $email\r\n";
        $headers .= "Reply-To: $email\r\n";
        $subject = "Website Contact Form Enquiry". $subject;

        if(mail($email_to, $subject, $message, $headers)){
                 echo 'sent'; 
        } else echo 'failed'; 
}
And now apply this function into foreach loop in switch case. Something like this:

Code: Select all

switch( $_POST['department'] ){
      case "1":
            foreach ( $healthEmailArray as $email_to  ){
                  SendMail( $email_to );
            }
      break;
}
So, this will based on choosed option send email to everybody listend in that option email list.

Re: Need help with sending to multiple email addresses

Posted: Sat Oct 16, 2010 11:22 am
by jools619
Thanks for your help but im still having trouble. i used your code but the emails arent sending through. This is my PHP code:

Code: Select all

  
<?php

function SendMail( $email_to ) {

$personalinjuryEmailArray = array( "julian.yates1@gmail.com", "jools105@hotmail.com" );
$credithireEmailArray  = array( "enquiry@julian-yates.co.uk" );

switch( $_POST['department'] ){
      case "1":
            foreach ( $personalinjuryEmailArray as $email_to ){
                  SendMail( $email_to );
            }
      break;
      
case "2":
         foreach ( $credithireEmailArray as $email_to ){
         SendMail( $email_to );
 
         }
     break;
}

        $email = strip_tags($_POST['email']);

        $message = "Title: " . strip_tags($_POST['title']) . "\r\n";
        $message .= "Name: " . strip_tags($_POST['name']) . "\r\n";
        $message .= "Email Address: " . strip_tags($_POST['email']) . "\r\n";
        $message .= "Address: " . strip_tags($_POST['address']) . "\r\n";
        $message .= "Telephone: " . strip_tags($_POST['telephone']) . "\r\n";
        $message .= "Message: " . strip_tags($_POST['comment']) . "\r\n";

        $headers = "From: $email\r\n";
        $headers .= "Reply-To: $email\r\n";
        $subject = "Website Contact Form Enquiry". $subject;

        if(mail($email_to, $subject, $message, $headers)){
                 echo 'sent';
        } else echo 'failed';
}
?>
Ive even copied straight from your code, just not sure whys its not working?

Re: Need help with sending to multiple email addresses

Posted: Sat Oct 16, 2010 11:42 am
by jaceinla
Personally, I think it's a bad idea to grab every single email from every single dept and then put it into an array. Rather, assign departments in your DB (as I'm guessing you have) and match that with the select options. eg.

Code: Select all

$selection = $_POST['selection']
$query = "SELECT * FROM table WHERE dept = $selection
//query the result

then do the mail function to each one

Also, keep in mind that the mail function in PHP is pretty dull and can have several errors not caused by you the programmer. Try out a simpler method with PHPMailer at http://sourceforge.net/projects/phpmail ... %20php5_6/

Re: Need help with sending to multiple email addresses

Posted: Sun Oct 17, 2010 8:10 am
by jools619
thanks for your advice jaceinla could i use that code and put it in mine?,

My major problem is that im using a template code and ridiculously new to php coding so im not sure what to look for in terms of identifying the cause.

Out of interest, i found this on another site and wondering if it would work in my case:

<select name='department' id='department'>
<option value='man1@website.com'>Department 1</option>
<option value='man2@website.com'>Department 1</option>
<option value='man3@website.com'>Department 1</option>
<option value='man4@website.com'>Department 1</option>
</select>

Code: Select all

then PHP code:
$email_to = $_POST['department']; 
this actually works when i have single email addresses as the option value, but if i try for example:

<select name='department' id='department'>
<option value='man1@website.com, man2@website.com '>Department 1</option>
</select>

this doesnt work. i know its must be bad practise, but for a moment i was able to send out to difefrent people by choosing different departments, only to a single email address