【问题标题】:emails is sent multiple times with phpemailer使用 phpmailer 多次发送电子邮件
【发布时间】:2017-03-14 22:40:09
【问题描述】:

我正在尝试使用 phpemailer 从我的服务器发送电子邮件。它工作正常,除了一件事:

<?php

$serverName = "xxx.x.x.xxx"; //serverName\instanceName
$connectionInfo = array( "Database"=>"test", "UID"=>"xxxxxx", "PWD"=>"xxxxxx");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}


$sqll=sqlsrv_query($conn,"select bla from table");


//while($row=sqlsrv_fetch_array($sql))

//  {

    //  $cnp=substr($row['bla'],-6);

//      echo $bla;
//      echo "<br>";

//  }
$con=mysqli_connect("localhost","root","","database");


$sql=mysqli_query($con,"select * from table");



if(isset($_POST['submitted'])){


    require 'phpmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;  
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = "xxxxxx";
//Set the SMTP port number - likely to be 25, 465 or 587
$mail->Port = xxx;

$mail->SMTPSecure = 'ssl';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication
$mail->Username = "xxxxxx";
//Password to use for SMTP authentication
$mail->Password = "xxxxxx";
//Set who the message is to be sent from
$mail->setFrom('xxxxxx');
//Set an alternative reply-to address
$mail->addReplyTo('xxxxxx');
//Set who the message is to be sent to



while($row=mysqli_fetch_array($sql))

    {
        echo $row['bla'];
        echo "<br>";


        $mail->AddBCC($row['bla2']);



//Set the subject line
$mail->Subject = 'text text';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body

//Replace the plain text body with one created manually
$mail->Body = "test test<br>link:<a href='http://localhost:8181/?cod=".$bla."'>click</a>";



$mail->IsHTML(true); 




 $mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }


    }


}
?>

<form name="contact" method="post" action="">

<input type="submit" name="submitted" value="Submit">
</form>

问题是:我正在尝试向数据库中的每封电子邮件发送带有不同链接的电子邮件。我把这部分放在了中间:

$mail->AddBCC($row['email']);



//Set the subject line
$mail->Subject = 'text text';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body

//Replace the plain text body with one created manually
$mail->Body = "test test<br>link:<a href='http://localhost:8181/?cod=".$bla."'>click</a>";



$mail->IsHTML(true); 




 $mail->send()) {
        echo 'Message could not be sent.';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Message has been sent';
    }


    }

电子邮件正在发送,但例如,如果我的表中有 3 封电子邮件,则电子邮件 1 发送电子邮件 3 次,电子邮件 2 发送 2 次,电子邮件 3 发送一次。这是为什么?为什么电子邮件会被发送 3 次到我表中的第一封电子邮件?

【问题讨论】:

  • 这不能回答您的问题,但请不要说这些是您包含在上述脚本中的真实邮件凭据?还有你为什么要连接我的sql server和mysql?
  • 只需使用$mail-&gt;setAddress($row['email']) 而不是$mail-&gt;addBCC()
  • 这只是来自我的本地主机服务器 xampp 的虚拟数据..
  • @DrKey Id' 假设密件抄送在这里非常重要。
  • 是的..密件抄送很重要

标签: php php-5.5


【解决方案1】:

我认为,问题在于您将电子邮件添加到密件抄送,因此,在第一个循环中,您将其发送到第一封电子邮件,在第二个循环中,您将发送到第一封电子邮件,而新的添加了第二封电子邮件,等等...

您应该在添加新电子邮件之前清除密件抄送。

【讨论】:

    【解决方案2】:

    $mail-&gt;send()之后调用$mail-&gt;ClearBCCs()

    清除密件抄送数组中分配的所有收件人。返回无效。

    --或--

    将您的$mail-&gt;send() 移到循环之后,以一次发送所有密件抄送地址的电子邮件。 (您可能需要根据邮件服务器分批执行此操作)

    【讨论】:

      【解决方案3】:

      我在发送电子邮件后添加了以下行:

      $mail->ClearAllRecipients();
      

      现在电子邮件只发送一次。谢谢大家!

      【讨论】:

        猜你喜欢
        • 2014-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多