php - 用 PHP 和 MySql 发送电子邮件

标签 php mysql email

您好,我正在尝试使用 while 循环从数据库获取数据来发送电子邮件。电子邮件功能正常,但电子邮件中仅附加一条记录。如何发送所有记录。 我的代码如下。

    $selectorder=$con->query("select * from products , order_details where order_details.order_id='$order_id' AND products.product_id=order_details.product_id");
    while($fetch_order_details=$selectorder->fetch_array())
    {
       $product_detail_id=$fetch_order_details['product_id'];
      $product_detail_unit=$fetch_order_details['unit_price'];
     $product_detail_sub=$fetch_order_details['sub_price'];
       $product_detail_qty=$fetch_order_details['quanitity'];
       $total;
       $pname=$fetch_order_details['product_title'];
        $strMessage = "
        <tr>
        <td>Item Name</td>
        <td>Item Price</td>
        <td>Item Quanitity</td>
        <td>Item Sub Price</td>
        <td>Item Total Price</td>
                    </tr>
                    <tr>
        <td>$pname;</td>
        <td>$product_detail_unit;</td>
        <td> $product_detail_qty;</td>
        <td> $product_detail_sub;</td>
        <td>$total;</td>
                    </tr>";
    }


    $subject = "Your Order Number" .$order_id;
    $message="$strMessage";
    $header="From:<xxx@gmail.com>";
    $to="$customeremail";
    $send=mail($to,$subject,$message,$header);
    if($send)
    { 
       echo "send successfully";
   }
    else
    {
       echo "not successfully";
    }
}

}

最佳答案

更改以下行

Line 0: 
Line 1:   $selectorder=$con->query("select * from products , order_details where order_details.order_id='$order_id' AND products.product_id=order_details.product_id");

Line 0:   $strMessage = ""; 
Line 1:   $selectorder=$con->query("select * from products , order_details where order_details.order_id='$order_id' AND products.product_id=order_details.product_id");

并且

Line 10:   $strMessage = '

Line 10:   $strMessage.= '

这会将字符串附加到变量,而不是每次都覆盖该值。

    $strMessage = "";

    $selectorder=$con->query("select * from products , order_details where order_details.order_id='$order_id' AND products.product_id=order_details.product_id");

    while($fetch_order_details=$selectorder->fetch_array())
    {
        $product_detail_id=$fetch_order_details['product_id'];
        $product_detail_unit=$fetch_order_details['unit_price'];
        $product_detail_sub=$fetch_order_details['sub_price'];
        $product_detail_qty=$fetch_order_details['quanitity'];

        $total; //Why is this in here?

        $pname=$fetch_order_details['product_title'];

        $strMessage.= "
        <tr>
            <td>Item Name</td>
            <td>Item Price</td>
            <td>Item Quanitity</td>
            <td>Item Sub Price</td>
            <td>Item Total Price</td>
        </tr>
        <tr>
            <td>$pname;</td>
            <td>$product_detail_unit;</td>
            <td> $product_detail_qty;</td>
            <td> $product_detail_sub;</td>
            <td>$total;</td>
        </tr>";
    }


    $subject = "Your Order Number" .$order_id;
    $message="$strMessage";
    $header="From:<xxx@gmail.com>";
    $to="$customeremail";
    $send=mail($to,$subject,$message,$header);
    if($send)
    { 
        echo "send successfully";
    }
    else
    {   
        echo "failed";
    }

关于php - 用 PHP 和 MySql 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29159464/

相关文章:

javascript - 关闭 colorbox 并将父窗口重定向到特定的 url

php - php脚本通过ajax执行完成后如何停止setInterval

php - WP Multisite + SSL 在 wp-admin 上给出 404

php - 如何从子域上传表单将上传的文件存储在主域的目录中

java - 我的应用程序未将数据发布到 Android 上的 MySQL 数据库

php - 如何在 ubuntu 上将 MySQL 主机设置为 'localhost' 以外的其他值?

mysql - Web 应用程序的数据库事务保护 - 推荐还是性能消耗?

node.js - 无法从 emailjs 和 smtp 服务器获取电子邮件 - Nodejs

iphone - 使用来自 Apple 示例的 Mailcomposer 的代码时出错

使用 corona sdk 验证电子邮件地址