用phpemailer多次发送电子邮件

时间:2017-03-14 22:40:10

标签: php php-5.5

我正在尝试使用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封电子邮件,那么发送电子邮件3发送电子邮件3次,发送电子邮件2发送2次,发送电子邮件3一次。这是为什么?为什么电子邮件被发送3次到我桌上的第一封电子邮件?

3 个答案:

答案 0 :(得分:0)

我认为,问题是您要将电子邮件添加到BCC,因此,在第一个循环中,您将其发送到第一封电子邮件,第二封,您要发送到第一封电子邮件,以及新邮件添加了第二封电子邮件,等等......

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

答案 1 :(得分:0)

$mail->ClearBCCs()

之后致电$mail->send()
  

清除在BCC阵列中分配的所有收件人。返回void。

- 或 -

将您的$mail->send()移至循环后,立即发送所有BCC地址的电子邮件。 (您可能需要批量执行此操作,具体取决于邮件服务器)

答案 2 :(得分:0)

我发送电子邮件后添加了以下内容:

$mail->ClearAllRecipients();

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