thinkphp6 用PHPMailer 发送失败原因排查


Q1:中文乱码问题

A1:$mail->CharSet = "UTF-8";

Q2:从数据库中取值然后多发问题

A2:增加一行:$mail->ClearAddresses();

====================================

require ("class.phpmailer.php");

 

 // 下载的文件必须放在该文件所在目录

 

$mail = new PHPMailer(); //建立邮件发送类

$link = mysql_connect("192.168.0.00", "root", "pass") or die('Could not connect: ' . mysql_error());

 

// echo 'Connected successfully';

 

mysql_query("set  names 'gb2312'");

mysql_select_db("oaerp") or die('Could not select database');

mysql_query("set character set 'utf8'"); //读库

$sql1 = "select * from sml"; //获取信息列表

echo $sql1;

$result = mysql_query($sql1);

 

//

 

while ($row = mysql_fetch_array($result))

{

$mail->ClearAddresses();

$n = $row["Name"];

$mail->CharSet = "UTF-8";

$address = $row["email"];

$mail->IsSMTP(); // 使用SMTP方式发送

$mail->Host = "smtp.mail.com"; // 您的企业邮局域名

$mail->SMTPAuth = true; // 启用SMTP验证功能

$mail->Username = "test@test.com"; // 邮局用户名(请填写完整的email地址)

$mail->Password = "*****"; // 邮局密码

$mail->Port = 25;

$mail->From = "rec@test.com"; //邮件发送者email地址

$mail->FromName = "system"; //发件人名称

$mail->AddAddress("$address", "$n"); //收件人地址,可以替换成任何想要接收邮件的email信箱,格式是AddAddress("收件人email","收件人姓名")

 

// $mail->AddReplyTo("", "");

// echo $mail;

// $mail->AddReplyTo("", "");

// $mail->AddAttachment("/var/tmp/file.tar.gz"); // 添加附件

// $mail->IsHTML(true); // set email format to HTML //是否使用HTML格式

 

$mail->Subject = "测试邮件"; //邮件标题

$Body = "$n,你好,这是测试邮件";

$mail->Body = $Body; //邮件内容

$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; //附加信息,可以省略

echo $address;

if (!$mail->Send())

{

echo "邮件发送失败.

";

echo "错误原因.:

" . $mail->ErrorInfo;

exit;

}

 

echo "邮件发送成功

";

}

 

// 释放结果集

 

$rc = mysql_affected_rows();

echo "发送条数: " . $rc;

mysql_free_result($result);

 

// 关闭连接

 

mysql_close($link);

?>