【问题标题】:Send Multiple Email in PHPMailer在 PHPMailer 中发送多个电子邮件
【发布时间】:2016-02-21 04:19:33
【问题描述】:

我在使用 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>

用户电子邮件取自数据库。下面是我的 mail.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.

【问题讨论】:

    标签: php email phpmailer


    【解决方案1】:

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

    emailKJ=<?php echo $emailKJ_string;?>
    

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

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

    【讨论】:

    • 感谢您提供详细信息。将尝试删除[] 并提供更新
    【解决方案2】:

    你在这里发送数组而不是字符串。

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

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

    $to = $_GET['emailKJ'];
    

    代替

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

    【讨论】:

    • 感谢您提供详细信息。我试过了,但现在出现了这个错误2016-02-21 04:42:47 Invalid address: (addAnAddress to): kamran@ktmparking.com.my,khairulamran.nazri@gmail.com Message could not be sent.Mailer Error: You must provide at least one recipient email address.
    • echo $emailKJ_string 的值是多少; ?
    • 只需在 url 中使用 emailKJ 而不是 emailKJ[] 并且您之前的代码没有变化,试试这个
    • 回声的值是例如; email1@gmail.com、email2@gmail.com、email3@gmail.com。我将尝试删除 [] 并更新结果
    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多