Home Code Snippets How to Send Bulk Emails in PHP using PHPMailer with Ajax

How to Send Bulk Emails in PHP using PHPMailer with Ajax

Feb 5, 2022
Sending mail to multiple address using PHPMailer
 
FOLDER STRUCTURE


 

 

 
 
 
 
index.php

 

 

<?php
$connect = new PDO("mysql:host=localhost;dbname=send-multiple-email", "root", "");
$query = "SELECT * FROM users ORDER BY ID";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Send Multiple Email Using PHP and AJAX</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <style>
        .btn-light {
            width: 60% !important;
            border: 0;
            border-radius: 0;
        }
        .btn-light:hover {
            background-color: #f0e97c;
        }
    </style>
</head>
<body>
    <br />
    <div class="container">
        <div class="table-responsive">
            <table class="table table-bordered table-dark table-hover">
                <tr class="thead-light">
                    <th>User Name</th>
                    <th>Email</th>
                    <th>Select</th>
                    <th>Action</th>
                </tr>
                <?php
$count = 0;
foreach ($result as $row)
{
    $count = $count + 1;
    echo '
                    <tr>
                        <td>' . $row["user_name"] . '</td>
                        <td>' . $row["user_email"] . '</td>
                        <td>
                            <input type="checkbox" name="single_select" class="single_select" data-email="' . $row["user_email"] . '" data-name="' . $row["user_name"] . '" />
                        </td>
                        <td>
                        <button type="button" name="email_button" class="btn btn-light shadow-none email_button" id="' . $count . '" data-email="' . $row["user_email"] . '" data-name="' . $row["user_name"] . '" data-action="single">Send Single</button>
                        </td>
                    </tr>
                    ';
}
?>
                <tr>
                    <td colspan="3"></td>
                    <td><button type="button" name="bulk_email" class="btn btn-light shadow-none  email_button" id="bulk_email" data-action="bulk">Send Bulk...</button></td>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</body>
</html>
<script>

    $(document).ready(function() {
        $('.email_button').click(function() {
            $(this).attr('disabled', 'disabled');
            var id = $(this).attr("id");
            var action = $(this).data("action");
            var email_data = [];
            if (action == 'single') {
                email_data.push({
                    email: $(this).data("email"),
                    name: $(this).data("name")
                });
            } else {

                $('.single_select').each(function() {
                    if ($(this).prop("checked") == true) {
                        email_data.push({
                            email: $(this).data("email"),
                            name: $(this).data('name')
                        });
                    }
                });
            }
            $.ajax({
                url: "send_mail.php",
                method: "POST",
                data: {
                    email_data: email_data
                },
                beforeSend: function() {
                    $('#' + id).html('Sending...');
                    $('#' + id).addClass('btn-danger');
                },
                success: function(data) {
                    if (data == 'send') {
                        $('#' + id).text('Success');
                        $('#' + id).removeClass('btn-danger');
                        $('#' + id).removeClass('btn-light');
                        $('#' + id).addClass('btn-success');
                    } else {
                        $('#' + id).text(data);
                    }
                    $('#' + id).attr('disabled', false);
                }
            })
        });
    });
</script>
 
send_mail.php
<?php
if (isset($_POST['email_data'])) {
    require 'class/class.phpmailer.php';
    $output = '';
    foreach ($_POST['email_data'] as $row) {
        $mail = new PHPMailer(true);
        $mail->isSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 587;
        $mail->SMTPSecure = "tls";
        $mail->SMTPAuth = true;
        $mail->Username = "username@gmail.com";
        $mail->Password = "password";
        $mail->From = 'username@gmail.com';     //Sets the From email address for the message
        $mail->FromName = 'Atechseva';                  //Sets the From name of the message
        $mail->AddAddress($row["email"], $row["name"]); //Adds a "To" address
        $mail->WordWrap = 50;                           //Sets word wrapping on the body of the message to a given number of characters
        $mail->IsHTML(true);                            //Sets message type to HTML
        $mail->Subject = 'Hello ' . ucwords($row["name"]) . '!'; //Sets the Subject of the message

        //An HTML or plain text message body
        $mail->Body = '
        Hello ' . ucwords($row["name"]) . ' !';
        $mail->AltBody = '';
        $result = $mail->Send();                        //Send an Email. Return true on success or false on error
        if ($result["code"] == '400') {
            $output .= html_entity_decode($result['full_error']);
        }
    }

    if ($output == '') {
        echo 'send';
    } else {
        echo $output;
    }
}

SQL
DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `user_name` varchar(300) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
  `user_email` varchar(300) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL,
  PRIMARY KEY (`ID`)) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
Share this snippet: