【问题标题】:Send multiple rows in one email在一封电子邮件中发送多行
【发布时间】:2015-05-17 03:32:40
【问题描述】:

我希望能够将数据库中的多行发送到一封电子邮件中。到目前为止,我得到的只是两封(或更多)电子邮件,每封电子邮件都包含一行。如何将所有内容都放入一封电子邮件?

这是我的代码。将 mail() 保留在 while 循环之外只会给我最后一个条目。将其保留在 while 循环中会发送两封电子邮件。

        $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@gmail.com";
          $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); }

如果有人能在正确的方向上推动我,我会非常高兴!

【问题讨论】:

    标签: php mysql email


    【解决方案1】:

    我想你只想重复这个字符串。

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

    所以它应该在循环中,而其他不应该。喜欢:

    $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@gmail.com";
    $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); 
    

    【讨论】:

    • 不幸的是,这仍然只给了我最后一个条目!
    • 谢谢!像魅力一样工作!
    【解决方案2】:

    问题在于您在 while 循环中有 mail() 函数,因此对于每个循环,该函数都会发送一封电子邮件。
    您可以从循环中提取 mail(),并且只收集循环内需要的行,如下所示:

        $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@gmail.com";
        $subject = "Order";          
        $headers = 'From: Email <no-reply@someemailaddress>' . "\r\n" .
              'Reply-To: someemailaddress' . "\r\n" .
              'X-Mailer: PHP/' . phpversion();
        mail($to, $subject, $emailBody, $headers); }
    

    【讨论】:

      猜你喜欢
      • 2011-02-08
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多