I want to send the data from the order to the email + contact form send.
I do not know how to send cart data to an email with a contact form
function load_cart_data()
{
$.ajax({
url:"fetch_cart.php",
method:"POST",
dataType:"json",
success:function(data)
{
$('#cart_details').html(data.cart_details);
$('.total_price').text(data.total_price);
$('.badge').text(data.total_item);
}
});
}
Cart ....
<?php
//fetch_cart.php
session_start();
$total_price = 0;
$total_item = 0;
$output = '
<div class="table-responsive" id="order_table">
<table class="table table-bordered table-striped">
<tr>
<th width="40%">Product Name</th>
<th width="10%">Quantity</th>
<th width="20%">Price</th>
<th width="15%">Total</th>
<th width="5%">Action</th>
</tr>
';
if(!empty($_SESSION["shopping_cart"]))
{
foreach($_SESSION["shopping_cart"] as $keys => $values)
{
$output .= '
<tr>
<td>'.$values["product_name"].'</td>
<td>'.$values["product_quantity"].'</td>
<td align="right">$ '.$values["product_price"].'</td>
<td align="right">$ '.number_format($values["product_quantity"] * $values["product_price"], 2).'</td>
<td><button name="delete" class="btn btn-danger btn-xs delete" id="'. $values["product_id"].'">Remove</button></td>
</tr>
';
$total_price = $total_price + ($values["product_quantity"] * $values["product_price"]);
$total_item = $total_item + 1;
}
$output .= '
<tr>
<td colspan="3" align="right">Total</td>
<td align="right">$ '.number_format($total_price, 2).'</td>
<td></td>
</tr>
';
}
else
{
$output .= '
<tr>
<td colspan="5" align="center">
Your Cart is Empty!
</td>
</tr>
';
}
$output .= '</table></div>';
$data = array(
'cart_details' => $output,
'total_price' => '$' . number_format($total_price, 2),
'total_item' => $total_item
);
echo json_encode($data);
?>
email send
<?php
$namebusiness = $_POST['namebusiness'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$adress = $_POST['adress'];
$city = $_POST['city'];
$psc = $_POST['psc'];
$state = $_POST['state'];
$phone = $_POST['phone'];
$visitor_email = $_POST['email'];
$data = $_POST['data'];
$message = $_POST['cart_details'];
$email_subject = "Order";
$email_body = "Name Of Business: $namebusiness.\n "."First name: $firstname.\n "."Last name: $lastname.\n"."E-mail: $visitor_email.\n"."Adress: $adress.\n "."City: $city.\n "."Post Code / ZIP: $psc.\n "."State: $state.\n "."Phone number: $phone.\n";
$to = "patrikl123#seznam.cz";
$to = $_POST['email'];
$headers = 'From: domaci#potrebyhanka.cz' . "\r\n" .
'Reply-To: domaci#potrebyhanka.cz' . "\r\n" .
'Content-type: text/html; charset=UTF-8' . "\r\n".
'X-Mailer: PHP/' . phpversion();
mail($to, $email_subject,$message,$email_body,$headers);
header("Content-type: text/html; charset=UTF-8");
header("Location: index.php");
//https://stackoverflow.com/questions/30802674/retrieve-data-from-cart-and-send-using-mail
?>
Related
I want to send email with multiple rows from database in php but when I send email that time send multiple email with single row data but I want single email with multiple rows.
I don't know where I am wrong. Below is my running code in php and mysql.
$fm_id = $_POST['fm_id'];
$issue = $_POST['issue'];
$resolution = $_POST['resolution'];
$fstatus = $_POST['fstatus'];
$date3= date("Y-m-d h:i:s");
$time2= date("h:i:s");
for ($i = 0; $i < count($fm_id); $i++)
{
$update=("UPDATE fm_status SET problem='$issue[$i]', solution='$resolution[$i]',status='$fstatus[$i]' WHERE fm_id='$fm_id[$i]'");
$res=mysql_query($update);
$update1=("UPDATE fm_status SET date2='$date3', time2='$time2' WHERE fm_id='$fm_id[$i]'");
$res1=mysql_query($update1);
$to ='abc#gmail.com'. ', ';
$to .='abc#abc.com';
$subject="Ticket Details from ";
$header="Solution ";
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= 'From: FM Engineer<solution#abc.com>' . "\r\n" ;
$message = '<html><body>';
$message .= '<head>';
$message .= '</head>';
$message .= " <align=center> <div id='apDiv2'>
<table id='table1' border='1' cellpadding='5' cellspacing='5' class='tinytable cf'>
<thead>
<tr>
<th><h3> Raised By </h3></th>
<th><h3> Issue </h3></th>
<th><h3> Resolution </h3></th>
<th><h3> Status </h3></th>
</tr> </thead>
<tbody>
<td> ".$issue[$i]."</td>
<td> ".$issue[$i]."</td>
<td> ".$resolution[$i]."</td>
<td> ".$issue[$i]."</td>
</tr><tbody></table>";
$message .= '<br>';
$message .= "</body></html>";
$sentmail = mail($to,$subject,$message,$header);
echo $sentmail;
}
I think this is more along the lines of what you are looking for (I am not going to reiterate the warnings regarding mysql_* functions). If you are trying to add rows to your table to send in one email, then just have the rows built by the for loop like so:
$fm_id = $_POST['fm_id'];
$issue = $_POST['issue'];
$resolution = $_POST['resolution'];
$fstatus = $_POST['fstatus'];
$date3= date("Y-m-d h:i:s");
$time2= date("h:i:s");
$to ='abc#gmail.com'. ', ';
$to .='abc#abc.com';
$subject="Ticket Details from ";
$header="Solution ";
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= 'From: FM Engineer<solution#abc.com>' . "\r\n" ;
$message = '<html>';
$message .= '<head>';
$message .= '</head><body>';
$message .= " <align=center> <div id='apDiv2'>
<table id='table1' border='1' cellpadding='5' cellspacing='5' class='tinytable cf'>
<thead>
<tr>
<th><h3> Raised By </h3></th>
<th><h3> Issue </h3></th>
<th><h3> Resolution </h3></th>
<th><h3> Status </h3></th>
</tr> </thead>
<tbody>";
for ($i = 0; $i < count($fm_id); $i++)
{
$update=("UPDATE fm_status SET problem='$issue[$i]', solution='$resolution[$i]',status='$fstatus[$i]' WHERE fm_id='$fm_id[$i]'");
$res=mysql_query($update);
$update1=("UPDATE fm_status SET date2='$date3', time2='$time2' WHERE fm_id='$fm_id[$i]'");
$res1=mysql_query($update1);
$message .= "<tr>
<td> ".$issue[$i]."</td>
<td> ".$issue[$i]."</td>
<td> ".$resolution[$i]."</td>
<td> ".$issue[$i]."</td>
</tr>";
}
$message .= '<tbody></table><br>';
$message .= "</body></html>";
$sentmail = mail($to,$subject,$message,$header);
echo $sentmail;
I am confused by the use of single and double quotation marks, echo, and $message .=
I cannot figure out how to code a foreach loop inside my $message to output the cart items. The thead part works, but the tbody area is flawed. Any help is appreciated.
<?php
session_start();
// Initialize variables
$name = $telephone = $email = $jim_gmail = '';
// Post data from #quote-form.html.php
if( $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'] ) ) {
// Sanitize and post data to variables
$company = sanitize( $_POST['company'] );
$name = sanitize( $_POST['name'] );
$telephone = sanitize( $_POST['telephone'] );
$email = sanitize( $_POST['email'] );
// Assign $name value to SESSION variable for use #thankyou.html.php
$_SESSION['name'] = $name;
// Initialize and declare variables for script validation
$errMsg = '';
$telephone_pattern = '/^((([0-9]{1})*[- .(]*([0-9]{3})[- .)]*[0-9]{3}[- .]*[0-9]{4})+)*$/';
$alpha_only_pattern = '/^[a-zA-Z]*$/';
$email_pattern = '/^([a-zA-Z0-9._%-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})*$/';
$integers_pattern = '/^[0-9]*$/';
// Validate user data before submitting to server
if(empty($name) || empty($telephone) || empty($email))
{
$errMsg = "*Name, telephone and email address required.<br>";
include 'error.html.php';
}
elseif (preg_match($email_pattern,$email) === 0 )
{
$errMsg = "*Please enter a valid email address.<br>";
include 'error.html.php';
}
else
{
/* Prepare message for e-mail */
/* set e-mail recipient */
$jim_gmail = 'jim#gmail.com';
// Three required arguments ($to, $subject, $message)
$to = "$jim_gmail";
$subject = "Buyer for CraneHeli";
$from = "$email";
$message = // contents of report in $message
"
<html>
<head></head>
<body>
<h3>Parts Buyer</h3>
<p>Company: $company</p>
<p>Name: $name</p>
<p>Telephone: $telephone</p>
<p>Email: $email</p>
<h3>Please quote the following:</h3>
<table name='contact_seller' style='border-collapse:collapse';>
<thead>
<tr>
<th>ID</th>
<th>Part Number</th>
<th>Description</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php foreach($cart as $item): ?>
<tr>
<td>$item['id']</td>
<td>$item['part_number']</td>
<td>$item['description']</td>
<td>$item['quantity']</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p>End of buyer data report</p>
<hr />
</body>
</html>
"; //end of $message
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n"; // code to send HTML on UNIX
$headers .= 'Content-type:text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Bcc: ' . $jim_gmail . "\r\n"; // works
$headers .= 'Bcc: ' . $jim_gmail . "\r\n"; // works
// Send message using mail() function
mail($to, $subject, $message, $headers);
// Check to see if headers not sent. If true, redirect to thank_you.php page
if(!headers_sent()){
header('Location: thankyou.html.php');
exit();
}else{
echo "<span class='errMsg'>Message sent successfully!</span><br><br>" .
"Cannot redirect, please click this <a " .
"href=\".\">link</a> instead\n";
}
exit();
}
/******* Functions used *******/
function sanitize($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
You're nearly there:
$message = // contents of report in $message
"
<html>
<head></head>
<body>
<h3>Parts Buyer</h3>
<p>Company: $company</p>
<p>Name: $name</p>
<p>Telephone: $telephone</p>
<p>Email: $email</p>
<h3>Please quote the following:</h3>
<table name='contact_seller' style='border-collapse:collapse';>
<thead>
<tr>
<th>ID</th>
<th>Part Number</th>
<th>Description</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>";
foreach($cart as $item) {
$message .="<tr>
<td>" . $item['id'] ."</td>
<td>".$item['part_number']."</td>
<td>".$item['description']."</td>
<td>".$item['quantity']."</td>
</tr>";
}
$message .= "</tbody>
</table>
<p>End of buyer data report</p>
<hr />
</body>
</html>"; //end of $message
Take note specifically of:
<tbody>";
foreach($cart as $item) {
$message .="<tr>
<td>" . $item['id'] ."</td>
<td>".$item['part_number']."</td>
<td>".$item['description']."</td>
<td>".$item['quantity']."</td>
</tr>";
}
$message .= "</tbody>
</table>
This project is with E-Commerce. There is option to Add to Cart. When user click on add, product will be added to his cart as well and working fine.
But finally when user click place order button Admin will receive Mail with products. That E-mail body should content should display like below.
I tried with using foreach and echo data row by row. But its useless .
Question is - I want retrieve data from cart and add to Above table Format and then it will end with mailing. How to archive this with CI?
Html Table Format
$to = 'mail#gmail.com';
$msg .= '
<table id="table" border="0" cellpadding="5px" cellspacing="1px" style="border: 1px solid #eee">
<tr id= "main_heading">
<td>Name</td>
<td>Price</td>
<td>Qty</td>
<td>Amount</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>';
Note : there is no special code to this(inserting and updating). all the codes are same with ellislab.com
CART VIEW
If i use
$as = $this->cart->contents();
print_r($as);
it shows
Controller
public function index()
{
$data['category'] = $this->Product_Model->get_category();
$data['category2'] = $this->Product_Model->get_category2();
$data['product'] = $this->Product_Model->get_product();
$data['right_brand'] = $this->Product_Model->get_right_brand();
$data['right_prod'] = $this->Product_Model->get_right_product();
$data['brand'] = $this->Product_Model->get_brand_names();
$data['products'] = $this->Product_Model->get_cart_items();
$data['head'] = $this->Product_Model->check_cart();
$this->load->view('template/header', $data);
$this->load->view('template/right_sidebar', $data);
$this->load->view('pages/cart', $data);
$this->load->view('template/foot');
}
public function insert_cart()
{
$data = array(
'id' => $this->input->post('pid'),
'qty' => $this->input->post('qty'),
'price' => $this->input->post('price'),
'name' => $this->input->post('head'),
);
$this->cart->insert($data);
}
function remove($rowid)
{
if ($rowid==="all")
{
$this->cart->destroy();
redirect('index.php/main');
}
else
{
$data = array(
'rowid' => $rowid,
'qty' => 0
);
$this->cart->update($data);
}
// This will show cancle data in cart.
redirect('index.php/cart');
}
function update_cart()
{
$cart_info = $_POST['cart'] ;
foreach( $cart_info as $id => $cart)
{
$rowid = $cart['rowid'];
$qty = $cart['qty'];
$data = array(
'rowid' => $rowid,
'qty' => $qty
);
$this->cart->update($data);
}
redirect('index.php/cart');
}
use jquery and capture the html table that you want to send as email. and pass the variable to the email function using ajax, try this,
<script>
$(document).ready(function() {
var htmlstring = $("#mail_table").html();
$( "#send" ).click(function() {
var cn_name = $("#cn_name").val();
var cn_mail = $("#cn_mail").val();
$.ajax({
method : 'post',
dataType : 'html',
url :'<?php echo base_url(); ?>index.php/cart/testmail',
data :{
name:cn_name,
mail:cn_mail
table:htmlstring
}
});
});
});
</script>
jquery veriable = htmlstring
also you can use jquery serialize method to get all form inputs.
https://api.jquery.com/serialize/
CI ocontroller
public function testmail(){
$to = 'mail#gmail.com';
$subject = 'Enquiry from ';
$message = $_POST['table'];
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
if(mail($to, $subject, $message, $headers))
{
}
}
For view cart and receive it as mail use this...
In mail use this code...
$tpl_file = 'cartmail.php';
if (!file_exists($tpl_file)) {
die('Mail could not be sent');
exit;
}
$msg_tmpl = file_get_contents($tpl_file);
$to = 'mail#gmail.com';
$subject = 'Enquiry from ' .$_REQUEST['guest_name'];
$message = $msg_tmpl;
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: '.$_REQUEST['email'];
mail($to, $subject, $message, $headers);
And in the cartmail.php file use this code
<div class="table-responsive">
<table class="shop_table cart" cellspacing="0">
<thead>
<tr>
<th class="product-remove"> </th>
<th class="product-thumbnail"> </th>
<th class="product-name">Product</th>
<th class="product-price">Price</th>
<th class="product-quantity">Quantity</th>
<th class="product-subtotal">Total</th>
</tr>
</thead>
<tbody>
<?php
$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();
?>
<tr class="cart_item">
<td class="product-name">
<span class="name"><?php echo $obj->product_name; ?> </span> </td>
<td class="product-price">
<span class="amount"><?php echo $currency,$obj->price;?></span> </td>
<td class="product-quantity">
<div class="quantity"><?php echo $cart_itm["qty"];?></div>
</td>
<td class="product-subtotal">
<span class="amount"><?php $ptotal=$obj->price*$cart_itm["qty"]; echo $ptotal; ?></span> </td>
<td class="product-remove">
<?php echo '×';?> </td>
</tr>
<?php
$subtotal = ($cart_itm["price"]*$cart_itm["qty"]);
$total = ($total + $subtotal);
$cart_items ++;
}
?>
</tbody>
</table>
</div>
What am I doing wrong here. When a user submits the form, it shows up in my inbox with their name as their email address and I would like it to be the name that they input in the form. here is my code.
<?php
//error_reporting(0);
//include("connection.php");
$n=$_POST['name'];
$e=$_POST['email'];
$p=$_POST['phone'];
$t1=$_POST['tot_eth'] ;
$t2=$_POST['tot_tax'];
$t3=$_POST['tot_acct'];
$t4=$_POST['tot_tot'];
$i=1;
while($i<13)
{
${'date' . $i} = $_POST["date$i"] ;
${'seminar'.$i}=$_POST["seminar$i"] ;
${'sponser'.$i}=$_POST["sponser$i"] ;
${'ethics'.$i}=$_POST["ethics$i"] ;
${'tax'.$i}=$_POST["tax$i"] ;
${'acct'.$i}=$_POST["acct$i"] ;
${'t'.$i}=$_POST["total$i"] ;
$i++;
}
//session_start();
$to = "mj#ntatax.com";
$from=$n $e;
$subject = "2015 CPE Verification Form";
// compose headers
$headers = 'From: '. $_POST['name'] . ' <' . $e . '>\r\n' .
'Reply-To:' .$from . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// compose message
$message ='<html>
<body>
<p>
From Name : '.$n.'<br>
From Email : '.$e.'<br>
Phone Number : '.$p.'<br><br>
</p>
<table cellpadding="5"> <tr>
<td>Date </td>
<td>Seminar Attended and Location </td>
<td>Sponsor </td>
<td>Ethics Hours </td>
<td>Tax Hours </td>
<td>Acct Hours</td>
<td>Total Hours</td>
</tr>
';
for ($i=1;$i<=13;$i++){
$message .='<tr>
<td>'.${'date' . $i}.'</td>
<td>'.${'seminar'.$i}.'</td>
<td>'.${'sponser'.$i}.'</td>
<td>'.${'ethics'.$i}.'</td>
<td>'.${'tax'.$i}.'</td>
<td>'.${'acct'.$i}.'</td>
<td>'.${'t'.$i}.'</td>
</tr>';
}
$message .=' <tr></tr>
<tr>
<td colspan=2>
<td><p style="text-align: right">Total</p></td>
<td>'.$_POST['tot_eth'].'</td>
<td>'.$_POST['tot_tax'].'</td>
<td >'.$_POST['tot_acct'].'</td>
<td >'.$_POST['tot_tot'].'</td> </td>
</tr> </table>
</body>
</html>';
$message = wordwrap($message, 270);
// send email
mail($to,$subject, $message, $headers,"'".'-f '.$from."'");
// mail($to,$subject, $message, $headers,"'".'-f '.$from."'");
session_start();
//$_SESSION['send1']="Thanks For Contacting Us,Your Query Will Be Replied At the Earliest.";
header("location:http://www.waainc.org");
?>
I'm working in PHP and I have a simple shopping cart which adds items when you click the add button.
Everything is collected in a variable called $cartOutput
When I echo it, it gives me everything in the cart as expected. Same with var_dump . Everything is there
However, when I try to put it in an email and send it off. It cuts off the first item. Can anyone think of why this might be?
Nothing filters it before it is put into the email. It is simply what is in the variable
here is an example...
// e.g of the php variable being assembled for each item
$cartOutput .= "<tr>";
$cartOutput .= "<td>" . $product_name . "</td>";
$cartOutput .= "<td>$" . $price . "</td>";
// emailing the variables off here
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$company = $_POST['company'];
$name = $_POST['name'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$commercialAdd = $_POST['commercial'];
$residentialAdd = $_POST['residential'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal_code = $_POST['postal_code'];
$email = $_POST['email'];
$special_instructions = $_POST['special_instructions'];
$date = date("Y/m/d");
$time = date("h:i:sa");
$to = "xxx#gmail.com";
$header = "Cc:xxx#somedomain.com \r\n";
$subject = "Email Order - $company ($date - $time)";
$message = <<<EOD
<h1>Email Order - $date - $time </h1>
<h3><strong><u>Company:</u></strong> $company</h3>
<h3><strong><u>Name:</u></strong> $Name </h3>
<h3><strong><u>Address:</u></strong> $address<br>
$address2</h3>
<h3><strong><u>Residential:</u></strong> $commercialAdd </h3>
<h3><strong><u>Commercial:</u></strong> $residentialAdd </h3>
<h3><strong><u>City:</u></strong> $city</h3>
<h3><strong><u>Province:</u></strong> $province</h3>
<h3><strong><u>Postal Code:</u></strong> $postal_code</h3>
<h3><strong><u>Phone Number:</u></strong> $phone</h3>
<h3><strong><u>Email:</u></strong> $email</h3>
<h3><strong><u>Special Instructions:</u></strong> $special_instructions</h3>
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Weight (Kg)</th>
<th>Qty</th>
<th>Subtotal</th>
<th></th>
</tr>
</thead>
<tbody>
$cartOutput
<tr>
<td class="totals"><strong>Total</strong></td>
<td class="totals"> </td>
<td class="totals">$weightTotal kg</td>
<td class="totals">$quantityTotal</td>
<td class="totals">$ $cartTotal</td>
<td class="totals"> </td>
<tr>
</tbody>
</table>
EOD;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
header("location: complete.php");
exit();
}
else {
echo "Order could not be sent. Please try again or contact our office for assistance";
}
}
Probably you are running into an email format issue. mail() requires the body to end each line in \r\n AND be less than 70 characters each. You will need to encode you HTML or include it as an attachment. See this set of instructions for an example.