在结帐完成时使用 PHP 发送电子邮件

红丹

所以我一直在做一个网站,我试图制作一个带有结账系统的功能性购物车。但是,购物车不允许在 Mozilla Firefox 中删除添加的项目,在其他浏览器上它运行良好;我不知道为什么会这样。

此外,我的第二个问题是,在 checkout.php 函数中,每当我在购物车页面中按下结帐按钮时,它不会将订单电子邮件发送到业务电子邮件。有什么办法可以让我完成这项工作吗?下面我已经把所有必要的代码。谢谢你。

购物车.php:

<link rel="stylesheet" href = "styles/styling.css" media="all" />

<?php
   session_start();
   include("adminarea/includes/DBcon.php");
   include ("functions/php1.php");
   include ("header.php");
   require 'obj.php';

    ?>       



<body>

<?php
                    //Fetches information from the database and displays them with the help of obj.php
if(isset($_GET['pID'])){
    $res = mysqli_query($connection, 'select * from product where pID='.$_GET['pID']);
    $prod = mysqli_fetch_object($res);
    $obj = new obj();
    $obj->pID = $prod->pID;
    $obj->pName = $prod->pName;
    $obj->pPrice = $prod->pPrice;
    $obj->qty = 1;

    //to check if products exists in cart or not
    $index = -1;
    $cart = unserialize(serialize($_SESSION['cart']));
        for($i=0;$i<count($cart);$i++)
        if($cart[$i]->pID==$_GET['pID'])
        {
            $index = $i;
            break;
        }

    if($index==-1)
         $_SESSION['cart'][] = $obj;
         else{
             $cart[$index]->qty++;
             $_SESSION['cart']=$cart;
         }
         echo "
         <script> 
         window.open('cart.php','_self')
        </script>
        ";

}


    if(!(isset($_SESSION['cart']))){ 
                            echo "
                            <script>
                            alert('Shopping cart is empty!')
                            </script>
                            ";
                            echo "
                            <script>
                            window.open('products.php','_self')
                            </script>
                            ";
                            }

//if statement to delete the chosen product inside the cart
if(isset($_GET['index']))
{
    $cart = unserialize(serialize($_SESSION['cart']));
    unset ($cart[$_GET['index']]);
    $cart = array_values($cart);
    $_SESSION['cart'] = $cart;
}


?>

<!-- This is to display the shopping cart table-->

<table cellpadding="5" cellspacing="4" border ="9" align="center" width="100%" border="9" bgcolor="darkred">
    <td style="color:#FFF" colspan="10" align="center"><h2><u><i>Shopping Cart:</i></u></h2> 
    <tr>
        <th style="color:#FFF">Option</th>
        <th style="color:#FFF">Id</th>
        <th style="color:#FFF">Name</th>
        <th style="color:#FFF">Price</th>
        <th style="color:#FFF">Quantity</th>
        <th style="color:#FFF">SubTotal</th>
    </tr>
   <?php

   $cart = unserialize(serialize($_SESSION['cart']));
   $s = 0;
   $index = 0;
   for($i=0; $i<count($cart); $i++){
       $s += $cart[$i] ->pPrice * $cart[$i]->qty;

    ?>
    <tr>
    <td>
<div class="shopcart">

        <button style="width:150px; height:50px;"><a href="cart.php?index=<?php echo $index;?>" onClick="return confirm('Please confirm deletion of the chosen product.')">Remove</a></button></td>

        <td style="color:#FFF" align="center"><?php echo $cart[$i] ->pID; ?> </td>
        <td style="color:#FFF" align="center"><?php echo $cart[$i] ->pName; ?></td>
        <td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice; ?></td>
        <td style="color:#FFF" align="center"><?php echo $cart[$i] ->qty; ?></td>
        <td style="color:#FFF" align="center">€<?php echo $cart[$i] ->pPrice * $cart[$i]->qty;?></td>
    </tr>

    <?php }
            $index++;


   ?>
   <tr>
    <td colspan="5" align="right" style="color:#FFF">Total</td>
    <td style="color:#FFF" align="center">€<?php echo $s;?></td>
   </tr>
</table>

<br>
    <a id="a" style="margin-left: 10px;" href="products.php"> Go back</a><br><br>   

    <div id="checkout">

        <form id="checkout" method="post" action="checkout.php"> 
        <input  id="input" type="submit" name="check" value="Checkout" style="background-color:gray; width:200px; margin-right: 10px;">       
    </div>

</div>


<?php include("footer.php") ?>

</body>
</html>

结帐.php:

<?php 
//starting the session
session_start();

    $from = "[email protected]"; //from
    $feedback = "Purchase details";


    $to = "[email protected]";//direction

    $email = "Email from: $from \r\n";


    mail("[email protected]", $feedback, $from);

    header("Location: index.php"); //returns the user back to homepage

    echo "
            <script>
            alert('The checkout has been done successfully! Thank you')     
            </script>
        ";



    ?>
克里斯

在我看来你的mail()电话是不正确的

参数的顺序应该是:地址、主题、电子邮件正文,然后是发件人。发件人应采用以下格式From: [email protected]

因此,将您的代码调整为如下所示:

$from = "[email protected]"; //from
$feedback = "Purchase details";

$to = "[email protected]";//direction

$email = "Email from: $from \r\n";

mail($to, $feedback, $email, "From: $from");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 php 邮件功能发送电子邮件 - 变慢

使用邮件功能从php发送电子邮件

使用Mandrill(php)发送电子邮件时出错

使用AJAX以PHP发送电子邮件

使用PHP中的GMail API发送电子邮件

使用PHP和AJAX发送电子邮件表单

如何使用PHP发送电子邮件?

使用php从html网页发送电子邮件

如何使用php CodeIgniter从本地发送电子邮件

使用php从html表单发送电子邮件

如何使用PHP通过SMTP发送电子邮件

如何使用PHP发送电子邮件?

尝试使用PHP从html表单发送电子邮件

使用php从Hotmail发送电子邮件

如何使用php发送电子邮件

如何使用PHP和WAMP发送电子邮件

使用jquery在php中发送电子邮件

外部托管的电子邮件,并使用PHP发送电子邮件

使用PHP发送电子邮件-接收空白电子邮件

通过 php 邮件发送电子邮件时,内联 css 不打印在电子邮件中

我在 php 中向多个电子邮件帐户发送电子邮件查询时遇到问题

设置“发件人”时,msmtp无法使用php发送电子邮件

Gmail API:尝试发送电子邮件时出现400错误的请求(PHP代码)

php 脚本想法在我的 Windows 挂起时向我发送电子邮件

由请求触发时,在 PHP 中向 Amazon SES 发送电子邮件会冻结

如何使用cURL在php中使用Google gmail API发送电子邮件?

无法使用gmail使用php发送电子邮件-授权错误

如何使用 MailSo 库 (PHP) 通过 SMTP 发送电子邮件

从 profile.php 页面使用 wp_mail 发送电子邮件