How send mail to multiple users with php

To send mail to multiple users with PHP, you can use the mail() function or a PHP mail library like PHPMailer. Here are some examples:

Using the mail() function:

$to = array('[email protected]', '[email protected]', '[email protected]');
$subject = 'Hello from PHP';
$message = 'This is a test email sent to multiple users';

foreach ($to as $email) {
    mail($email, $subject, $message);
}

This code sends a separate email to each user in the $to array.

Using PHPMailer:

require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';

$to = array('[email protected]', '[email protected]', '[email protected]');
$subject = 'Hello from PHP';
$message = 'This is a test email sent to multiple users';

$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress($to);

$mail->Subject = $subject;
$mail->Body = $message;

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

This code uses PHPMailer to send a single email to multiple recipients.

Using a loop with mail() function:

$to = array('[email protected]', '[email protected]', '[email protected]');
$subject = 'Hello from PHP';
$message = 'This is a test email sent to multiple users';

foreach ($to as $email) {
    $headers = 'From: [email protected]'. "\r\n".
               'Reply-To: [email protected]'. "\r\n".
               'MIME-Version: 1.0'. "\r\n".
               'Content-Type: text/plain; charset=UTF-8';

    mail($email, $subject, $message, $headers);
}

This code uses a loop to send a separate email to each user in the $to array, with a custom set of headers.

Remember to replace the placeholders ([email protected], your_password, etc.) with your actual email credentials and settings.