在PHP和MySql中发送电子邮件

时间:2015-03-20 04:31:47

标签: 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";
    }
}

}

2 个答案:

答案 0 :(得分:1)

更改以下行

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";
    }

答案 1 :(得分:0)

  

你应该将发送邮件功能保留在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";
   }
    }
  }
}