sending mail()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

sending mail()

Post by aceconcepts »

Hi,

I've created an order confirmation script that not only displays the order someone has made but sends them an email of their order confirmation.

Here is the script:

Code: Select all

<?php
include"top_menu.php";
?>
<html>
<body>
<table width="760" border="0">
<tr><td>
<div align="center">
<table width="600" cellspacing="0">
<tr bgcolor="#0099CC"><td width="5"></td>
	<td height="30">
	<font size="2" color="#FFFFFF" face="Verdana, Arial, Helvetica, sans-serif"><strong>
	Payment
	</strong></font>
	</td>
</tr>
<tr><td colspan="2" height="30"><hr noshade></td></tr>
<tr>
<td width="5"></td>
	<td>
	<font size="2" color="#000000" face="Verdana, Arial, Helvetica, sans-serif">
	1) Your order has been received and is awaiting payment.
	<br><br>
      2) To complete your order, please have your card details ready. 
	  <br><br>
	  3) Your online 
      transaction will be processed by <a href="http://www.worldpay.com" target="_blank"> 
      WorldPay Merchant Services</a>.
	  <br><br>
	4) Please click 'Continue' to make your payment.
	</strong></font>
	</td>
</tr>
<tr>
	<td align="center" colspan="2"><font size="2" color="#000000" face="Verdana, Arial, Helvetica, sans-serif"><br>
        More about WorldPay: <a href="http://www.worldpay.com" target="_blank">Home</a> 
        | <a href="http://www.worldpay.com/about_us/index.php" target="_blank">About</a> 
        | <a href="http://www.worldpay.com/about_us/index.php?page=security" target="_blank">Security</a> 
        | <a href="http://www.worldpay.com/about_us/index.php?page=history" target="_blank">History</a> 
        </font></td>
</tr>
<tr><td colspan="2" height="30"><hr noshade></td></tr>
<?php
include"conn.inc.php";

//SET ALL VARIABLES NEED TO STORE IN ORDER_TABLE--------------------------
$customerid = $_REQUEST['cid'];
$deliveryid = $_REQUEST['del'];
$sessid = $_REQUEST['sess'];
$today = date("d-m-Y");
$order_total = $_REQUEST['totalsum'];
//------------------------------------------------------------------------

//STORE ORDER AND GET ORDER NUMBER FROM ID
$insert_details = "INSERT INTO product_order (
			order_date, order_total, customer_id, del_id)
			VALUES (
    		'$today',
			'$order_total',
			'$customerid',
			'$deliveryid')";
$insert_order = mysql_query($insert_details)
	or (mysql_error());
//SET ORDER NUMBER
$orderid = mysql_insert_id();

//GET ORDER DETAILS FOR EACH ITEM ORDERED (order_qty and product_id)
$query = "SELECT * FROM carttemp WHERE carttemp_sess = '$sessid'";
$results = mysql_query($query)
	or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
extract($row);

//ADD EACH ITEM TO ORDER DETAILS TABLE------------------------------------
$insert4 = "INSERT INTO order_details (
			order_num, order_qty, product_id)
			VALUES (
			'$orderid',
			'$carttemp_quan',
			'$carttemp_prodnum')";
$insert_details4 = mysql_query($insert4)
	or (mysql_error());

//GET CURRENT STOCK QTY FOR EACH ITEM------------------------------------
$get_query = "SELECT * FROM product WHERE product_id = '$carttemp_prodnum'";
$get_results = mysql_query($get_query)
	or die(mysql_error());
while ($row = mysql_fetch_array($get_results)) {
$prodstockqty = $row['prod_stock_qty'];
}
//------------------------------------------------------------------------

//SET NEW STOCK QTY VARIABLE----------------------------------------------
$new_stock_qty = $prodstockqty - $carttemp_quan;

//UPDATE EACH PRODUCT'S STOCK QTY
$update = "UPDATE product
			SET prod_stock_qty = '$new_stock_qty'
			WHERE product_id = '$carttemp_prodnum'";
$update_results = mysql_query($update)
	or die(mysql_error());
//------------------------------------------------------------------------
	$total_sum = $_SESSION['total_sum'];
				
}
//END OF LOOP-------------------------------------------------------------
	
//EMPTY SHOPPING CART-----------------------------------------------------
$query_del = "DELETE FROM carttemp WHERE carttemp_sess = '$sessid'";
$delete = mysql_query($query_del);
//------------------------------------------------------------------------

//GET CUSTOMER DETAILS
$query = "SELECT * FROM customer WHERE customer_id = '$customerid'";
$results = mysql_query($query)
	or die(mysql_error());
while ($row = mysql_fetch_array($results)) {
extract($row);
$first_name = $row['customer_first_name'];
$last_name = $row['customer_last_name'];
$door_no = $row['customer_door_no'];
$street = $row['customer_street'];
$town = $row['customer_town'];
$county = $row['customer_county'];
$postcode = $row['customer_postcode'];
$country = $row['customer_country'];
$telephone = $row['customer_tel'];
$email_add = $row['customer_email'];
}

?>
<form name="form1" method="POST" action="order_complete.php">
<input type="hidden" name="total_sum" value"<?php echo $total_sum; ?>">
<input type="hidden" name="first_name" value"<?php echo $first_name; ?>">
<input type="hidden" name="last_name" value"<?php echo $last_name; ?>">
<input type="hidden" name="door_no" value"<?php echo $door_no; ?>">
<input type="hidden" name="street" value"<?php echo $street; ?>">
<input type="hidden" name="town" value"<?php echo $town; ?>">
<input type="hidden" name="county" value"<?php echo $county; ?>">
<input type="hidden" name="postcode" value"<?php echo $postcode; ?>">
<input type="hidden" name="country" value"<?php echo $country; ?>">
<input type="hidden" name="telephone" value"<?php echo $telephone; ?>">
<input type="hidden" name="email_add" value"<?php echo $email_add; ?>">
<tr>
	<td colspan="2" align="right">
	<input type="submit" name="submit" value="Continue...">
	</td>
</tr>
<tr><td height="25"></td></tr>
</form>
</table>
</body>
</html>
<?php

//SEND EMAIL TO CUSTOMER AND SELF
//recipients
$to = "<" . $email_add . ">";
//subject
$subject = "Order confirmation";

//message
	//top of message
	$message = "
		<html>
		<head>
		<title>Order Confirmation</title>
		</head>
		<body>
		<table align=\"center\" width=\"600\" border=\"0\">
		<tr bgcolor=\"#0099CC\">
		<td height=\"20\" align=\"center\"><font size=\"2\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\"><strong>
		Order Summary<br>
		</strong></font></td></tr>
		<tr><td><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">
		Order date: ";
	$message .= $today;
	$message .= "
		<br>
		Order number: ";
	$message .= $orderid;
	$message .= " </font></td></tr></table>
		<table align=\"center\" width=\"600\" border=\"0\">
		 <tr>
		  <td colspan=\"6\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">
		   <p>Bill to:<br>";
	$message .= $first_name;
	$message .= " ";
	$message .= $last_name;
	$message .= "<br>";
	$message .= $door_no . " " . $street;
	$message .= "<br>";
	$message .= $town;
	$message .= "<br>";
	$message .= $county . ", " . $postcode;
	$message .= "<br>";
	$message .= $country;
	$message .= "</p>
		</font></td>
		 </tr><tr><td colspan=\"6\"><hr width=\"600px\"></td></tr>";
		 
	//USE DELIVERY ID TO GET CHOSEN DELIVERY DETAILS
	$del_query = "SELECT * FROM delivery WHERE del_id = '$deliveryid'";
	$del_results = mysql_query($del_query)
		or die(mysql_error());
	while ($row = mysql_fetch_array($del_results)) {
	$destination = $row['del_destination'];
	$duration = $row['del_duration'];
	$price = $row['del_price'];
	$region = $row['del_region'];
	}	
	$message .= "<tr><td width=\"70\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">Delivery:</font></td><td width=\"300\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	$message .= $destination;
	$message .= "</font></td>";
	$message .= "<td width=\"100\" align=\"center\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	$message .= $duration;
	$message .= "</font></td>";
	$message .= "<td align=\"right\" width=\"70\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	$message .= $region;
	$message .= "</font></td>";
	$message .= "<td align=\"right\" width=\"50\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	$message .= $price;
	$message .= "</font></td><td width=\"5\"></td</tr>
				<tr><td colspan=\"6\"><hr width=\"600px\"></td></tr>
				</table>

		  
	  <table align=\"center\" width=\"600\" cellspacing=\"0\" cellpadding=\"5\">
	<tr bgcolor=\"#0099CC\">
	<td width=\"5\"></td>
	<td width=\"290\"><font size=\"2\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\">Item</front></td>
	<td align=\"center\" width=\"100\"><font size=\"2\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\">Qty</font></td>
	<td align=\"right\" width=\"100\"><font size=\"2\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\">Price</font></td>
	<td align=\"right\" width=\"100\"><font size=\"2\" color=\"#FFFFFF\" face=\"Verdana, Arial, Helvetica, sans-serif\">Total</font></td>
	<td width=\"5\"></td>
	</tr>
	</table>
	<table align=\"center\" width=\"600\" cellspacing=\"0\" cellpadding=\"5\">";
	
//--INSERT THE ORDER CONTENT INTO MESSAGE FIELD---------------------------
	
	//GET THE ORDER NUMBER AND INITIATE THE LOOP TO GET ORDER CONTENT
	$query = "SELECT * FROM order_details WHERE order_num = '$orderid'";
	$results = mysql_query($query)
		or die(mysql_error());
	while ($row = mysql_fetch_array($results)) {
	extract($row);
	$prodid = $row['product_id'];
	$orderqty = $row['order_qty'];
	
	//USE PRODUCT ID TO GET PRODUCT DETAILS
	$prod_query = "SELECT * FROM product WHERE product_id = '$prodid'";
	$prod_results = mysql_query($prod_query)
		or die(mysql_error());
	while ($row = mysql_fetch_array($prod_results)) {
	$product_name = $row['prod_name'];
	$product_code = $row['prod_code'];
	$product_price = $row['prod_unit_price'];
	}
	//INSERT ORDER CONTENT INTO MESSAGE

	$message .= "<tr><td width=\"5\"></td><td width=\"290\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	$message .= $product_name;
	$message .= "</font></td>";
	$message .= "<td width=\"100\" align=\"center\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	$message .= $orderqty;
	$message .= "</font></td>";
	$message .= "<td align=\"right\" width=\"100\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	$message .= $product_price;
	$message .= "</font></td>";
	$message .= "<td align=\"right\" width=\"100\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\">";
	//GET UNIT PRICE * QTY
	$extprice = number_format($orderqty * $product_price, 2);
	$message .= $extprice;
	$message .= "</font></td><td width=\"5\"></td>";
	$message .= "</tr>";
	$totalexdel = $totalexdel + $extprice;
	}
			$subtotal = number_format($totalexdel, 2);
//------------------------------------------------------------------------
	$message .= "
				</table><table width=\"600\" cellspacing=\"0\" cellpadding=\"5\">
				<tr><td align=\"center\" width=\"600\" colspan=\"6\"><hr width=\"600px\"></td></tr>
				<tr>
				<td width=\"495\" align=\"right\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\"><strong>
				Sub total:
				</strong></font></td>
				<td width=\"100\" align=\"right\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\"><strong>";
	$message .= "£" . $subtotal;
	$message .= "</strong></font></td>";
	$message .= "<td width=\"5\"></td>
				</tr>";
	$message .= "<tr>			
				<td width=\"495\" align=\"right\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\"><strong>
				Total (inc del):
				</strong></font></td>
				<td width=\"100\" align=\"right\"><font size=\"2\" color=\"#000000\" face=\"Verdana, Arial, Helvetica, sans-serif\"><strong>";
	$message .= "£" . $order_total;
	$message .= "</strong></font></td>";
	$message .= "<td width=\"5\"></td>
				</tr>
				</table>
				</div>
				</td>
				</tr>
				</table>
				</body>
				</html>";
	
	/*headers*/
	$headers = "MIME-Version: 1.0\r\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
	$headers .= "From: <nick@aceconcepts.co.uk>\r\n";
	$headers .= "Cc: <nick@aceconcepts.co.uk>\r\n";
	$headers .= "X-Mailer: PHP / ".phpversion()."\r\n";
	
	/*mail confirmation*/
	mail($to, $subject, $message, $headers);
	
	//DISPLAY THE ORDER AND GIVE THEM AN ORDER NUMBER
	echo $message;
	?>
The problem is that I do not receive an email.

Can anyone notice anything I have done wrong?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I would start by checking the value of $to, then I would check if sendmail is installed and working properly
xterra
Forum Commoner
Posts: 69
Joined: Mon Mar 06, 2006 12:52 pm

Post by xterra »

Sorry to jump in but do you notice that sometimes the mail function does not send the entire email. The email message is always the same but some of my users are saying they are either not getting the message, or only parts of it. The function itself is simple so I don't know exactly what I can fix.

Thanks in advance,
Rob.
souvit
Forum Newbie
Posts: 2
Joined: Fri May 20, 2005 11:23 pm
Location: India
Contact:

SMTP email

Post by souvit »

You could try smtp email and for that you can download the email class which is tested and secured.

http://www.phpclasses.org/browse/package/346.html
Post Reply