user3669874
user3669874

Reputation: 13

Send multiple email rows and multiple product list in php

I have one table for products (name, price) with 10 rows and another table for clients(client names and emails) with 50 rows with mysql database. I want to send my product list (10 rows) to my clients that are active.

So that each client will get all the 10 rows of products as one email. I tried to put the product and client table in a loop, but I can only send one to one email.

Below is my code:

$sql = mysqli_query($conn, "SELECT * FROM products");
$numRows = mysqli_num_rows($sql); 
$mail_body = '';

while($row = mysqli_fetch_array($sql)){
  $id = $row["pdid"];
  $price = $row["price"];
  $product = $row["product"];
  $mail_body = '';
}
$sql = mysqli_query($conn, "SELECT email FROM clients WHERE  type='Active'");
$numRows = mysqli_num_rows($sql); 
$email = '';
while($row = mysqli_fetch_array($sql)){
  $email = $row["email"];
  $name = $row["firstname"];
}
$subject = "New products";
$headers  = "From:[email protected]\r\n";
$headers .= "Content-type: text/html\r\n";
$to = $email;
$mail_result = mail($to, $subject, $mail_body, $headers);

if ($mail_result) {     
    echo 'ok';
}

The link below shows how to loop multiple product rows for one static email address, but I want to be able to send the email to multiple emails clients with looping product rows as body of the email all from mysql database table using php.

Send multiple rows in one email

Can anyone help me

Thanks

Upvotes: 1

Views: 579

Answers (1)

wroniasty
wroniasty

Reputation: 8052

You need to put the call to mail inside the loop that iterates over clients:

...
$sql = mysqli_query($conn, "SELECT email FROM clients WHERE type='Active'");
$numRows = mysqli_num_rows($sql); 
$subject = "New products";
$headers  = "From:[email protected]\r\n";
$headers .= "Content-type: text/html\r\n";

while($row = mysqli_fetch_array($sql)){
   $email = $row["email"];
   $name = $row["firstname"];
   $mail_result = mail($email, $subject, $mail_body, $headers);
}

Upvotes: 1

Related Questions