【发布时间】:2017-10-02 06:06:29
【问题描述】:
我在使用 PHPMailer 发送多个由逗号分隔的电子邮件 cc 时遇到问题。 .我正在使用 PHP 7。
我的 mysql 数据库中的这些数据包含单个用户帐户中的多个电子邮件地址,以逗号 (,) 分隔。
我已经关注了这个问题here 的答案,但它不起作用。电子邮件只能发送到 john@mail.com 。 billy@mail.com 没有收到任何邮件。
我尝试回显 var_dump(maildbs);
输出显示没有昏迷。 .
john@mail.combilly@mail.com
这是我的 PHP 代码。 .
sendmail.php
<?php
$connect = mysqli_connect("", "", "", "");
global $connect;
if(isset($_POST['Submit'])){
$staffname = $_POST['staffname'];
$sql = "SELECT * FROM table WHERE staff_name ='$staffname'";
$get = mysqli_query($connect,$sql);
if($get && mysqli_num_rows($get) > 0 )
{
while($row = mysqli_fetch_assoc($get))
{
$maildbs = explode(',',$row["email_address"]);
foreach($maildbs as $maildb){
date_default_timezone_set('Etc/UTC');
require_once '../PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "host";
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->setFrom('sender@mail.com', 'Sender');
$mail->addAddress('mail@mail.com','receipent');
$mail->addCC($maildb);
$mail->Subject = 'PHPMailer SMTP without auth test';
$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__));
$mail->AltBody = 'This is a plain-text message body';
$mail->Body = 'body content';
$mail->addAttachment('images/phpmailer_mini.png');
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
}
mysqli_free_result($get);
}
}
?>
<!DOCTYPE html>
<html><title>Test Email</title></head>
<body>
<table>
<form action="sendmail.php" method="POST">
<tr>
<td>Staff Name :</td>
<td><input type="text" name="staffname" value="" ></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="send email"></td>
</tr>
</form>
</table>
</body>
</html>
感谢有人可以提供帮助。
【问题讨论】:
-
这是非常低效的。查看the mailing list example provided with PHPMailer 了解如何做得更好。使用
addCc()和addBcc()一次添加一个抄送或密件抄送地址。还要注意clearAddresses()的作用。