【发布时间】: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->setAddress($row['email'])而不是$mail->addBCC() -
这只是来自我的本地主机服务器 xampp 的虚拟数据..
-
@DrKey Id' 假设密件抄送在这里非常重要。
-
是的..密件抄送很重要