It is common for companys to send email confirmations of orders after they are taken on line. ASP can generate automatic email messages through scripting, and we'll add this capability to the eCommerce application. The following illustration shows the format of the confirmation message that will be received by the customer.
The mail() function
PHP includes the mail() for the purpose of generating email messages under script control.
The mail() includes parameters to construct and send email, much as you would under your personal email program. Among the properties and parameters useful to our purpose are the following:
| Property | Description |
|---|---|
| to | A string that represents the email address of the recipient of the message. |
| subject | A string that represents the subject line of the message. |
| message | A string that represents the main body of the message. |
| headers | A string that represents the additional information included at the beginning of e-mail messages that formulate their structure and essentially make them valid mail addresses. |
In its simplest form, an email script can be like the following:
<?php
$to = 'youraddress@domain.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
Of course, we'll need to work a little harder to compose the order confirmation message, especially if it is formatted for presentation as an order form.
Script Location
The best location for the order confirmation email script is on the ordercapture.php page. This is the page that receives information back from the credit card company and which creates the OrderHeader and OrderDetail entries for the customer. The page contains all the information we need to compose an email message. The location for the script is shown in the following page outline:
<?php
$Approval = $_POST[Approval];
if ($Approval)
{
$sqlInsert = "INSERT INTO OrderHeader (OrderNo,OrderDate,CustomerName,CustomerAddress,
CustomerCity,CustomerState,CustomerZip,CustomerPhone,CustomerEmail) Values ('$OrderNo','$Date','$Name','$Address','$City','$State','$Zip','$Phone','$Email')";
$rsInsert = odbc_exec($conn,$sqlInsert);
//Create OrderDetail Record
$sqlDetail ="SELECT * FROM OrderDetail WHERE NULL";
$rsDetail = odbc_exec($conn,$sqlDetail);
$sqlShopCart ="SELECT * FROM ShopCart WHERE OrderNo ='$OrderNo'";
$rsShopCart = odbc_exec($conn,$sqlShopCart);
while ($row = odbc_fetch_array($rsShopCart))
{
$sqlProd ="SELECT ItemTitle,ItemPrice FROM Products WHERE ItemNumber ='$row[OrderItem]'";
$rsProd = odbc_exec($conn,$sqlProd);
$ProdTitle = odbc_result($rsProd,ItemTitle);
$ProdPrice = odbc_result($rsProd,ItemPrice);
$sqlInsertDetail = "INSERT INTO OrderDetail (OrderNo,ItemNumber,ItemQuantity,
ItemTitle,ItemPrice) Values ('$row[OrderNo]','$row[OrderItem]',
'$row[OrderQuantity]','$ProdTitle','$ProdPrice')";
$rsInsertDetail = odbc_exec($conn,$sqlInsertDetail);
}
//INSERT EMAIL SCRIPT HERE // INSERT EMAIL SCRIPT HERE
----------------------------------------------------------
$to = 'youraddress@domain.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
----------------------------------------------------------
//Delete the shopcart
$sqlDelete = "DELETE FROM ShopCart WHERE OrderNo='$OrderNo'";
$rsDelete = odbc_exec($conn,$sqlDelete);
odbc_close($conn);
//echo $sqlInsertDetail;
header("Location:salesorder.php?Approval=$Approval");
}
?>
Scripting an Email Message
Let's begin, then, coding the script that is inserted onto the page. We need to be aware that the email field is optional on the credit card checking form. Some customers might not have submitted an email address and we can only send a confirmation message to those who do. So the entire script appears inside this condition test.
The first thing to do is to initialize the mail() parameters - $to, and $subject. The assignment to the $toparameter is one of the pieces of information sent from the credit card company and captured at the beginning of the page. The variable contains the email address of the customer. This must be a real, valid email address.
The $subject assignment uses a text string that includes the value of the $OrderNo variable for identification.
Now we need to compose the body of the message, formatting it in HTML to control its layout and appearance. Recall from above that the body must be a single text string. So, we cannot write a series of separate HTML lines and assign them to the $message parameter. We need to compose them as a single string. We'll do this by establishing a $message variable and then piece together within it one long string from separate substrings.This technique can be seen in the next portion of code.
Each substring of text is concatenated to the $message variable, creating from line to line a single text string within the variable. The strings also contain XHTML tags to format the enclosed text. Note that the $Date, $OrderNo, $Name, $Address, $City, $State, and $Zip variables captured from the credit company's returns are embedded within the growing string. The final portion of code creates the table headers for the sales order information that is formatted next.
Next, we need to gather the sales order information from the OrderDetail Table and format it as rows within the table.
$sqlMail = "SELECT * From OrderDetail WHERE OrderNo = '$OrderNo'";
$rsMail = odbc_exec($conn,$sqlMail);
while ($row = odbc_fetch_array($rsMail))
{
$ItemNumber =$row[ItemNumber];
$ItemTitle =$row[ItemTitle];
$ItemQuantity = $row[ItemQuantity];
$ItemPrice = $row[ItemPrice];
$ItemAmount = $ItemQuantity * $ItemPrice;
$OrderTotal = $OrderTotal + $ItemAmount;
$message = $message . "<tr>";
$message = $message ." <td>" . $ItemNumber . "</td>";
$message = $message . " <td>" . $ItemTitle . "</td>";
$message = $message . " <td align=right>". $ItemQuantity . "</td>";
$message = $message . " <td align=right>" . number_format($ItemPrice,2) . "</td>";
$message = $message . " <td align=right>" . number_format($ItemAmount,2) . "</td>";
$message = $message . "</td>"
}
We do that here within a while loop, extracting the field values into variables that are formatted within table cells and concatenated to the $message string. As the loop iterates, the $OrderTotal is accumulated for formatting next.
$ShippingCharge = $OrderTotal * .02;
$OrderTotal = $OrderTotal + $ShippingCharge;
$message = $message . "<tr>";
$message = $message . "<td colspan=4 align=right>Shipping</td>";
$message = $message . " <td align=right>" . number_format($ShippingCharge,2) . "</td>";
$message = $message . "</tr>";
$message = $message . "<tr>";
$message = $message . "<td colspan=4 align=right>Order Total</td>";
$message = $message . "<td align=right>" . number_format($OrderTotal,2) . "</td>";
$message = $message . "</tr>";
$message = $message . "</table>";
$message = $message . "</body>";
$message = $message . "</html>";
$headers = 'From: sales@webwarehouse.com' . "\r\n" .
'Reply-To: sales@webwarehouse.com' . "\r\n" .
'Content-Type:text/html;charset=us-ascii' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
Now that the script has iterated through the recordset, formatting individual table rows for the products purchased, The $ShippingCharge is calculated and formatted, along with the $OrderTotal, as the final two rows of the table. The closing HTML tags are concatenated to the end of the $message string, and the email message is complete.
After completing the email message, we create the $headers variable to contain any header data that we want to describe the message. In this case, we are adding a From (indicates the e-mail address from which the e-mail originates), Reply-To (the e-mail address that appears in the recipients TO field if they reply to the automated message), Content-Type (specifies the format of the message), and X-Mailer (specifies the version of PHP used to generate the message) header values. While the header values are optional, it is recommended that these values always be included.
If an email message contains HTML to be interpreted as such, then the Content-Type header setting is necessary.
The last step is to call the built-in mail() function.
It should be mentioned in passing that before email can be sent through a PHP script, the web server must have SMTP (Simple Mail Transfer Protocal) Services running and edits to the PHP.ini file may be necessary. Refer to the PHP site for specfics on various server settings.