在php中发送多个电子邮件行和多个产品列表

时间:2016-11-09 09:46:48

标签: php mysql

我有一个表用于产品(名称,价格)有10行,另一个表用于客户端(客户端名称和电子邮件),其中50行有mysql数据库。我想将我的产品列表(10行)发送给我的活跃客户。

这样每个客户端都可以将所有10行产品作为一封电子邮件获取。 我试图将产品和客户端表放在循环中,但我只能发送一对一的电子邮件。

以下是我的代码:

$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:info@welcare.com\r\n";
$headers .= "Content-type: text/html\r\n";
$to = $email;
$mail_result = mail($to, $subject, $mail_body, $headers);

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

下面的链接显示了如何为一个静态电子邮件地址循环多个产品行,但我希望能够将电子邮件发送到多个电子邮件客户端,其中循环产品行作为电子邮件的主体,全部来自使用php的mysql数据库表。

language specification

任何人都可以帮助我

由于

1 个答案:

答案 0 :(得分:1)

您需要将调用mail置于循环客户端的循环中:

...
$sql = mysqli_query($conn, "SELECT email FROM clients WHERE type='Active'");
$numRows = mysqli_num_rows($sql); 
$subject = "New products";
$headers  = "From:info@welcare.com\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);
}