Email to multiple recipients with PHP

We have already seen how easy it is to send an email in PHP, but how we have to do to be able to send an email to multiple recipients with PHP. Well let’s see how simple it is to perform this task.

The first thing we have to look at is the method used in PHP to send emails, this is the function
mail()
. If we analyze the parameters that this function offers us, we will see that they are very easy to understand:

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

The $to parameter is what allows us to identify the recipients of the email. Since we want to send the email to multiple recipients with PHP we must form a chain of emails which are separated by commas.

It is important to respect the use of the comma to separate them, since another way of separating emails would not work.

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

The rest of the parameters will be: $subject to represent the title of the message and $message to indicate the message you want to send.

In order to indicate who is sending you the email we must use the method headers
mail()
.

$para = '[email protected], [email protected]';
$titulo = 'Sending email from PHP';
$message = 'This is an email sent to multiple recipients';
$headers = 'From: Line of Code ';

We will only have to invoke
mail()
.

$sent = mail($to, $title, $message, $headers);

The value that is left in the variable $sent will represent whether the email was sent correctly or there were problems in sending it. That is why we can carry out some verification:

if ($sent)
  echo 'Email successfully sent to '.$para;
else
  echo 'Error sending email';

In this simple way we have managed to send an email to multiple recipients with PHP.