在 PHPMailer 中发送多封电子邮件


Send Multiple Email in PHPMailer

我在使用phpmailer发送多封电子邮件时遇到问题。如果我只发送到 1 封电子邮件,则可以毫无错误地发送电子邮件。尝试搜索并进行测试,但结果失败。有人可以检查我做错了什么,以下是代码。

我正在使用GET将数据发送到第 mail.php 页以处理邮件。以下是我对获取链接的编码,

//php and mySQL coding to select email from db
$emailKJ_string = implode(",",$emailKJ);
<iframe style="border:0;" src="http://www.ktmparking.com.my/staff/mail.php?emailKJ[]=<?php echo $emailKJ_string;?>&nama=<?php echo $nama;?>"></iframe>

用户电子邮件取自数据库。以下是我的邮件代码.php用于PhpMailer处理,

if(isset($_GET['nama'])){
    $nama=$_GET['nama'];
}
require '../staff/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '*my_host_name*';  // Specify main and backup SMTP servers
$mail->SMTPDebug = 2;
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = '*myusermane*';                 // SMTP username
$mail->Password = '*mypassword*';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                  // TCP port to connect to 
$mail->setFrom('info@ktmparking.com.my', 'HR-KTMBCP');
$to=explode(",",$_GET['emailKJ']);
foreach($to as $emails)
{
    $mail->AddAddress($emails);
}
//other codes for phpmailer

这是我收到的错误,

Warning: explode() expects parameter 2 to be string, array given in /home/ktm10001/public_html/staff/mail.php on line 22
Warning: Invalid argument supplied for foreach() in /home/ktm10001/public_html/staff/mail.php on line 23
Message could not be sent.Mailer Error: You must provide at least one recipient email address.

如果您用昏迷分隔电子邮件 - 只需删除括号:

emailKJ=<?php echo $emailKJ_string;?>

如果你想通过URI传递数组(并且不要在邮件中爆炸.php) - 你应该以这种方式传递每个电子邮件地址:

emailKJ[]=first@email.com&emailKJ[]=second@email.com&emailKJ[]=third@email.com...

您在此处发送的是数组而不是字符串。

见 http://www.w3schools.com/php/func_string_explode.asp

试试这个$_GET['emailKJ']因为是电子邮件数组

$to = $_GET['emailKJ'];

而不是

$to=explode(",",$_GET['emailKJ']);