Retrieve data from cart and send using mail

你说的曾经没有我的故事 提交于 2019-12-02 09:25:07

use jquery and capture the html table that you want to send as email. and pass the variable to the email function using ajax, try this,

    <script>
                    $(document).ready(function() {
                        var htmlstring = $("#mail_table").html();
                        $( "#send" ).click(function() {

                            var cn_name = $("#cn_name").val();
                            var cn_mail = $("#cn_mail").val();

                            $.ajax({
                                method : 'post',
                                dataType : 'html',
                                url :'<?php echo base_url(); ?>index.php/cart/testmail',
                                data :{
                                    name:cn_name,
                                    mail:cn_mail
                                    table:htmlstring
                                }
                            });
                        });
                    });
                </script>

jquery veriable = htmlstring

also you can use jquery serialize method to get all form inputs.

https://api.jquery.com/serialize/

CI ocontroller

public function testmail(){

            $to = 'mail@gmail.com';
            $subject = 'Enquiry from ';
            $message = $_POST['table'];
            $headers = "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

            if(mail($to, $subject, $message, $headers))
            {

            }
        }

For view cart and receive it as mail use this...

In mail use this code...

$tpl_file = 'cartmail.php';
if (!file_exists($tpl_file)) {
die('Mail could not be sent');
exit;
}
$msg_tmpl = file_get_contents($tpl_file);
$to = 'mail@gmail.com';
$subject = 'Enquiry from ' .$_REQUEST['guest_name'];
$message = $msg_tmpl;
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$_REQUEST['email'];
mail($to, $subject, $message, $headers);

And in the cartmail.php file use this code

<div class="table-responsive">
<table class="shop_table cart" cellspacing="0">
    <thead>
        <tr>
            <th class="product-remove">&nbsp;</th>
            <th class="product-thumbnail">&nbsp;</th>
            <th class="product-name">Product</th>
            <th class="product-price">Price</th>
            <th class="product-quantity">Quantity</th>
            <th class="product-subtotal">Total</th>
        </tr>
    </thead>
    <tbody>
    <?php
    $cart_items = 0;
        foreach ($_SESSION["products"] as $cart_itm)
        {
           $product_code = $cart_itm["code"];
           $results = $mysqli->query("SELECT product_name,product_desc, price FROM products WHERE product_code='$product_code' LIMIT 1");
           $obj = $results->fetch_object();
    ?>

                        <tr class="cart_item">
                    <td class="product-name">
                    <span class="name"><?php echo $obj->product_name; ?> </span>                    </td>

                    <td class="product-price">
                        <span class="amount"><?php echo $currency,$obj->price;?></span>                 </td>

                    <td class="product-quantity">
                        <div class="quantity"><?php echo $cart_itm["qty"];?></div>
                    </td>

                    <td class="product-subtotal">
                        <span class="amount"><?php $ptotal=$obj->price*$cart_itm["qty"]; echo $ptotal; ?></span>                    </td>
                        <td class="product-remove">
                    <?php echo '<a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'" class="remove" title="Remove this item">&times;</a>';?> </td>
                </tr>
<?php
    $subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
            $total = ($total + $subtotal);

            $cart_items ++;

        }
        ?>
            </tbody>
</table>

</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!