How to Send Bulk Emails in PHP using PHPMailer with Ajax jQuery

Most of the websites have an e-mail function Send one email or bulk email. In this post we cover the topic of how we can send bulk emails using PHP script using PHPMailer Ajax and Jquery. We’re using the PHPMailer library because the PHP mail() function doesn’t work on localhost.

So we discussed How to Send Email from localhost in PHP using SMTP to bulk emails from localhost to PHP using Ajax Jquery. So we can send  bulk emails without reload or website.

We use SMTP to send bulk or mass emails because SMTP is our best way to send email from localhost or an online server. If we are using SMTP to send email, PHP has a nice library called the PHPMailer library which we can use to send emails via SMTP and provide the necessary credentials.

If you have a web hosting email account, your hosting company will provide you with SMTP credentials such as username, password, outgoing and incoming server addresses, and port number. You can use this information to configure these details in the PHPMailer class and then send an email from your script.

In this post we have a simple application we can use to send bulk emails to users with one click. Here we are using Jquery with Ajax so that we can send emails without reload the web page. When we press the Send mail button, the email from the checkbox data attribute is captured with jquery.

After collecting data, it is sent to an ajax request and passed to and into the PHP script via an ajax request. If we use the PHPMailer class with the required SMTP configuration, it will be a bulk email sent.

After all emails have been successfully sent, the data will be sent to Ajax upon request and displayed on the website. This way we can use Ajax with Jquery to send bulk emails using PHP with PHPMailer Library.

See also

  1. How to Send Email from localhost in PHP using SMTP
  2. How to Send Bulk Emails in PHP using PHPMailer with Ajax JQuery
  3. Contact form send attachment using jquery ajax in PHP
  4. Contact form with validation in PHP mailer jquery Ajax script, (PHP code to send email from contact form)

Create database configuration file


	// Database configuration 
	$dbHost     = "localhost"; 
	$dbUsername = "root"; 
	$dbPassword = ""; 
	$dbName     = "registration"; 
	// Create database connection 
	$con = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName); 
	// Check connection 
	if ($con->connect_error) { 
	    die("Connection failed: " . $con->connect_error); 

Create HTML for page Fetch users record


<!Doctype html>
<html lang="en">
  <meta charset="utf-8">
  <title>How to Send Bulk Email in PHP using PHPMailer with Ajax JQuery</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="">
  <script src=""></script>
  <script src=""></script>
<style type="text/css">
    float: right;
<div class="container" style="margin-top:50px">
  <h1 style="text-align:center">Send Bulk Email in PHP using PHPMailer with Ajax JQuery</h1><br>
    <div class="row">
      <div class="col-md-12">
        <div id="emailMsg"></div>
        <button type="button" class="btn btn-success" id="sendMail">Send Email</button>
        <table class="table table-striped">

              $query  = "SELECT * FROM users";
              $result = mysqli_query($con, $query);
            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
              <td><?php echo $row['id'] ?></td>
              <td><?php echo $row['name'] ?></td>
              <td><?php echo $row['username'] ?></td>
              <td><?php echo $row['email'] ?></td>
              <td><input type="checkbox" class="email" name="email" value="<?php echo $row['email'] ?>"></td>
            <?php } }else { echo "No record found"; } ?> 

jQuery AJAX Code for Send Bulk Emails

<script type="text/javascript">
      var email = [];

      if (email.length > 0) {
          $("#emailMsg").html('<div class="alert alert-primary">Please wait...!</div>');
            url : "action.php",
            type : "POST",
            data : {email:email},
              if(response == true) {
        $("#emailMsg").html('<div class="alert alert-danger alert-dismissible"><button type="button" class="close" data-dismiss="alert">&times;</button> Plase Select at least one checkbox </div>');

 Create PHP Code for sending bulk emails



if(isset($_POST['email']) && $_POST['email']!=""){

	require_once ('PHPMailer/PHPMailerAutoload.php');

        $emails  = $_POST['email'];

        $subject = "Send Bulk Email in PHP using PHPMailer with Ajax JQuery";

	$message = "<br>Powered By Webs Codex <br><br>Web: <a href='' target='_blank'>Webs Codex</a>";

        $mail = new PHPMailer(true);

	$mail->SMTPDebug = 3;                                 // Enable verbose debug output
	$mail->CharSet="UTF-8";                               // Put right encoding here  
	$mail->isSMTP();                                      // Set mailer to use SMTP
	$mail->Host = '';  					  // Specify main and backup SMTP servers
	$mail->SMTPAuth = true;                               // Enable SMTP authentication
	$mail->Username = 'YourEmailAdress';                  // SMTP username
	$mail->Password = 'YourEmailPassword';                // SMTP password
	$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
	$mail->Port = 587;                                    // TCP port to connect to
	$mail->isHTML(true); 								  // Set email format to HTML
	$mail->SMTPOptions = array(
        'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true

    foreach($emails as $email){
        $mail->setFrom($email, 'Webs Codex');
        $mail->addAddress($email, 'Your Name');              // Add a recipient
        $mail->addReplyTo($email, 'Webs Codex');

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

            $esMessage = true;
    	    $esMessage = false;
        echo'<div class="alert alert-success alert-dismissible">
                <button type="button" class="close" data-dismiss="alert">&times;</button>
                Email sent successfully
        echo'<div class="alert alert-danger alert-dismissible"> 
                <button type="button" class="close" data-dismiss="alert">&times;</button>
                Email not sent to Please try again or type correct email!



How to Send Bulk Emails in PHP using PHPMailer with Ajax jQuery

You can always support by sharing on social media or recommending my blog to your friends and colleagues. If you have any suggestions or problems about this tutorial, please comment on the form below.😊

How to Send Bulk Emails in PHP using PHPMailer with Ajax jQuery


Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Firstly thanks a lot for sharing the code with us & it's been a long time since i've tried to make such a thing for my backend…
    in fact your code works really fine but it has some issues which i'd really hope you'll fix such as of getting duplicated sent emails into your mailbox whenever you select more than 1 email , also an other issue it's about "Please wait…!" it keeps appearing even after i received the email & one more thing it's about the select boxes needed to be unchecked after the successfully email sent
    and i have some suggestions about emails selection , it will be awesome if it gets replaced by (Select ALL checkbox) & also to add a custom messages form (where you can add dynamically your subject and message…
    Thanks you very much for your attention.