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;
Related
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);
?>
I am trying to receive a loop based generated HTML Table in email but seems like I am getting email but no CSS embedded with it as I am passing the BootStrap library CSS files with the email but it's not getting any CSS at all..So I am wondering that what would be the problem...??
Here is screenshot as :
Here is my whole code as :
<?php
$message .= '<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">';
$message .= '<table class="table table-bordered">';
$message .= '<thead>';
$message .= '<tr>';
$message .= '<th>#</th>';
$message .= '<th>Username</th>';
$message .= '<th>Session From</th>';
$message .= '<th>Session Till</th>';
$message .= '<th>Uptime</th>';
$message .= '<th>Download</th>';
$message .= '<th>Upload</th>';
$message .= '<th>Total Usage</th>';
$message .= '</tr>';
$message .= '</thead>';
$message .= '<tbody>';
function human_filesize($bytes, $decimals = 2) {
$factor = floor((strlen($bytes) - 1) / 3);
if ($factor > 0) $sz = 'KMGT';
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . #$sz[$factor - 1] . 'B';
}
if (isset($_GET)) {
$user = $_GET["user"];
}
$x = 1;
$handle = fopen($user, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$split_data = (explode(" ",$line));
if (in_array('customer=admin', $split_data)) {
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'user=') !== false) {
$username = explode("=", $split_data[$i]);
$username = $username[1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'from-time=') !== false) {
$from_time = explode("=", $split_data[$i]);
$from_time = $from_time[1];
$from_time = $from_time." ".$split_data[$i+1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'till-time=') !== false) {
$till_time = explode("=", $split_data[$i]);
$till_time = $till_time[1];
$till_time = $till_time." ".$split_data[$i+1];
}
}
foreach (array_values($split_data) as $i => $value) {
if (strpos($split_data[$i], 'uptime=') !== false) {
$uptime = explode("=", $split_data[$i]);
$uptime = $uptime[1];
$download = explode("=", $split_data[$i+1]);
$download = $download[1];
$upload = explode("=", $split_data[$i+2]);
$upload = $upload[1];
#$total_download += $download;
#$total_upload += $upload;
$total_usage = $total_download+$total_upload;
}
}
$message .= '<tr>';
$message .= '<th scope="row">'.$x.'</th>';
$message .= '<td>'.$username.'</td>';
$message .= '<td>'.$from_time.'</td>';
$message .= '<td>'.$till_time.'</td>';
$message .= '<td>'.$uptime.'</td>';
$message .= '<td>'.human_filesize($download,2).'</td>';
$message .= '<td>'.human_filesize($upload,2).'</td>';
$message .= '<td>'.human_filesize($total_usage,2).'</td>';
$message .= '</tr>';
$x=$x+1;
}
}
fclose($handle);
}
$message .= '</tbody>';
$message .= '</table>';
$to = 'nicefellow1234#gmail.com';
$subject = 'Website Change Reqest';
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
mail($to, $subject, $message, $headers);
?>
As of September 2016, Gmail accepts embedded styles – CSS within <style> tags in the head section of HTML documents.
This is in addition to inline styles, which were previously the only way to apply CSS in Gmail.
At the same time, Google says nothing about support for external styles, which is likely why your Bootstrap styles are failing to load.
https://developers.google.com/gmail/design/
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.
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);
There seems to be a performance issue with the script as it is really slow. I was wondering what I could do to speed this up. If you have any ideas, please let me know. I can't seem to figure it out.
Below is the code:
<?php
include_once("connect.php.inc");
class HtmlEnc{
static function uniord($c) {
$ud = 0;
if (ord($c{0}) >= 0 && ord($c{0}) <= 127) $ud = ord($c{0});
if (ord($c{0}) >= 192 && ord($c{0}) <= 223) $ud = (ord($c{0})-192)*64 + (ord($c{1})-128);
if (ord($c{0}) >= 224 && ord($c{0}) <= 239) $ud = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
if (ord($c{0}) >= 240 && ord($c{0}) <= 247) $ud = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
if (ord($c{0}) >= 248 && ord($c{0}) <= 251) $ud = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128);
if (ord($c{0}) >= 252 && ord($c{0}) <= 253) $ud = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128);
if (ord($c{0}) >= 254 && ord($c{0}) <= 255) $ud = false; // error
return $ud;
}
static function toHtml($str){
$html_str = "";
while (strlen($str) > 0) {
preg_match("/^(.)(.*)$/u", $str, $match);
$test = utf8_decode($match[1]);
if ($test != "?") {
$html_str .= htmlentities(htmlentities($test));
} else if (strlen($match[1]) > 1) {
$html_str .= "&#".self::uniord($match[1]).";";
} else $html_str .= htmlentities(htmlentities($match[1]));
$str = $match[2];
}
return $html_str;
}
}
/*
List of mail servers
*/
function alreadyDone($domain){
$domain = strtolower($domain);
$qry = "SELECT * FROM emdb WHERE domain ='" . $domain . "'";
$result = mysql_query($qry);
return (mysql_num_rows($result)!=0);
}
$template_fn = $_REQUEST['template'];
//"mail_template.html";
$keywords = HtmlEnc::toHtml($_REQUEST['Keywords']);
$keywords = str_replace("&","&",$keywords);
$domain = $_REQUEST['Domain'];
$rank = $_REQUEST['Rank'];
$to = $_REQUEST['Email'];
$adminEmail = "test#example.com";
if (!alreadyDone($domain)) {
if ($to=="") {
$to = "info#" . $domain;
}
function int_divide($x, $y) {
if ($x == 0) return 0;
if ($y == 0) return FALSE;
return ($x - ($x % $y)) / $y;
}
$page = int_divide($rank,10) + 1;
if ($template_fn == "mail_template_nick.html" || $template_fn == "mail_template_chet.html" || "mail_template_salesperson.php")
$subject = $domain." is on Page ".$page." of Google - Want to be #1?";
elseif ($template_fn == "seo_template.html")
$subject = "Outsource your SEO - Lowest rates guaranteed!";
elseif ($template_fn == "adwords_template.html")
$subject = $domain . " - Save your money on Google Adwords";
else $subject = $domain . " is ranked " . $rank . " on Google - Be 1st!";
$message = file_get_contents($template_fn);
/*$message = "<body>
<p>Hi There,</p>
<p>How's your week been so far?</p>
<p>When I Googled "{KEYWORD}", I found {WEBSITE} on page {PAGE}, not on page 1. This means consumers will find your competitors before they find you!</p>
<p>93% of all people, never go past the 1st page of Google, so at this very moment you're losing sales & leads to a competitor. </p>
<p>If you agree your Google exposure needs drastic improvement, please call me for a chat, I'm sure I can give some good, free advice. </p>
<p> </p>
<p><strong>Best Regards,</strong></p>
<p><strong>Kayne Chong </strong><strong>- Business Development Director</strong></p>
<p><strong>Tel:</strong> | <strong>Fax: </strong><br />
<strong>Office:</strong> <br />
<strong>Web:</strong> <a href='http://www.seoagency.com.sg/' target='_blank'><em>www.seoagency.com.sg</em></a><br />
<strong><em>Web marketing that brings BUSINESS to you!</em></strong></p>
</body>";*/
$message = str_replace("{WEBSITE}", $domain , $message);
$message = str_replace("{PAGE}", $page , $message);
//$message = str_replace("{RANK}", $rank , $message);
$message = str_replace("{KEYWORD}", $keywords , $message);
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
/*$headers .= 'Bcc: ' . $adminEmail . "\r\n";
if ($template_fn == "mail_template_salesperson.php")
{ $headers .= 'From: Kayne - Web Marketing Experts <test#example.com>' . "\r\n";
$headers .= 'Reply-To: test#example.com' . "\r\n";}
elseif ($template_fn == "mail_template_chet.html")
{ $headers .= 'From: Chester - Web Marketing Experts <test#example.com>' . "\r\n";
$headers .= 'Reply-To: test#example.com' . "\r\n";}*/
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'Bcc: ' . $adminEmail . "\r\n";
$headers .= 'From: Info - Web Marketing Experts <test#example.com>' . "\r\n";
$headers .= 'Reply-To: test#example.com' . "\r\n";
if (mail($to, $subject, $message, $headers)) {
echo "Mail successfully sent to $to and $adminEmail";
} else echo "Mail sending failed!";
$qry = "INSERT INTO emdb (domain, keywords, rank, last, count) VALUES ('$domain','$keywords','$rank',CURDATE(), '1')";
mysql_query($qry) or die(mysql_error());
echo "<BR />";
echo "DB updated";
} else {
echo "Domain name $domain has already been processed";
}
?>
Thank you.
Jae
Replace every string concatenation with the "." operator with an array push, for example array [ ] = "foo" and then return a string concatenation implode ( array );
Use ob_start(); to cache the output:
ob_start();
echo $a,$b,$c;
$str = ob_get_contents();
ob_end_clean();
You can optimize if to switch and change the order to your expected result. For example if result a is more likely then result b the condition to catch result a should be the first condition.
Put a primary key and secondary key on your table(s).
1.1) Don't use a glue and don't add the construction of the array to the time. Here is a benchmark for http://www.sitecrafting.com/blog/php-string-concat-vs-array
1.2) http://dan.doezema.com/2011/07/php-output-profiling-echo-vs-concat ( although echo is fastest concat is slower then array and also he uses a glue!
1.3) https://stackoverflow.com/questions 1.4) http://www.sitepoint.com/high-performance-string-concatenation-in-php/
Here are my results (30000 strings, time in milliseconds) (Script is taken from 1.4):
standard: 0.02418089
implode w/ glue: 0.00435901
implode w/o glue: 0.02205801
foreach: 0.02081609
Conclusion: use implode with glue.
Your toHtml() is pointless (not to mention it's implemented poorly hence the low performance), you don't need to convert every unicode character to &#...; notation, just put this in your <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
and print utf-8 strings as they are, your browser will know how to deal with them.