Множественный адрес почтовой программы в PHP

Опубликовано: 20 Февраля, 2022

В этой статье мы продемонстрируем, как мы можем отправлять почту на несколько адресов из базы данных с помощью PHP.

Библиотека PHPMailer используется для безопасной отправки любого электронного письма с неизвестного адреса электронной почты на любой почтовый идентификатор с использованием кода PHP через веб-сервер XAMPP для этого проекта.

Процесс установки для всех предварительных условий, упомянутых в разделе «Как отправить электронное письмо по ссылке PHPMailer».

Предварительное условие: для этого проекта требуются следующие файлы PHP.

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

Пожалуйста, следуйте инструкциям.

  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”

    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 PHPMailerPHPMailerPHPMailer();
       
    // 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 makeing 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 recieved by the user

    Multiple recievers at the same time.