douqian1835 2012-11-19 22:34
浏览 96
已采纳

将邮件发送给多个收件人

i have already research on using the mail() to send to multiple recipient's but i just cant get it to work. What im trying to do is, for every order that i have, order 1,2,3, each having their own email addresses, when i change their order status from pending to confirm, the mail() will use that id to refer to the db table and send the email of those 3 orders. But for my case, it mailed just the latest order which is order 3. This is the form that i use to change the order status.

<form action="results-action" method="post" enctype="multipart/form-data">
<fieldset>


<table id ="table_id" class="display">

<thead>

<tr><td><h2>Pending Order</h2></td></tr>

<tr>
<th scope="col">Order ID</th>
<th scope="col"> </th>
<th scope="col">Name</th>
<th scope="col">Address</th>
<th scope="col">Product Name</th>
<th scope="col">Produt Quantity</th>
<th scope="col">Price</th>
<th scope="col">Order status</th>
</tr>
</thead>

<tbody>

<?php 
while ($row = mysqli_fetch_array($result)) {
?>

<tr>
<td><input type="text" value='<?=$row['virtuemart_order_id']?>' name="orderid" id="virtuemart_order_id"></td>
<td><input type="hidden" value='<?=$row['virtuemart_product_id']?>' name="productid" id="virtuemart_product_id"></td>
<td><?=$row['first_name']?></td>
<td><?=$row['address_1']?></td>
<td><?=$row['order_item_name']?></td>

<td><?=$row['product_quantity']?></td>

<td><?=$row['product_final_price'] ?></td>
<td><select name='change[<?=$row['virtuemart_order_id']?>]'>
<option value='C'> Confirmed</option>

<option value='X'> Cancelled</option></select></td>
</tr>

<?php
}
?>

</tbody>
</table>
</fieldset>



<fieldset>
<table>
<tr>
<td><input type="submit" value="Update status" name="update status"> </td>
</tr>
</table>
</fieldset>



</form>

This is the php, using the order id from the form to select the email addresses.

<?php
$orderid = $_POST['orderid'];


// build SQL statement to select email addresses
$query3 = "SELECT email from ruj3d_virtuemart_order_userinfos where virtuemart_order_id = '$orderid'";


// execute SQL statement
$result3 = mysqli_query($link, $query3) or die(mysqli_error($link)); 


$subject = "Order confirmed by Home and decor";
$message = "Hello! This is a message to inform that your order has been confirmed";
$from = "107496@myrp.edu.sg";
$headers = "From: $from";

while($row3 = mysqli_fetch_array($result3)){ 

$addresses[] = $row3['email'];

}

$to = implode(",", $addresses);



mail($to, $subject, $message, $headers);

?>
  • 写回答

4条回答 默认 最新

  • duanchuli5647 2012-11-19 22:54
    关注

    You are using the wrong $_POST field to select your email addresses.

    The form generated by the first php file contains a table, with each row containing an input field named 'orderid'. But the field you are really interested in is the select input value of each row. These are all stored in one single array named change in the $_POST array.

    Here's an example of how $_POST could be initialized to correspond to the select values being returned by the form:

    $_POST['change'] = array('id1' => 'C', 'id2' => 'X' /*, 'id3' => ... */ );
    

    So you need to:

    • get the array returned by the form in $_POST,
    • filter out the entries which aren't equal to 'C',
    • keep the keys of these entries,
    • escape them to sanitize them,

    You should replace the beginning of your php file with the following, which does as explained above.

    This first version doesn't sanitize the entries:

    <?php
    
    function confirmed($v) { return ($v == 'C'); }
    
    // pick the rows in your form table which have been set as confirmed, and use the keys 
    $orderid = implode(',', array_keys(array_filter($_POST['change'],'confirmed')));
    
    // build SQL statement to select email addresses
    $query3 = "SELECT email FROM ruj3d_virtuemart_order_userinfos WHERE virtuemart_order_id IN (".$orderid.")";
    
    // etc.
    

    This one does sanitization:

    <?php
    
    function confirmed($v) { return ($v == 'C'); }
    
    // pick the rows in your form table which have been set as confirmed, and use the keys 
    $orderid = implode(',', array_map('mysql_real_escape_string', 
                                      array_keys(array_filter($_POST['change'],'confirmed'))));
    
    
    // build SQL statement to select email addresses
    $query3 = "SELECT email FROM ruj3d_virtuemart_order_userinfos WHERE virtuemart_order_id IN (".$orderid.")";
    
    // etc.
    

    See this SO question for the use of BCC with the mail() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作