循环 PHPMailer 时缺少消息文本

Missing message text while looping PHPMailer

这是我的代码:

// $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 个地址,消息将完全像这样显示:

Dear Purchasing,
You have NOTES from COATS REJO INDONESIA with PO : NORT170283-014950-L
Here the list :
1. ITEM :EMB THREAD TEX 18 DRYVENT LOGO ( KXON ) COLOR/SIZE :ASPHALT GREY/SYLCO TEX 18 notes

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

Dear Purchasing,
You have NOTES from COATS REJO INDONESIA with PO : NORT170283-014950-L
Here the list :

同样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 ;
    }

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

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