Sending multiple emails with PHPmailer

前端 未结 2 870
广开言路
广开言路 2021-01-14 14:26

Edit: I forgot I\'d created the SendMail(); function myself, which is why the explanation doesn\'t mention at first what it does.

I\'m having s

相关标签:
2条回答
  • 2021-01-14 15:04

    Added an example below:

    <?php
    /**
     * This example shows how to send a message to a whole list of recipients efficiently.
     */
    
    //Import the PHPMailer class into the global namespace
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    error_reporting(E_STRICT | E_ALL);
    
    date_default_timezone_set('Etc/UTC');
    
    require '../vendor/autoload.php';
    
    //Passing `true` enables PHPMailer exceptions
    $mail = new PHPMailer(true);
    
    $body = file_get_contents('contents.html');
    
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->Port = 25;
    $mail->Username = 'yourname@example.com';
    $mail->Password = 'yourpassword';
    $mail->setFrom('list@example.com', 'List manager');
    $mail->addReplyTo('list@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
    $mail->msgHTML($body);
    //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!';
    
    //Connect to the database and select the recipients from your mailing list that have not yet been sent to
    //You'll need to alter this to match your database
    $mysql = mysqli_connect('localhost', 'username', 'password');
    mysqli_select_db($mysql, 'mydb');
    $result = mysqli_query($mysql, 'SELECT full_name, email, photo FROM mailinglist WHERE sent = FALSE');
    
    foreach ($result as $row) {
        try {
            $mail->addAddress($row['email'], $row['full_name']);
        } catch (Exception $e) {
            echo 'Invalid address skipped: ' . htmlspecialchars($row['email']) . '<br>';
            continue;
        }
        if (!empty($row['photo'])) {
            //Assumes the image data is stored in the DB
            $mail->addStringAttachment($row['photo'], 'YourPhoto.jpg');
        }
    
        try {
            $mail->send();
            echo 'Message sent to :' . htmlspecialchars($row['full_name']) . ' (' . htmlspecialchars($row['email']) . ')<br>';
            //Mark it as sent in the DB
            mysqli_query(
                $mysql,
                "UPDATE mailinglist SET sent = TRUE WHERE email = '" .
                mysqli_real_escape_string($mysql, $row['email']) . "'"
            );
        } catch (Exception $e) {
            echo 'Mailer Error (' . htmlspecialchars($row['email']) . ') ' . $mail->ErrorInfo . '<br>';
            //Reset the connection to abort sending this message
            //The loop will continue trying to send to the rest of the list
            $mail->getSMTPInstance()->reset();
        }
        //Clear all addresses and attachments for the next iteration
        $mail->clearAddresses();
        $mail->clearAttachments();
    }
    
    0 讨论(0)
  • 2021-01-14 15:19

    You haven't posted this code that lets me make this a complete conclusion, but from the Exception and the way you've defined an overriding class inside a function, you probably have class.phpmailer.php loading every time like this:

    require('class.phpmailer.php');
    

    or

    include('class.phpmailer.php');
    

    You should change that line to

    require_once('class.phpmailer.php');
    

    The reason you need to change it to require_once is so that PHP will not load the class file the second time when you try to create the new/second PHPMailer class. Otherwise, the line class PHPMailer throws the __clone() exception.

    0 讨论(0)
提交回复
热议问题