How to send PHP mailer to multiple email addresses received via MySQL query

前端 未结 2 1316
北海茫月
北海茫月 2021-01-14 07:26

I run a MySQL query to get firstname, lastname and email from a table where \'notify\' is set to YES, like this below. And in the while loop, I create all the info which I

相关标签:
2条回答
  • 2021-01-14 08:17

    Given the other answers, am submitting the following.

    As I stated in comments, have pulled from their example the following and slightly modified to also show the person's name should you want to use that.

    You can add to it also, using different columns while assigning a variable to them.

    Borrowed from https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps

    N.B.: You will need to modify the query/column names slightly and possibly some of the code since you did not post your full code.

    The following is a working example of what I used, and hope it serves you well.

    <?php 
    
    include ('/path/to/database_connection.php');
    
    error_reporting(E_STRICT | E_ALL);
    date_default_timezone_set('Etc/UTC');
    
    require 'PHPMailerAutoload.php';
    
    $mail = new PHPMailer;
    
    $mail->isSMTP();
    $mail->Host = 'xxx';
    $mail->SMTPAuth = true;
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    
    $mail->SMTPSecure = 'tls'; // if required
    $mail->Port = 587; // or use the preferred port of your choice
    $mail->Username = 'xxx';
    $mail->Password = 'xxx';
    
    $mail->addAddress('your_email@example.com', 'John');
    $mail->setFrom('email@example.com', 'List manager');
    $mail->addReplyTo('email@example.com', 'List manager');
    $mail->Subject = "PHPMailer Simple database mailing list test";
    //Same body for all messages, so set this before the sending loop
    //If you generate a different body for each recipient (e.g. you're using a templating system),
    //set it inside the loop
    
    //msgHTML also sets AltBody, but if you want a custom one, set it afterwards
    $mail->AltBody = 'To view the message, please use an HTML compatible email viewer!';
    
    $result = mysqli_query($connection, "SELECT user, email FROM table WHERE col = 'x'");
    
        foreach ($result as $row) { //This iterator syntax only works in PHP 5.4+
    
            $user = $row['user'];
    
            $body = "Hello $user, <br><br>This is the HTML message body <b>in bold!</b>";
    
            $mail->msgHTML($body);
    
            $mail->addBCC($row['email'], $row['user']);
    
        /*
            if (!empty($row['photo'])) {
                $mail->addStringAttachment($row['photo'], 'YourPhoto.jpg'); //Assumes the image data is stored in the DB
            }
        */
    
            if (!$mail->send()) {
                echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
                break; //Abandon sending
            } else {
                echo "Message sent to:" . $row['user'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
                //Mark it as sent in the DB
    
        /* UPDATE the table if needed
                mysqli_query(
                    $connection,
                    "UPDATE mailinglist SET sent = true WHERE email = '" .
                    mysqli_real_escape_string($connection, $row['email']) . "'"
                );
        */
    
            }
            // Clear all addresses and attachments for next loop
            $mail->clearAddresses();
            $mail->clearAttachments();
        }
    
    0 讨论(0)
  • 2021-01-14 08:21

    Your array isn't well constructed, use the following:

    while ($row = mysqli_fetch_array($getnotify)) {
         $notifyemailscontent[$row['email']] =  "{$row['firstname']}  {$row['lastname']}";
    }
    

    Then, inside the phpmailer block:

    foreach($notifyemailscontent as $email => $name)
    {
       $mail->AddCC($email, $name);
    }
    
    0 讨论(0)
提交回复
热议问题