Ellinor
Ellinor

Reputation: 323

Send multiple rows in one email

I want to be able to send multiple rows from a database into one email. So far all I get is two (or more) emails containing one row in each. How do I go about getting everything into the one email?

This is my code. Keeping the mail() outside the while loop only gives me the last entry. Keeping it in the while loop sends two emails.

        $sql = "SELECT productid, kind, qty, price, GROUP_CONCAT(product) as product FROM orderitems LEFT JOIN Products ON orderitems.code = Products.productid WHERE orderitems.customerid = $customerid GROUP BY productid";
        $result = mysqli_query($db, $sql) or die(mysqli_error($db));
        while($row = mysqli_fetch_array($result)) {
          $product = $row['product'];
          $productid = $row['productid']; 

          $to = "[email protected]";
          $subject = "Order";
          $emailBody = "ID: ".$product."\n"."Product: ".$productid."\n";
          $emailBody .= "Total: ".$total."\n";
          $headers = 'From: Email <no-reply@someemailaddress>' . "\r\n" .
              'Reply-To: someemailaddress' . "\r\n" .
              'X-Mailer: PHP/' . phpversion();
          mail($to, $subject, $emailBody, $headers); }

I'd be really happy if someone could give me a nudge in the right direction!

Upvotes: 1

Views: 1256

Answers (2)

SeriousDron
SeriousDron

Reputation: 1346

I think you only want to repeat this string.

ID: ".$product."\n"."Product: ".$productid."\n";

So it should be in loop and other should not. Like:

$sql = "SELECT productid, kind, qty, price, GROUP_CONCAT(product) as product FROM orderitems LEFT JOIN Products ON orderitems.code = Products.productid WHERE orderitems.customerid = $customerid GROUP BY productid";
$result = mysqli_query($db, $sql) or die(mysqli_error($db));

$emailBody = '';
$total = 0;//I dont know what is total for you

while($row = mysqli_fetch_array($result)) {
    $product = $row['product'];
    $productid = $row['productid']; 

    $emailBody .= "ID: ".$product."\n"."Product: ".$productid."\n";
    $total = $total + 1; //Just for example

}
$to = "[email protected]";
$subject = "Order";
$emailBody .= "Total: ".$total."\n";
$headers = 'From: Email <no-reply@someemailaddress>' . "\r\n" .
     'Reply-To: someemailaddress' . "\r\n" .
     'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $emailBody, $headers); 

Upvotes: 1

nik.longstone
nik.longstone

Reputation: 254

The problem is that you have the mail() function inside the while loop, so for each loop the function send an email.
You could extract the mail() from the loop, and only collect the rows you need inside the loop, something like that:

    $emailBody = '';
    while($row = mysqli_fetch_array($result)) {
      $product = $row['product'];
      $productid = $row['productid']; 
      $emailBody .= "ID: ".$product."\n"."Product: ".$productid."\n";
      $emailBody .= "Total: ".$total."\n";
    }

    $to = "[email protected]";
    $subject = "Order";          
    $headers = 'From: Email <no-reply@someemailaddress>' . "\r\n" .
          'Reply-To: someemailaddress' . "\r\n" .
          'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $emailBody, $headers); }

Upvotes: 0

Related Questions