How to send email to 2recipents using mail in php

To send an email to two recipients using PHP's mail() function, you can simply separate the recipient email addresses with a comma. Here's an example:

$to = '[email protected], [email protected]';
$subject = 'Test email';
$message = 'This is a test email sent to two recipients';

mail($to, $subject, $message);

In this example, the $to variable contains a string with the two recipient email addresses separated by a comma. The mail() function will automatically split the string into individual recipient addresses and send the email to both of them.

Note that you can also use an array to store the recipient email addresses, like this:

$recipients = array('[email protected]', '[email protected]');
$to = implode(',', $recipients);
$subject = 'Test email';
$message = 'This is a test email sent to two recipients';

mail($to, $subject, $message);

This approach can be useful if you need to send the email to a variable number of recipients.

Also, you can use PHPMailer library which is more powerful and flexible than the built-in mail() function. Here is an example:

require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->setFrom('[email protected]');
$mail->addAddress('[email protected]');
$mail->addAddress('[email protected]');
$mail->Subject = 'Test email';
$mail->Body = 'This is a test email sent to two recipients';

if (!$mail->send()) {
    echo 'Error sending email';
} else {
    echo 'Email sent successfully';
}

In this example, we create a new instance of the PHPMailer class and set up the SMTP server settings. We then add the two recipient email addresses using the addAddress() method, and set the subject and body of the email. Finally, we call the send() method to send the email.