How can I email this shopping cart array? - php

I can't figure out how to email the entire shopping cart array. I tried using the print_r function, but I only get one item in the result.
Any help would be greatly appreciated.
<?php session_start(); ?>
<?php
if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
echo '<p>Your shopping cart is empty, so get shopping.</p>';
} else {
echo '<table>
<tr>
<th>Product</th>
<th>Cost</th>
<th>Units</th>
<th>Subtotal</th>
</tr>';
$total = 0;
foreach($_SESSION['cart'] as $item) {
echo "<tr>
<td>{$item['item']}</td>
<td>\${$item['unitprice']}</td>
<td>{$item['quantity']}</td>
<td>$".($item['unitprice'] * $item['quantity'])."</td>
</tr>";
$total += ($item['unitprice'] * $item['quantity']);
}
echo '</table>';
echo "<p>Grand total: \$$total</p>";
}
?>
<?php
$to = 'blah#gmail.zzz';
$subject = 'the subject';
$body = print_r($item);
$headers = 'From: blah#gmail.zzz' . "\r\n" .
'Reply-To: blah#gmail.zzz' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $body, $headers);
?>

Only the last item is shown, because your foreach ( $_SESSION['cart'] as $item ) ... loop uses the $item variable. $item is re-assigned each loop. At the end of the loop, the last value remains.
Try $body = print_r($_SESSION['cart'], true); instead.

Try this:
<?php
session_start();
$body = '';
if (!isset($_SESSION['cart']) || (count($_SESSION['cart']) == 0)) {
echo '<p>Your shopping cart is empty, so get shopping.</p>';
} else {
echo '<table>
<tr>
<th>Product</th>
<th>Cost</th>
<th>Units</th>
<th>Subtotal</th>
</tr>';
$total = 0;
foreach($_SESSION['cart'] as $item) {
echo "<tr>
<td>{$item['item']}</td>
<td>\${$item['unitprice']}</td>
<td>{$item['quantity']}</td>
<td>$".($item['unitprice'] * $item['quantity'])."</td>
</tr>";
$total += ($item['unitprice'] * $item['quantity']);
$body .= print_r($item, true)."\n";
}
echo '</table>';
echo "<p>Grand total: \$$total</p>";
}
$to = 'blah#gmail.zzz';
$subject = 'the subject';
$body = "<pre>{$body}</pre>";
$headers = 'From: blah#gmail.zzz' . "\r\n" .
'Reply-To: blah#gmail.zzz' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $body, $headers);

Related

Using expressmail with PHP form

We have a php script that emails form field values when a user submits the form. The form action points to the script below.
We've been asked to configure things to use expressmail explicitly. My question is, would this entail a modification to the script or is this a config setting on the server somewhere?
<?php
if (! $_POST) {
header('HTTP/1.0 405 Method Not Allowed');
exit;
}
$redirectTo = html_entity_decode($_POST['post']);
$body = '<html><body>';
$content = array();
foreach ($_POST as $key => $value) {
if ('-label' !== substr($key, -6)) {
continue;
}
$field = substr($key, 0, strlen($key) - 6);
$content[$field]['value'] = $_POST[$field];
$content[$field]['label'] = $_POST[$key];
}
$body .= '<h1>' . htmlentities($_POST['formName']) . '</h1>';
foreach ($content as $field => $value) {
$data = $value['value'];
$label = $value['label'];
$body .= '<p><b>' . htmlentities($label) . '</b><br />';
if (false === is_array($data) && (null === $data OR "" === trim($data))) {
$body .= 'N/A';
} elseif (is_array($data)) {
$body .= '<ul>';
foreach ($data as $val) {
$val = htmlentities($val);
$body .= '<li>' . $val . '</li>';
}
$body .= '</ul>';
} else {
$body .= htmlentities($data);
}
$body .= '</p>';
}
$body .= '</body></html>';
$to = strip_tags($_POST['emailTo']);
$subject = strip_tags($_POST['emailSubject']);
$headers = "From: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "Reply-To: " . strip_tags($_POST['emailFrom']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $body, $headers);
header('Location: ' . $redirectTo);
?>

how to put a php array in an email message?

I have a general question about sending an email trough php. How do you put php arrays in the message part of the mail?
below you can see a simple array that i made and tried to put it in the email but this is totally wrong cause I didnt find it on the internet how to do it (I am a beginner. I just started coding with php).
The array:
<?php
$Array[1] = array('qualitypoint', 'technologies', 'India','Hey');
$Array[2] = array('quality', 'tech', 'Ind','He');
$Array[3] = array('q', 't', 'I','H');
?>
The mailer:
<?php
include "index.php";
while($row = $LevAdres->fetch_assoc()) {
$email=null;
$email= $row['Email'];
}
$to = example#hotmail.com;
$subject = "Bestelling";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: Restaurant#test.be" . "\r\n";
$headers .= "Reply-To: people#info.be". "\r\n";
$headers .= "CC: Restaurant#test.be\r\n";
$message = "<?php
echo '<table>';
echo '<table>';
for($i=0;$i<=3;$i++)
{
echo "<tr>";
for($j=1;$j<=3;$j++)
{
echo "<td>{$Array[$j][$i]}<td>";
}
echo "</tr>";
}
echo '</table>';
?>";
if (mail($to, $subject, $message, $headers)) {
echo '<p>successfully sent.</p>';
} else {
echo'<p>delivery failed...</p>';
}
?>
This is how the table must look like
Here you go ... complete code
$Array[1] = array('qualitypoint', 'technologies', 'India', 'Hey');
$Array[2] = array('quality', 'tech', 'Ind', 'He');
$Array[3] = array('q', 't', 'I', 'H');
$to = "example#hotmail.com";
$subject = "Bestelling";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: Restaurant#test.be" . "\r\n";
$headers .= "Reply-To: your#email.com" . "\r\n";
$headers .= "CC: Restaurant#test.be\r\n";
$message = "";
$message .= '<table>';
for ($i = 0; $i <= 3; $i++) {
$message .= "<tr>";
for ($j = 1; $j <= 3; $j++) {
$message .= "<td>{$Array[$j][$i]}</td>";
}
$message .= "</tr>";
}
$message .= '</table>';
if (mail($to, $subject, $message, $headers)) {
echo '<p>successfully sent.</p>';
} else {
echo'<p>delivery failed...</p>';
}
Change your $message variable to this.
$message = "<table>";
for($i=0;$i<=3;$i++)
{
$message .= "<tr>";
for($j=1;$j<=3;$j++)
{
$message .= "<td>{$Array[$j][$i]}<td>";
}
$message .= "</tr>";
}
$message .= '</table>';
I can see that you have a doublequotes issue. Try putting it this way:
echo '<td>'."{$Array[$j][$i]}".'<td>';
If you want to put it into message then don't echo it but write it in message body
change your echo code to
$array = [
['qualitypoint', 'technologies', 'q'],
['technologies', 'tech', 't'],
['India', 'ind', 'i'],
];
$message = "";
$message .= '<table>';
foreach ($array as $row)
{
$message .= "<tr>";
foreach ($row as $cell)
{
$message .= "<td>$cell</td>";
}
$message .= "</tr>";
}
$message .= '</table>';
However if you want to echo it then you can catch output buffering and use it contents but I don't recommend it. Using foreach makes it more readable as well. Also consider naming your variables with lower camel case.

Can't get the name from $NAME

I have a subscription form that contains tow variables $name and $email.
and I get only the $email from the form.I can't get the $name.
I have tried all I knew to fix it, the form is fully working, it sends the code to email and writes to the file,just doesn't give me name!
Thanks.
This is the PHP code:
<?php
header('content-type: application/json');
$o = new stdClass();
$o->status = 'success';
echo json_encode($o);
$Length = 9;
$RandomString = substr(str_shuffle(md5(time())), 0, $Length);
$email = $_POST["email"];
$name = $_POST["author"];
$emailTo = 'info#praia.co.il';
$subject = 'הרשמה לרכישה קבוצתית פראיה ';
$body = "\n\nשם הלקוח: $name \n\nאימייל: $email \n\nקוד קופון: $RandomString";
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($emailTo, $subject, $body, $headers, $randomString);
$headers2 = "From: Praia <info#praia.co.il>" . "\r\n".
'Reply-To: '.$emailTo."\r\n" .
'Content-Type: text/html; charset=ISO-8859-1\r\n'.
'Return-Path: Praia <info#praia.co.il>\r\n'.
'X-Mailer: PHP/' . phpversion();
$message = '<html><body><center><div style="background:#f0f0f0; font-family:"almoni-tzar";>';
$message .= '<img src="http://praia.co.il/assets/img/logo.png" alt="logo" style="width:150px; height:150px;"></a><br />';
$message .= '<br />';
$message .= " שלום $name, אנו שמחים שהצטרפת לרכישה הקבוצתית של פראיה.</p>";
$message .= '<br>';
$message .= "$RandomString :קוד ההרשמה שלך לרכישה הוא ";
$message .= '<br />';
$message .= "קוד ההרשמה הינו חד פעמי*";
$message .= '<br />';
$message .= '<br />';
$message .= '<br />';
$message .= '<br />';
$message .= ' מעבר לאתר מעבר לתקנון';
$message .= '</div></center></body></html>';
$mailed = true;
if($mailed==true){
mail($email, "פראיה- הרשמה לרכישה קבוצתית.", $message , $headers2);
}else{
echo 'error';
}
if($mailed==true){
//file_put_contents("coupon.txt", $email. . $RandomString . PHP_EOL, FILE_APPEND);
file_put_contents("coupon.txt", $email . " " . $RandomString . "\r\n" , FILE_APPEND | LOCK_EX);
}else{
echo 'error';
}
?>
JS code:
/*
notifyMe jQuery Plugin v1.0.0
Copyright (c)2014 Sergey Serafimovich
Licensed under The MIT License.
*/
(function(e) {
e.fn.notifyMe = function(t) {
var r = e(this);
var i = e(this).find("input[name=email]");
var s = e(this).attr("action");
var o = e(this).find(".note");
e(this).on("submit", function(t) {
t.preventDefault();
var h = i.val();
var p = /^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (p.test(h)) {
$(".message").removeClass("error bad-email success-full");
$(".message").hide().html('').fadeIn();
o.show();
e.ajax({
type: "POST",
url: s,
data: {
email: h
},
dataType: "json",
error: function(e) {
o.hide();
if (e.status == 404) {
$(".message").hide().html('<p class="notify-valid style="font-family:almoni-tzar;"> .אופס, נראה שמשהו השתבש! נסה מאוחר יותר <i class="icon ion-close-round"></i></p>').slideDown();
} else {
$(".message").hide().html('<p class="notify-valid style="font-family:almoni-tzar;"> .אופס, נראה שמשהו השתבש! נסה מאוחר יותר <i class="icon ion-close-round"></i></p>').fadeIn();
}
}
}).done(function(e) {
o.hide();
if (e.status == "success") {
$(".message").removeClass("bad-email").addClass("success-full");
$(".message").hide().html('<p class="notify-valid" style="font-family:almoni-tzar;"> .נרשמת בהצלחה לרכישה הקבוצתית תעודכן בהמשך <i class="icon ion-checkmark-round"></i></p>').fadeIn();
} else {
if (e.type == "ValidationError") {
$(".message").hide().html('<p class="notify-valid style="font-family:almoni-tzar;"> .כתובת המייל נראית לא חוקית, נא הזן כתובת חדשה <i class="icon ion-close-round"></i></p>').fadeIn();
} else {
$(".message").hide().html('<p class="notify-valid style="font-family:almoni-tzar;"> .אופס, נראה שמשהו השתבש! נסה מאוחר יותר <i class="icon ion-close-round"></i></p>').fadeIn();
}
}
})
} else {
$(".message").addClass("bad-email").removeClass("success-full");
$(".message").hide().html('<p class="notify-valid" style="font-family:almoni-tzar;"> .כתובת המייל אינה חוקית נא נסה שנית <i class="icon ion-close-round"></i></p>').fadeIn();
o.hide();
}
// Reset and hide all messages on .keyup()
$("#notifyMe input").keyup(function() {
$(".message").fadeOut();
});
})
}
})(jQuery)
Your ajax call is only sending the mail. You should change
var i = e(this).find("input[name=email]");
...
data: {
email: h
},
To something along the lines of:
var i = e(this).find("input[name=email]");
var authorValue = e(this).find("input[name=author]");
...
data: {
email: h,
author: authorValue
},
While adding on your list above the

How to mail grandTotal value in simpleCartjs

I'm trying to send all data from the shopping cart in SimpleCartjs to an email. Basically I prefer to send it to a clients email for confirmation of his order. So for now I got it working, but I cannot seem to send the grandTotal, which is the sum of everything (cost + Tax + Shipping). I am able to echo it to the screen, but when mailing it the grandTotal will not send. Any ideas? here is my code so far. The grandTotal in the end of the body field is just there, But I dont know how to get its value:
$send = $_GET['send'];
$to = 'name#myname.com';
$subject = 'Your Tea shopping Cart';
$content = $_POST;
$body = '';
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i;
$quantity = 'item_quantity_'.$i;
$price = 'item_price_'.$i;
$tax = $_POST['taxRate'];
$ship = $_POST['shipping'];
$total = $content[$quantity]*$content[$price];
$iva = $content[$price]*$tax;
$subtotal = $total + $ship + $iva;
$body .= 'item #'.$i.':';
$body .= $content[$name].'<br>';
$body .= $content[$quantity].'Un.: '.$content[$price].'Euros<br>';
$body .= 'IVA : '.$content[$price]*$tax.'Euros<br>';
$body .= 'Shipping: '. $ship.'Euros<br>';
$body .= 'Total: '.$subtotal.'Euros<br>';
$body .= '------------------------------------<br>';
};
$body .= 'Total: '.$grandTotal;
$headers = 'From: name#myname.com' . "\r\n" .
'Reply-To: name#myname.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $body, $headers);
First initialize $grandTotal = 0; before the for-loop.
Then, inside the for-loop, after calculating the subtotal, add $grandTotal = $grandTotal + $subTotal;

Html to this code - PHP Form

I am new to php and my webhost does not allow me to run simple php form available on http://www.freecontactform.com/email_form.php
they guided me to a knowledge base and asked me to use this php code i tried to intergrate this with my current form and it just doesn't send any email
Here is the php code:
<?
$mailto = "xyz#yourdomain.com";
$file = "thanks.htm";
$pcount = 0;
$gcount = 0;
$subject = "Mail from Enquiry Form";
$from = "some-name#yourdomain.com";
while (list($key, $val) = each($_POST)) {
$pstr = $pstr . "$key : $val \n ";
++$pcount;
}
while (list($key, $val) = each($_GET)) {
$gstr = $gstr . "$key : $val \n ";
++$gcount;
}
if ($pcount > $gcount) {
$message_body = $pstr;
mail($mailto, $subject, $message_body, "From:" . $from);
include("$file");
} else {
$message_body = $gstr;
mail($mailto, $subject, $message_body, "From:" . $from);
include("$file");
}
?>
Here is the html
http://pastebin.com/Nzq0TCVp
Any suggestions?

Categories