[php] PHP send mail to multiple email addresses

What code I should do change in this PHP script to send one email to more than 20 email addresses?

<?php

$email_to = "[email protected]"; // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

?>

Please give me an example. Thank you.

This question is related to php forms

The answer is


Something like this:

mail("[email protected] , [email protected] , [email protected]", "Test e-mail", "Hi, this is a test message!");

http://myphpform.com/php-form-multiple-recipients.php


I think the following code will works.

$tos = array('[email protected]', '[email protected]');
foreach ($tos as $to){
    $ok = mail ($to, $subject, $body, $from);
}
if ($ok) {
    echo "Message Send";
} else { 
    echo "Error";
}

Following code will do the task....

<?php

$contacts = array(
"[email protected]",
"[email protected]",
//....as many email address as you need
);

foreach($contacts as $contact) {

$to      =  $contact;
$subject = 'the subject';
$message = 'hello';
mail($to, $subject, $message, $headers);

}

?>

Fore readability sake in the code use an array and implode it to a comma separated string:-

$recipients = array(
  "[email protected]",
  // more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

The best way could be to save all the emails in a database.

You can try this code, assuming you have your email in a database

/*Your connection to your database comes here*/
$query="select email from yourtable";
$result =mysql_query($query);

/the above code depends on where you saved your email addresses, so make sure you replace it with your parameters/

Then you can make a comma separated string from the result,

while($row=$result->fetch_array()){
        if($rows=='')    //this prevents from inserting comma on before the first element
        $rows.=$row['email'];
        else
        $rows.=','.$row['email'];
    }

Now you can use

$to = explode(',',$rows); // to change to array

$string =implode(',',$cc); //to get back the string separated by comma

With above code you can send the email like this

mail($string, "Test", "Hi, Happy X-Mas and New Year");

This worked for me,

$recipient_email = '[email protected],[email protected]';

$success = mail($recipient_email, $subject, $body, $headers);

Just separate them by comma, like $email_to = "[email protected], [email protected], John Doe <[email protected]>".


Try this. It works for me.

$to = $email1 .','. $email2 .','. $email3;

Programmatically sending an submitted form to multiple email address is a possible thing, however the best practice for this is by creating a mailing list. On the code the list address will be place and any change or update on email addresses to the recipients list can be done without changing in the code.


In mail function you can as many reciepient as you want in $emailto paramater seperated by comma.


Your

$email_to = "[email protected], [email protected], [email protected]"

Needs to be a comma delimited list of email adrresses.

mail($email_to, $email_subject, $thankyou);

It is very bad practice to send all email addresses to all recipients; you should use Bcc (blind carbon copies).

    $from = "[email protected]";
    $recipList = "mailaddress1,mailaddress2,etc";
    $headers = "MIME-Version: 1.0\nContent-type: text/html; charset=utf-8\nFrom: {$from}\nBcc: {$recipList}\nDate: ".date(DATE_RFC2822);
    mail(null,$subject,$message,$headers); //send the eail

    $recipients = "[email protected],[email protected],[email protected],[email protected]";
    $email_array = explode(",",$recipients);
    foreach($email_array as $email)
    {
        echo $to      =  $email;
        $subject = 'the subject';
        $message = 'hello';
       $headers = 'From: [email protected]' . "\r\n" .
       'Reply-To: [email protected]' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $message, $headers);

    }

You can just write multiple email address to whom you want to send and pass it as the first argument. Example:-

mail("[email protected], [email protected]","Subject","Message","From: [email protected]");