I have a online shop that collects data and displays the product and price on the page. The customer then needs complete the form and mail delivery address to me. Everything is working fine, but i would like to include the product that have been ordered in the email that comes to me.
My view_cart.php looks like this
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
if(isset($_SESSION["products"]))
{
$total = 0;
echo '<form method="post" action="paypal-express-checkout/process.php">';
echo '<ul>';
$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();
$ShippinCost = 150.00;
echo '<li class="cart-itm">';
echo '<span class="remove-itm"><a href="cart_update.php?removep='.$cart_itm["code"].'&return_url='.$current_url.'">×</a></span>';
echo '<div class="p-price">'.$currency.$obj->price.'</div>';
echo '<div class="product-info">';
echo '<h3>'.$obj->product_name.' (Code :'.$product_code.')</h3> ';
echo '<div class="p-qty">Qty : '.$cart_itm["qty"].'</div>';
echo '<div>'.$obj->product_desc.'</div>';
echo '</div>';
echo '</li>';
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal + $ShippinCost);
if ($subtotal > 1200) { $total = $subtotal;
}
echo '<input type="hidden" name="item_name['.$cart_items.']" value="'.$obj->product_name.'" />';
echo '<input type="hidden" name="item_code['.$cart_items.']" value="'.$product_code.'" />';
echo '<input type="hidden" name="item_desc['.$cart_items.']" value="'.$obj->product_desc.'" />';
echo '<input type="hidden" name="item_qty['.$cart_items.']" value="'.$cart_itm["qty"].'" />';
$cart_items ++;
echo 'Order Number:';
$today = date("Ymd");
$rand = strtoupper(substr(uniqid(sha1(time())),0,4));
echo $unique = $today . $rand;
}
echo '</ul>';
echo '<span class="check-out-txt">';
echo '<strong>Total : '.$currency.$total.'</strong> ';
echo '</span>';
echo '</form>';
}else{
echo 'Your Cart is empty';
}
?>
Then I have a simple HTML form that collects the info and sends it with ajax. This looks like this
<?php
error_reporting(-1);
ini_set('display_errors', 'on');
ini_set("SMTP", 'smtp.xxxxxxx');
ini_set("smtp_port", "25");
ini_set('sendmail_from','noreply@domain.com');
if($_POST)
{
$to_email = "sean@domain.co.za";"CC: info@domain.co.za "; //Recipient email, Replace with own email here
$from_email = "noreply@YOUR-DOMAIN.com"; //From email address (eg: no-reply@YOUR-DOMAIN.com)
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
$output = json_encode(array( //create JSON data
'type'=>'error',
'text' => 'Sorry Request must be Ajax POST'
));
die($output); //exit script outputting json data
}
//Sanitize input data using PHP filter_var().
$user_name = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING);
$user_email = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL);
$country_code = filter_var($_POST["country_code"], FILTER_SANITIZE_NUMBER_INT);
$phone_number = filter_var($_POST["phone_number"], FILTER_SANITIZE_NUMBER_INT);
$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["msg"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error.
$output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation
$output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if(!filter_var($country_code, FILTER_VALIDATE_INT)){ //check for valid numbers in country code field
$output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in country code'));
die($output);
}
if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field
$output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number'));
die($output);
}
if(strlen($subject)<3){ //check emtpy subject
$output = json_encode(array('type'=>'error', 'text' => 'Subject is required'));
die($output);
}
if(strlen($message)<3){ //check emtpy message
$output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
//email body
$message_body = $message."
".$user_name."
Email : ".$user_email."
Phone Number : (".$country_code.") ". $phone_number ;
### Attachment Preparation ###
$file_attached = false;
if(isset($_FILES['file_attach'])) //check uploaded file
{
//get file details we need
$file_tmp_name = $_FILES['file_attach']['tmp_name'];
$file_name = $_FILES['file_attach']['name'];
$file_size = $_FILES['file_attach']['size'];
$file_type = $_FILES['file_attach']['type'];
$file_error = $_FILES['file_attach']['error'];
//exit script and output error if we encounter any
if($file_error>0)
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
$output = json_encode(array('type'=>'error', 'text' => $mymsg[$file_error]));
die($output);
}
//read from the uploaded file & base64_encode content for the mail
$handle = fopen($file_tmp_name, "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content));
//now we know we have the file for attachment, set $file_attached to true
$file_attached = true;
}
if($file_attached) //continue if we have the file
{
# Mail headers should work with most clients
$headers = "MIME-Version: 1.0
";
$headers = "X-Mailer: PHP/" . phpversion()."
";
$headers .= "From: ".$from_email."
";
$headers .= "Subject: ".$subject."
";
$headers .= "Reply-To: ".$user_email."" . "
";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."
";
$headers .= "--".md5('boundary1')."
";
$headers .= "Content-Type: multipart/alternative; boundary=".md5('boundary2')."
";
$headers .= "--".md5('boundary2')."
";
$headers .= "Content-Type: text/plain; charset=utf-8
";
$headers .= $message_body."
";
$headers .= $obj."
";
$headers .= $product_code."
";
$headers .= $cart_itm."
";
$headers .= $total."
";
$headers .= "--".md5('boundary2')."--
";
$headers .= "--".md5('boundary1')."
";
$headers .= "Content-Type: ".$file_type."; ";
$headers .= "name=\"".$file_name."\"
";
$headers .= "Content-Transfer-Encoding:base64
";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"
";
$headers .= "X-Attachment-Id:".rand(1000,9000)."
";
$headers .= $encoded_content."
";
$headers .= "--".md5('boundary1')."--";
}else{
//proceed with PHP email.
$headers = 'From: '.$user_name.'' . "
" .
'Reply-To: '.$user_email.'' . "
" .
'X-Mailer: PHP/' . phpversion();
}
$send_mail = mail($to_email, $subject, $message_body, $headers);
if(!$send_mail)
{
//If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens)
$output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
}else{
$output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email'));
die($output);
}
}
?>
I have searched the site but cant find my solution. Please can someone point me in the right direction