Jump to content

Need help with sending to multiple email addresses


jools619

Recommended Posts

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>

</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?

 

Out of interest i tried something myself and sort of got a result im after:

 

<select name='department' id='department'>

<option value='man1@website.com'>Department 1</option>

<option value='man2@website.com'>Department 1</option>

</select>

 

Then i used $email_to = $_POST['department']; in the php file.

 

But i would like to be able to send to 2 or 3 people so tried:

 

<select name='department' id='department'>

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

</select>

 

This didnt work :(

 

 

Any help very much appreciated

 

Thanks

Jools

 

Link to comment
Share on other sites

I would strongly advise you against putting e-mail addresses in your forms.  Spam Bots and spiders are going to find this, and also someone can override these e-mail addresses via an external form and post to your processing page and end up spamming as many people as they want.

 

I recommend you create an option and in your processing code - not visible to the public - based on which option you choose which e-mail (hard coded, or through a database of e-mails) etc.

 

 

Link to comment
Share on other sites

Pikachu2000 - well it works when i used:

<select name='department' id='department'>

<option value='man1@website.com'>Department 1</option>

</select>

 

But when i tried to add in another email address like:

<select name='department' id='department'>

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

</select>

 

The email form wouldnt send. I thought by putting in another email address, i could send to another person, but i assume the syntax in the option value is wrong?

 

PHP Freak - Yes very true on the spamming aspect, it was usuing this method just to get a basic model working. Although i found this out on the web which was reccommended but no luck:

 

<?php

function SendMail( $email_to ) {

$personalinjuryEmailArray = array( "man1@website.com", "man2@website.com" );
$credithireEmailArray  = array( "man3@website.com" );

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';
}
?>

 

Is there anyway the first option could work with multiple addresses?

 

Thanks for the replies though guys

 

Link to comment
Share on other sites

Pikachu2000 - well it works when i used:

<select name='department' id='department'>

<option value='man1@website.com'>Department 1</option>

</select>

 

But when i tried to add in another email address like:

<select name='department' id='department'>

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

</select>

 

The email form wouldnt send. I thought by putting in another email address, i could send to another person, but i assume the syntax in the option value is wrong?

 

PHP Freak - Yes very true on the spamming aspect, it was usuing this method just to get a basic model working. Although i found this out on the web which was reccommended but no luck:

 

<?php

function SendMail( $email_to ) {

$personalinjuryEmailArray = array( "man1@website.com", "man2@website.com" );
$credithireEmailArray  = array( "man3@website.com" );

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';
}
?>

 

Is there anyway the first option could work with multiple addresses?

 

Thanks for the replies though guys

 

 

You can send to multiple addresses via the mail function just comma separate the addresses in the TO argument - but the issue is unless you setup a BCC header - all the e-mail recipients would see each other.  I'm not sure if you would want to do that or not... perhaps just do a loop and send one e-mail through the function per e-mail address.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.