PHP Variable not outputting in full - php

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.

Related

Cannot receive the email message (PHP)

I upload the PHP script to two different domains, but the A domain/server is not working and the B domain/server is able to receive the email message. Someone says that it may be the server setting issue, and someone says that is the script issue. Can anyone help or must use the STMP format only able to work on a certain domain? How to use STMP as I still new in PHP.
<?php
if ($_GET) {
$Name = $_GET['Name'];
$Email = $_GET['Email'];
$Phone = $_GET['phonefull'] . $_GET['Phone'];
$Message = $_GET['Message'];
$Country = $_GET['country'];
$Departmnet = $_GET['Department'];
$PreferedDate = $_GET['PreferedDate'];
$DOB = $_GET['dob'];
$PreferedTime = $_GET['PreferedTime'];
$mine_email = "enquiry#ahppl.com.sg";
if ($_GET['Department'] == 'imaging') {
$to = "ljack4167#gmail.com";
} elseif ($_GET['Department'] == 'Wellness') {
$to = "ctjohn93#gmail.com";
} elseif ($_GET['Department'] == 'Aesthetics') {
$to = "aesthetics#ahppl.com";
}
$subject = "For Appoinment";
$message = '<html><body>';
$message .= "<table border='1px'>
<thead>
<th>Name</th>
<th>Phone</th>
<th>Country</th>
<th>DOB</th>
<th>Email</th>
<th>Department</th>
<th>Preferred Date</th>
<th>Preferred Time</th>
</thead>
<tbody>
<tr>
<td>" . $Name . " </td>
<td>" . $Phone . "</td>
<td>" . $Country . "</td>
<td>" . $DOB . "</td>
<td>" . $Email . "</td>
<td>" . $Departmnet . "</td>
<td>" . $PreferedDate . "</td>
<td>" . $PreferedTime . "</td>
</tr>
<tr>
<th colspan='8'> Message </th>
</tr>
<tr>
<td colspan='8'> " . $Message . "</td>
</tr>
</tbody>
</table>";
$message .= '</body></html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$retval = mail($to, $subject, $message, $headers);
if ($retval == true) {
?>
<script>
localStorage.setItem("mailsend", "true");
window.location = "../index.html";
</script>
<?php
} else {
echo "Message could not be sent...";
}
}
?>
A mail sending issue can be a server issue or php script issue as you said. But for sure you can debug them.
If you are new and want to send emails, then I would suggest using any Mail Libraries in PHP.
PHPMailer is one such library for sending emails and make them less painful. The link is given below.
PHPMailer
If you are using SMTP for sending emails then you should verify the ports (Eg: 25, 465 etc) are open depending upon the protocol you are using. And I would suggest always using some Fake SMTP Server for testing. Mailtrap can do that job for you.
Link to Mail Trap: MailTrap.
I hope these 2 should do the job for you.

Jquery cart data send to e-mail

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
?>

How to code foreach loop in mail message

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>

PHP for loop not iterating fully inside HTML email

I have a PHP script through which I send HTML emails. I'm using a 'for' loop to collect data and storing it in a variable. The loop is instructed to run 25 times. The problem is that it is looping only 19 times. I checked for any unlcosed tags or typos in syntax but didn't find any. I'm posting the for loop section in case any of you can spot what I couldn't. It is really frustrating as I think the solution is very simple and yet I'm unable to spot the problem.
My headers for the 'mail()' function are fine. Here they are just in case
$headers = "From: $from \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
And here is my for loop:
$abc ="<table width='600'>";
$abc .= "<tr>
<td bgcolor='#d6bf86'><span style='color:#9c2a00'>Number of Products</span></td>
<td align='center' bgcolor='#fde4d0'>$pagess</td>
<td align='center' bgcolor='#c3deb9'>$pagese</td>
<td align='center' bgcolor='#bee7f8'>$pagesu</td>
</tr>";
for($i=1; $i<=25; $i++)
{
$abc .="<tr>";
if($i % 2 == 0) // EVEN ROW
{
$abc .= "<td bgcolor='#d6bf86' width='260'><span style='color:#9c2a00'>".${f.$i}."</span></td>";
}
else // ODD ROW
{
$abc .= "<td bgcolor='#fffbd0' width='260'><span style='color:#9c2a00'>".${f.$i}."</span></td>";
}
if(isset(${s.$i}))
{
$abc .= "<td bgcolor='#fde4d0' align='center'>Yes</td>";
${s.$i} = "Yes";
}
else
{
$abc .= "<td bgcolor='#fde4d0' align='center'>No</td>";
${s.$i} = "No";
}
if(isset(${e.$i}))
{
$abc .= "<td bgcolor='#c3deb9' align='center'>No</td>";
${e.$i} = "Yes";
}
else
{
$abc .= "<td bgcolor='#c3deb9' align='center'>Yes</td>";
${e.$i} = "No";
}
if(isset(${u.$i}))
{
$abc .= "<td bgcolor='#bee7f8' align='center'>No</td>";
${u.$i} = "Yes";
}
else
{
$abc .= "<td bgcolor='#bee7f8' align='center'>Yes</td>";
${u.$i} = "No";
}
$abc .="</tr>";
}
if(isset($_POST['dscs'])) // DISCOUNT HAS BEEN APPLIED
{
$abc .= "<tr>
<td>Base Price</td>
<td align='center'>$sums</td>
<td align='center'>$sume</td>
<td align='center'>$sumu</td>
</tr>";
$abc .= "<tr>
<td>Discount Offered</td>
<td align='center'>$discount% </td>
<td align='center'>$discount% </td>
<td align='center'>$discount% </td>
</tr>";
$abc .= "<tr>
<td>Effective Price</td>
<td align='center'>$dscs</td>
<td align='center'>$dsce</td>
<td align='center'>$dscu</td>
</tr>";
}
else
{
$dscs = $sums;
$dsce = $sume;
$dscu = $sumu;
$abc .= "<tr>
<td>Total Price</td>
<td align='center'>$sums</td>
<td align='center'>$sume</td>
<td align='center'>$sumu</td>
</tr>";
}
$abc .="</table>";
I can attach the screenshot of the email that is being sent to give an idea on how the code is breaking. Let me know if you want the screenshot too.
PS : I copy-pasted this code in a separate file and ran it and it was working fine. The loop iterated 25 times. This makes me believe that there is a problem putting it inside HTML email.
Also adding in the email script if that helps:
$to = $clem;
$subject = "Something goes here";
$message = "<html>
<head></head>
<body style='background-color:#ffffff; font-family:Lucida Sans Unicode, Lucida Grande, sans-serif;'>
<table width='600'>
<tr>
<td>$abc</td>
</tr>
</table>
</body>
</html>";
$from = "$logged_user";
$headers = "From: $from \r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$res=mail($to,$subject,$message,$headers);
Thanks in advance,
Nisar
Nevermind, I found what was wrong. Here it is for people with similar problems.
I learnt that html emails have a 990 character limit per line. That was the reason why my code was breaking

Recover data on javascript form

Lately, I've made changes on my javascript code, just a simple contact form and also using colorbox plugin too. It's up and running for 5 days. For the first 2 days I received emails from the form after that I've made changes on the code that causes not to received the forms data from user. Since then, I didn't receive any emails. Is there a way to still retrieve and recover the data send from the form?
if (isset($_POST['register-form'])) {
$_POST = array_map('htmlentities', $_POST);
if ($_POST['type'] == '1') {
$status = "Professional";
}
if ($_POST['type'] == '2') {
$status = "Student";
}
/** Make the mail **/
$headers = "from: noreply#sample.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$subject = "Registration";
$mail = "<table border='0' width='100%' cellpadding='5' cellspacing='1'>
<tr>
<td witdth='15%'><strong>First Name</strong></td>
<td>{$_POST['first-name']}</td>
</tr>
<tr>
<td><strong>Last Name</strong></td>
<td>{$_POST['last-name']}</td>
</tr>
<tr>
<td><strong>Contact Number</strong></td>
<td>{$_POST['contact-number']}</td>
</tr>
<tr>
<td><strong>E-mail</strong></td>
<td>{$_POST['email']}</td>
</tr>
<tr>
<td><strong>Level</strong></td>
<td>{$status}</td>
</tr>
<tr>
<td><strong>Company/School</strong></td>
<td>{$_POST['company-name']}</td>
</tr>
</table>";
mail('sample91#gmail.com', $subject, $mail, $headers);
}

Categories