Open In App

Mailer multiple address in PHP

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be demonstrating how we can send the mail to the multiple addresses from the database using the PHP.

PHPMailer library is used to send any email safely from the unknown e-mail to any mail id using PHP code through XAMPP web-server for this project.

Installation process for all the pre-requisites mentioned in this How to send an email using PHPMailer link.

Prerequisite: The following PHP files are required for this project.

  • phpMailerautoLoad.php
  • phpMailer.php
  • OAuth.php
  • SMTP.php

Please follow the steps.

  1. Go to the htdocs of the xampp folder. Create the folder as shown in the image, then install the PHPMailer library into the that folder.

  2. Create the “index.php” file where the code implementation should be done.

  3. Create the database so that we can manually store the email_ID of the user.

    db_name="mailer";
    table_name="users";
    
  4. Create the dataBase=”mailer”

  5. Create the table names “users”

  6. PHP code:

    PHP




    <?php
      
    $conn= mysqli_connect("localhost"
                    "root", "", "mailer");
       
    require "PHPMailer.php";
    require "SMTP.php" ;
    require "PHPMailer-master/src/Exception.php" ;
      
    // Server settings
    $mail = new PHPMailer\PHPMailer\PHPMailer();
       
    // Enable verbose debug output
    $mail->isSMTP(); 
      
    // Send using SMTP
    $mail->Host = "smtp.gmail.com";
    $mail->SMTPAuth = true;
      
    // SMTP username
    $mail->Username = "YOUR SMTP USERNAME"
      
    // SMTP password                   
    $mail->Password = "YOUR PASSWORD";
    $mail->SMTPAuth = "tls";
    $mail->Port = 587;           
      
    //Recipients
    // This email-id will be taken
    // from your database
    $mail->setFrom("###");
      
    // Selecting the mail-id having
    // the send-mail =1
    $sql = "select * from users where send_mail=1";
      
    // Query for the making the connection.
    $res = mysqli_query($conn, $sql);
      
    if(mysqli_num_rows($res) > 0) {
        while($x = mysqli_fetch_assoc($res)) {
            $mail->addAddress($x['email']);
        }
      
        // Set email format to HTML
        $mail->isHTML(true);
        $mail->Subject = 
            "Mailer Multiple address in php";
              
        $mail->Body = "Hii </p>Myself </h1>Rohit 
        sahu</h1> your Article has been acknowledge 
        by me and shortly this article will be 
        contributing in</p> <h1>Geeks for Geeks !</h1>";
          
        $mail->AltBody = "Welcome to Geeks for geeks";
           
        if($mail->send())
        {
           echo "Message has been sent check mailbox"
        }
        else{
            echo "failure due to the google security";
        }
           
    ?>

    
    

    After executing the code, mails will be send to multiple ids.

    Output:

    Email received by the user

    Multiple receivers at the same time.



    Like Article
    Suggest improvement
    Previous
    Next
    Share your thoughts in the comments

Similar Reads