phpemailconcatenationphpmailermultiple-resultsets

Missing message text while looping PHPMailer


This is my code:

// $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 ;
}

If I'm using just 1 address, the message show perfectly like these:

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

But if I'm using multiple emails like these: usera@email.com;userb@email.com I get:

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

There's no item in there at same po. Did I do something wrong on looping the messages?

Thank you from mickmackusa, so i try this, i'm change position looping my list item before foreach, and make varibale in looping my list item. and last i'm using ny variable looping inside foreach. And working. Here the code i changed:

// $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 ;
    }

Solution

  • Use mysqli_data_seek($tbl_note,0); before your while loop to reset the pointer when you have more than one iteration.

    Better practice would be to build your list message part from your resultset and set that text to a variable outside of your loops (I don't know where your query is -- it's not in your code) and just use that variable inside of your loop to concatenate the messages.