How to give multiple mails in php
In PHP, you can send multiple emails using a loop or by separating the email addresses with commas. Here are a few examples:
Method 1: Using a loop
$to = array('[email protected]', '[email protected]', '[email protected]');
$subject = 'Test email';
$message = 'This is a test email';
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);
}
Method 2: Separating email addresses with commas
$to = '[email protected], [email protected], [email protected]';
$subject = 'Test email';
$message = 'This is a test 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($to, $subject, $message, $headers);
Method 3: Using a PHPMailer library
require_once 'PHPMailer/PHPMailer.php';
require_once '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->SMTPSecure = 'tls';
$mail->Port = 587;
$to = array('[email protected]', '[email protected]', '[email protected]');
$subject = 'Test email';
$message = 'This is a test email';
foreach ($to as $email) {
$mail->addAddress($email);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
}
Note: In the above examples, replace [email protected]
and your-password
with your actual email address and password.
Also, make sure to check the email settings and configuration on your server to ensure that the emails are being sent correctly.