循环PHPMailer时缺少消息文本

时间:2017-05-19 03:12:19

标签: php email concatenation phpmailer multiple-resultsets

这是我的代码:

// $mail->addReplyTo('noreply@pbrx.co.id', 'No Reply');
$subjectTo = explode(';', $Addres);
//var_dump($Addres);
array($subjectTo);
// var_dump(array($subjectTo));
foreach ($subjectTo as $key => $value) {
    // $mail->isSMTP();     
    $mail->Protocol = 'smtp';
    $mail->Host = '192.168.40.220';
    $mail->Port = 25;
    $mail->SMTPDebug  = false;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->From = 'noreply1@pbrx.co.id';
    $mail->FromName = 'Notes Email';
    $mail->addAddress($value);

    $subject = $supplier.' have sent NOTES FROM PO - '.$PO;
    if(substr($PO,-1)=='L'){
        $message = "Dear Purchasing,\nYou have NOTES from ".$supplier." with PO : ".$PO."\n Here the list :";               
    }else{
        $message = "Dear Merchandiser,\nYou have NOTES from ".$supplier." with PO : ".$PO."\n Here the list :";             
    }

    $nomor = 1;
    //var_dump($tbl_note->fetch_assoc());
    while ($val = $tbl_note->fetch_assoc()) {
        $message .= "\n".$nomor.". ITEM :".$val['itemdesc']." ( ".$val["matcontents"]." ) COLOR/SIZE :".$val['colorcode']."/".$val['size']." notes : ".$val['PIReason'];
        $nomor = $nomor + 1;
    }

    $mail->Subject = $subject;
    $mail->Body = $message ;
}

如果我只使用1个地址,则消息显示如下:

  

亲爱的采购,
  您有来自COATS REJO INDONESIA的注意事项:PO:NORT170283-014950-L   列表如下:
  1.项目:EMB THREAD TEX 18 DRYVENT LOGO(KXON)颜色/尺寸:沥青灰/ SYLCO TEX 18注意

但如果我使用多个这样的电子邮件:usera@email.com;userb@email.com我会:

  

亲爱的采购,
  您有来自COATS REJO INDONESIA的注意事项:PO:NORT170283-014950-L   列表如下:

那里的po没有项目。我在循环消息时做错了吗?

谢谢mickmackusa, 所以我试试这个, 我改变了在foreach之前循环我的列表项的位置,并在循环我的列表项时创建了varibale。最后我在foreach中使用了ny变量循环。工作。 这里改变了代码:

// $mail->addReplyTo('noreply@pbrx.co.id', 'No Reply');
    $subjectTo = explode(';', $Addres);
    //var_dump($Addres);
    array($subjectTo);
    // var_dump(array($subjectTo));             
        $nomor = 1;
        //var_dump($tbl_note->fetch_assoc());
        while ($val = $tbl_note->fetch_assoc()) {
            $listitem .= "\n".$nomor.". ITEM :".$val['itemdesc']." ( ".$val["matcontents"]." ) COLOR/SIZE :".$val['colorcode']."/".$val['size']." notes : ".$val['PIReason'];
            $nomor = $nomor + 1;
        }
    foreach ($subjectTo as $key => $value) {
        // $mail->isSMTP();     
        $mail->Protocol = 'smtp';
        $mail->Host = '192.168.40.220';
        $mail->Port = 25;
        $mail->SMTPDebug  = false;
        $mail->SMTPSecure = 'tls';
        $mail->SMTPAuth = true;
        $mail->From = 'noreply1@pbrx.co.id';
        $mail->FromName = 'Notes Email';
        $mail->addAddress($value);

        $subject = $supplier.' have sent NOTES FROM PO - '.$PO;
        if(substr($PO,-1)=='L'){
            $message = "Dear Purchasing,\nYou have NOTES from ".$supplier." with PO : ".$PO."\n Here the list :";               
        }else{
            $message = "Dear Merchandiser,\nYou have NOTES from ".$supplier." with PO : ".$PO."\n Here the list :";             
        }
        $message .= $listitem;

        $mail->Subject = $subject;
        $mail->Body = $message ;
    }

1 个答案:

答案 0 :(得分:1)

在while循环之前使用mysqli_data_seek($tbl_note,0);在有多次迭代时重置指针。

更好的做法是在结果集中构建您的list消息部分,并将该文本设置为循环之外的变量(我不知道您的查询在哪里 - 它是' s不在您的代码中)只需在循环中使用该变量来连接消息。