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
- How to Send Email from localhost in PHP using SMTP
- How to Send Bulk Emails in PHP using PHPMailer with Ajax JQuery
- Contact form send attachment using jquery ajax in PHP
- Contact form with validation in PHP mailer jquery Ajax script, (PHP code to send email from contact form)
Create database configuration file
config.php
<?php // 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
index.php
<!Doctype html> <html lang="en"> <head> <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="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/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.0/js/bootstrap.min.js"></script> </head> <body> <style type="text/css"> #sendMail{ margin-bottom:15px; float: right; } </style> <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"> <thead> <tr> <th>S.no.</th> <th>Name</th> <th>Username</th> <th>Email</th> <th>Select</th> </tr> </thead> <tbody> <?php require_once('config.php'); $query = "SELECT * FROM users"; $result = mysqli_query($con, $query); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { ?> <tr> <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> </tr> <?php } }else { echo "No record found"; } ?> </tbody> </table> </div> </div> </div> </body> </html>
jQuery AJAX Code for Send Bulk Emails
<script type="text/javascript"> $(document).ready(function(){ $("#sendMail").click(function(){ var email = []; $(".email:checked").each(function(){ email.push($(this).val()); }); if (email.length > 0) { $("#emailMsg").html('<div class="alert alert-primary">Please wait...!</div>'); $.ajax({ url : "action.php", type : "POST", cache:false, data : {email:email}, success:function(response){ if(response == true) { $("#emailMsg").html(response); }else{ $("#emailMsg").html(); } } }); }else{ $("#emailMsg").html('<div class="alert alert-danger alert-dismissible"><button type="button" class="close" data-dismiss="alert">×</button> Plase Select at least one checkbox </div>'); } }); }); </script>
Create PHP Code for sending bulk emails
action.php
<?php 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='https://www.webscodex.com/' 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 = 'smtp.gmail.com'; // 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; if($mail->send()){ $esMessage = true; }else{ $esMessage = false; } } if($esMessage){ echo'<div class="alert alert-success alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> Email sent successfully </div>'; exit; }else{ echo'<div class="alert alert-danger alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> Email not sent to Please try again or type correct email! </div>'; exit; } } ?>
Output

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.😊
wow great man, i'll try it first, thank you
thank you
bhai downoad link is not working
solve download issue you can download full source code
Adding a select all check box will be really great for this
this not working haged at plese wait..!
Can you please tell me. What you are facing problem.
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.
webcodex to the moon thank you for the help repping this source code all the way from nigeria