使用phpemailer发送多次电子邮件
我正在尝试使用phpemailer从我的服务器发送电子邮件。除了一件事外,它工作得很好:使用phpemailer发送多次电子邮件,php,php-5.5,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, $connectionInf
<?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->send()之后调用$mail->ClearBCCs()
清除在密件抄送阵列中分配的所有收件人。返回void
--或--
将$mail->send()
移动到循环后,即可一次发送所有密件抄送的电子邮件。(您可能需要根据邮件服务器分批执行此操作)我在发送电子邮件后添加了以下行:
$mail->ClearAllRecipients();
现在电子邮件只发送一次。谢谢大家 这并不能回答您的问题,但请不要说这些是您在上面的脚本中包含的真实邮件凭据?为什么要连接到我的sql server和mysql?只需使用$mail->setAddress($row['email'])
而不是$mail->addBCC()
这只是来自本地主机服务器xampp的虚拟数据。@DrKey Id'假设BCC在这里非常重要。是的。..BCC很重要
$mail->ClearAllRecipients();