在一封电子邮件中发送多行 [英] Send multiple rows in one email

查看:137
本文介绍了在一封电子邮件中发送多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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@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); }

如果有人能朝我正确的方向推动我,我将非常高兴!

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

推荐答案

我认为您只想重复此字符串。

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@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); 

这篇关于在一封电子邮件中发送多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆