hoping someone can assist or send me in the direction of a tutorial?
I have simple Shopping Cart (PHP/MYSQLi) that processes the orders and stores them in the database. However I can't figure out how the best way is to also have an email alert sent with the order confirmation details?
below is the code for checkout.php - this is when it submits to the database.
I also have ordersuccess.php - this is just page confirming the OrderID.
should I implement a script to ordersuccess.php instead?
thanks in advance folks, all a learning curve for me!
checkout.php
<?php
// include database configuration file
include 'dbConfig.php';
// initializ shopping cart class
include 'Cart.php';
$cart = new Cart;
// redirect to home if cart is empty
if($cart->total_items() <= 0){
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Checkout</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.container{width: 100%;padding: 50px;}
.table{width: 65%;float: left;}
.shipAddr{width: 30%;float: left;margin-left: 30px;}
.footBtn{width: 95%;float: left;}
.orderBtn {float: right;}
</style>
</head>
<body>
<div class="container">
<h1>Order Preview</h1>
<table class="table">
<thead>
<tr>
<th>Scent</th>
<th>Type</th>
<th>Price</th>
<th>Qty</th>
<th>Subtotal</th>
</tr>
</thead>
<tbody>
<?php
if($cart->total_items() > 0){
//get cart items from session
$cartItems = $cart->contents();
foreach($cartItems as $item){
?>
<tr>
<td><?php echo $item["name"]; ?></td>
<td><?php echo $item["category"]; ?></td>
<td><?php echo '£'.$item["price"].' GBP'; ?></td>
<td><?php echo $item["qty"]; ?></td>
<td><?php echo '£'.$item["subtotal"].' GBP'; ?></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="4"><p>No items in your cart......</p></td>
<?php } ?>
</tbody>
<tfoot>
<tr>
<td colspan="4"></td>
<?php if($cart->total_items() > 0){ ?>
<td class="text-left"><strong>Total <?php echo '£'.$cart->total().' GBP'; ?></strong></td>
<?php } ?>
</tr>
</tfoot>
</table>
<div class="footBtn">
<a href="index.php" class="btn btn-warning"><i class="glyphicon glyphicon-menu-left"></i> Continue Shopping</a>
<a href="cartAction.php?action=placeOrder" class="btn btn-success orderBtn">Place Order <i class="glyphicon glyphicon-menu-right"></i></a>
</div>
</div>
</body>
</html>
ordersucess.php
<?php
if(!isset($_REQUEST['id'])){
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Order Success</title>
<meta charset="utf-8">
<style>
.container{width: 100%;padding: 50px;}
p{color: #34a853;font-size: 18px;}
</style>
</head>
</head>
<body>
<div class="container">
<h1>Order Status</h1>
<p>Your order has submitted successfully. Order ID is #<?php echo $_GET['id']; ?></p>
</div>
</body>
</html>
Sending e-mail can be slow and blocking. You should move sending emails to background job. The easiest way is to create a command/message queue and write a worker/consumer of the queue. I did that some time ago by using swiftmailer with Symfony2
You may, however, want to avoid the performance hit of the communication between Swift Mailer and the email transport, which could cause the user to wait for the next page to load while the email is sending.