how to put a php array in an email message? - php

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.

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

Not getting CSS with HTML Based Email In PHP?

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/

form php attachment - specify which info I want to show in mail

I have this code that works good, but when i send it in the mail also appears things that I don't want that appears, for example:
submit = Enviar
recaptcha_challenge_field =
03AHJ_VuuCrxaVdxnIIg-Us7zrqZBDMBXeOuU21J60IblOCtFTQhMgtx-TsfsY6oHl5xbgdeqIRUQ7bTui
recaptcha_response_field = rsinmBlockquote
So I want to ask if there is a way of especify wich items I want to send something like:
$body="Contacto:
Nombre: " .$_POST["nombre"] . "\n
Empresa: " .$_POST["empresa"] . "\n
Ubicacion: " .$_POST["ubicacion"] . "\n
Telefono: " .$_POST["telefono"] . "\n
I'm not a programmer so I don't know how to put it into the actual code that is working:
function form_mail($sPara, $sAsunto, $sTexto, $sDe)
{
$bHayFicheros = 0;
$sCabeceraTexto = "";
$sAdjuntos = "";
if ($sDe)$sCabeceras = "From:".$sDe."\n";
else $sCabeceras = "";
$sCabeceras .= "MIME-version: 1.0\n";
foreach ($_POST as $sNombre => $sValor)
$sTexto = $sTexto."\n".$sNombre." = ".$sValor;
foreach ($_FILES as $vAdjunto)
{
if ($bHayFicheros == 0)
{
$bHayFicheros = 1;
$sCabeceras .= "Content-type: multipart/mixed;";
$sCabeceras .= "boundary=\"--_Separador-de-mensajes_--\"\n";
$sCabeceraTexto = "----_Separador-de-mensajes_--\n";
$sCabeceraTexto .= "Content-type: text/plain;charset=iso-8859-1\n";
$sCabeceraTexto .= "Content-transfer-encoding: 7BIT\n";
$sTexto = $sCabeceraTexto.$sTexto;
}
if ($vAdjunto["size"] > 0)
{
$sAdjuntos .= "\n\n----_Separador-de-mensajes_--\n";
$sAdjuntos .= "Content-type: ".$vAdjunto["type"].";name=\"".$vAdjunto["name"]."\"\n";;
$sAdjuntos .= "Content-Transfer-Encoding: BASE64\n";
$sAdjuntos .= "Content-disposition: attachment;filename=\"".$vAdjunto["name"]."\"\n\n";
$oFichero = fopen($vAdjunto["tmp_name"], 'r');
$sContenido = fread($oFichero, filesize($vAdjunto["tmp_name"]));
$sAdjuntos .= chunk_split(base64_encode($sContenido));
fclose($oFichero);
}
}
if ($bHayFicheros)
$sTexto .= $sAdjuntos."\n\n----_Separador-de-mensajes_----\n";
return(mail($sPara, $sAsunto, $sTexto, $sCabeceras, '-faltitux#altitux.mx'));
}
/* enviando correo */
if (form_mail("mymail#mail.com", $_POST[asunto],
"Los datos introducidos en el formulario son:\n\n", $_POST[email]))
$resultMenuUrlName = "bolsa_gracias.html";
echo "<META HTTP-EQUIV=Refresh CONTENT=0;URL=$resultMenuUrlName>";
} else {
echo "Lo sentimos pero no ha colocado el texto correctamente! Intente nuevamente...";
}
thx in advice and sorry for the bad english
this should work:
remove
foreach ($_POST as $sNombre => $sValor)
$sTexto = $sTexto."\n".$sNombre." = ".$sValor;
in the same spot add:
$sTexto="Contacto:
Nombre: " .$_POST["nombre"] . "\n
Empresa: " .$_POST["empresa"] . "\n
Ubicacion: " .$_POST["ubicacion"] . "\n
Telefono: " .$_POST["telefono"] . "\n

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