将电子邮件发送到电子邮件列表

时间:2019-01-11 10:10:46

标签: php email gmail phpmailer

我正在使用最新版本的PHPMailer向同一域中的3封电子邮件发送一封电子邮件。

所有电子邮件,我从中发送电子邮件的电子邮件,以及我向其发送消息的其他三封电子邮件,都在同一域中,这是一个企业Gmail帐户。

代码如下:

//Including PHPMailer files
require_once('phpmailer/src/phpmailer.php');
require_once('phpmailer/src/SMTP.php');
require_once('phpmailer/src/Exception.php');

$msg = '';

//List of emails
$recipients = array('info@mydomain.com', 'help@mydomain.com', 'schedule@mydomain.com');

//Initializing PHPMailer
$mail = new PHPMailer\PHPMailer\PHPMailer();                              // Passing `true` enables exceptions

try {

    //Sender data
    $mail->setFrom('admin@mydomain.com', 'Admin');
    $mail->addReplyTo('info@mydomain.com', 'Admin');

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Hello World!';
    $mail->Body    = 'Hello World!';
    $mail->AltBody = 'Hello World!';

    //Loop through the emails list
    foreach ($recipients as $recipient) {

        $mail->addAddress($recipient);

        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $recipient) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        } else {
            echo "Message sent to :"  . ' (' . str_replace("@", "&#64;", $recipient) . ')<br />';
        }

        // Clear all addresses and attachments for next loop
        $mail->clearAddresses(); 
        $mail->clearAttachments();

    }

} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

当我在同一域上运行脚本时,得到以下输出:

Message sent to : (info@mydomain.com)
Message sent to : (help@mydomain.com)
Message sent to : (schedule@mydomain.com)

但是当我检查这些电子邮件的收件箱时,找不到该电子邮件。

我试图通过以下方式获取mail.log文件:

ini_set('mail.log', __DIR__ . 'mail.log');

但是它没有用,我不能使用$mail->SMTPDebug = 2;,因为我没有使用SMTP

0 个答案:

没有答案