doukeng1922 2017-03-14 22:40
浏览 86

使用phpmailer多次发送电子邮件

I'm trying to use the phpemailer to send emails from my server. It's working fine except one thing:

<?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>

The problem is: I'm trying to send an email with a different link to every email I've got in my database. I had put this part between the while:

$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';
    }


    }

The emails are being sent but for example if i have 3 emails in my table then to email1 is sent the email 3 times, to email2 is sent 2 times and the email3 is sent one time. Why is that? why does the email being sent 3 times to the first email in my table?

  • 写回答

3条回答 默认 最新

  • duanjie1339 2017-03-14 22:45
    关注

    I think, the problem is that you are adding the emails to the BCC, therefore, in the first loop, you are sending it to the first email, on the second, you are sending to the first email, and the newly added second email, and... so on...

    You should clean the bcc before adding a new email.

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序