What code I should do change in this PHP script to send one email to more than 20 email addresses?
Try this. It works for me.
$to = $email1 .','. $email2 .','. $email3;
Following code will do the task....
<?php
$contacts = array(
"youremailaddress@yourdomain.com",
"youremailaddress@yourdomain.com",
//....as many email address as you need
);
foreach($contacts as $contact) {
$to = $contact;
$subject = 'the subject';
$message = 'hello';
mail($to, $subject, $message, $headers);
}
?>
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");
In mail function you can as many reciepient as you want in $emailto paramater seperated by comma.
I think the following code will works.
$tos = array('address1@yourdomain.com', 'address2@yourdomain.com');
foreach ($tos as $to){
$ok = mail ($to, $subject, $body, $from);
}
if ($ok) {
echo "Message Send";
} else {
echo "Error";
}
$recipients = "test1@test.com,test2@test.com,test3@test.com,test1@test.com";
$email_array = explode(",",$recipients);
foreach($email_array as $email)
{
echo $to = $email;
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}