How to send bulk mail with php mailer

To send bulk mail using PHP Mailer, you can follow these steps:

Step 1: Install PHP Mailer

You can install PHP Mailer using Composer by running the following command:

composer require phpmailer/phpmailer

Step 2: Configure PHP Mailer

Create a new PHP file (e.g., mail.php) and include the PHP Mailer library:

<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Set your SMTP server settings
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'your_smtp_host';
$mail->SMTPAuth = true;
$mail->Username = 'your_smtp_username';
$mail->Password = 'your_smtp_password';
$mail->Port = 587; // or 25, 465, etc.

Step 3: Prepare the email content

Create an array to store the email content for each recipient:

$emails = array(
    array(
        'to' => '[email protected]',
        'subject' => 'Bulk Mail Test',
        'body' => 'This is a test email sent using PHP Mailer.'
    ),
    array(
        'to' => '[email protected]',
        'subject' => 'Bulk Mail Test',
        'body' => 'This is a test email sent using PHP Mailer.'
    ),
    // Add more recipients here...
);

Step 4: Loop through the recipients and send the emails

Use a loop to iterate through the $emails array and send each email:

foreach ($emails as $email) {
    $mail->clearAllRecipients();
    $mail->addAddress($email['to']);
    $mail->Subject = $email['subject'];
    $mail->Body = $email['body'];
    $mail->send();
    echo "Email sent to ". $email['to']. "<br>";
}

Step 5: Handle errors

You can use the Exception class to catch any errors that occur during the email sending process:

try {
    $mail->send();
} catch (Exception $e) {
    echo "Error sending email: ". $e->getMessage();
}

Full code example

Here's the complete code example:

<?php
require 'vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Set your SMTP server settings
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'your_smtp_host';
$mail->SMTPAuth = true;
$mail->Username = 'your_smtp_username';
$mail->Password = 'your_smtp_password';
$mail->Port = 587; // or 25, 465, etc.

$emails = array(
    array(
        'to' => '[email protected]',
        'subject' => 'Bulk Mail Test',
        'body' => 'This is a test email sent using PHP Mailer.'
    ),
    array(
        'to' => '[email protected]',
        'subject' => 'Bulk Mail Test',
        'body' => 'This is a test email sent using PHP Mailer.'
    ),
    // Add more recipients here...
);

foreach ($emails as $email) {
    $mail->clearAllRecipients();
    $mail->addAddress($email['to']);
    $mail->Subject = $email['subject'];
    $mail->Body = $email['body'];
    try {
        $mail->send();
        echo "Email sent to ". $email['to']. "<br>";
    } catch (Exception $e) {
        echo "Error sending email: ". $e->getMessage();
    }
}

Make sure to replace the placeholders (your_smtp_host, your_smtp_username, your_smtp_password) with your actual SMTP server settings. Also, be aware of any email sending limits imposed by your SMTP server or email service provider.