In this web development tutorial you have so far learned to create a form, validate the data on the form and send an email to a single person, now in this part of this tutorial we will look at how can we send that email to multiple people automatically.

the form is on the file contact.html, the sending mail is on mail.php and here is the code inside mail.php assuming we are going to send the email to these 3 email addresses:

“email1@somesite.com”

“email2@somewhere.com”

“email3@nowhere.com”

if you send this email to a lot of emails at the same time your web hosting company may think that you are spamming other sites and also it will use a lot of resources of their server and they usually don’t like that and will stop your process so the best way to do this is to send an email, wait for a few seconds and then send another in this example I will set it to wait for 60 seconds after it has sent 1 email

<?php
$emails=array(“email1@abc.com”,”email2@bcd.com”,”email3@def.com”);

foreach ($emails as $email){ // this means get any of the $emails and do the following for it
mail($email,$subject,$message,$header); //send the message to the $email which is one of the $emails
sleep(60);// wait for 60 seconds before continuing this loop again this will give us 1440 email per day
}

?>

you can set the wait time anything from 30 seconds to as many seconds as you want for the list of emails you can fill that $emails array in different ways : you can just add them to the array or you can set it up so it will load it from a database or even read it from a txt file or an XML file but as they say that is a story for another day.