Pedro
Pedro

Reputation: 575

Sending mail to multiple recipients

I have this PHP code for sending emails from a website's contact form:

<?php

  if(count($_POST) > 0){

    $userName = $_POST['userName'];
    $userEmail = $_POST['userEmail'];
    $userSubject = $_POST['userSubject'];
    $userMessage = $_POST['userMessage'];
    $header = "Content-Type: text/html\r\nReply-To: $userEmail\r\nFrom: $userEmail <$userEmail>";

    $body = 
    @"Contact sent from website ".$_SERVER['REMOTE_ADDR']." | Day and time ".date("d/m/Y H:i",time())."<br />
    <hr />
    <p><b>Name:</b></p>
    $userName
    <p>———————————</p>
    <p><b>Subject:</b></p>
    $userSubject
    <p>———————————</p>
    <p><b>Mensagem:</b></p>
    $userMessage
    <hr />
    End of message";

    if(mail("[email protected]", "Mensage sent from website", $body, $header)){
      die("true");  
    } else {
        die("Error sending.");  
      }

  }

?>

I need to change it in order to send emails to two recipients:

... don't know how, though. Where do I put the other e-mail? I tried adding "[email protected]" in the mail() but it didn't work...

Thanx.

Pedro

Upvotes: 1

Views: 5970

Answers (3)

Fluffeh
Fluffeh

Reputation: 33502

You can put multiple email addresses into the to field by simply adding a comma between them inside the parameter string like this:

mail("[email protected], [email protected]", // rest of your code

Edit: Per comments below.

you can hide the multiple email addresses by using the additional headers param in the mail() function as per the docs on it:

// Additional headers
$headers .= 'To: Mary <[email protected]>, Kelly <[email protected]>' . "\r\n";
$headers .= 'From: Birthday Reminder <[email protected]>' . "\r\n";
$headers .= 'Cc: [email protected]' . "\r\n";
$headers .= 'Bcc: [email protected]' . "\r\n";

This is the fourth param in the mail() arguments passed:

mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Upvotes: 1

Andy
Andy

Reputation: 3012

Just put your emails into an array like this example:

$recepients = array('[email protected]','[email protected]');

foreach($recepients as $recepient){
    mail($recepient, "Mensage sent from website", $body, $header);
}

Upvotes: 1

SkarXa
SkarXa

Reputation: 1194

From

http://php.net/manual/en/function.mail.php

The formatting of this string must comply with » RFC 2822. Some examples are:

[email protected]
[email protected], [email protected]
User <[email protected]>
User <[email protected]>, Another User <[email protected]>

Upvotes: 2

Related Questions