Here is a small snippet of the PHP-Code I use to create a HTML e-mail:
$tmpl = '<table border="0" width="100%";><tr><td>%title%</td></tr><tr><td>%text%</td></tr></table>';
$html = '<table border="0" width="100%";>';
$html .= '<td width="20%" style="padding:0;">';
if(isset($var)) {
$html .= 'Value: '.$object->val;
}
$html .= '</td>';
$html .= '</table>';
$tmpl = str_replace('%text%', $html, $tmpl);
$mail->setBody( $tmpl );
$mail->send();
Sometimes the mail in the HTML view when viewed inside an email program is broken because there is a space character inside an opening TD-Element. Like this:
< td width="20%" style="padding:0;">
Where is this space character coming from?
Had the same issue with php mail() function, my html styling was broken by seemingly random spaces. Using wordwrap() on the message before sending it with mail sorted out the problem.
$message = wordwrap($message, 70);
mail("person#example.com","Some subject",$message);
www.w3schools.com explanation, example
See this line here?
$html = '<table border="0" width="100%";>';
after width="100%" you do not need this symbol -> ;
Try removing it.
Markup has errors
$tmpl = '<table border="0" width="100%";><tr><td>%title%</td></tr><tr><td>%text%</td></tr></table>';
Notice the ; in border="0" width="100%";>
You're not concatenating $html
The following line overwrite the table built up earlier
$html = '</table>';
You need to change that to;
$html .= '</table>';
Suggested actions
I would suggest echoing the body of the mail, and validating it with w3.
Have a read...
Concatenating
Validate your markup
<?php
$tmpl = '
<table border="0" width="100%">
<tr>
<td>%title%</td>
</tr><tr>
<td>%text%</td>
</tr>
</table>';
$html = '<table border="0" width="100%">';
$html .= '<td width="20%" style="padding:0;">';
if(isset($var)) {
$html .= 'Value: '.$object->val;
}
$html .= '</td>';
$html .= '</table>';
//Debug:
// echo $html;
$tmpl = str_replace('%text%', $html, $tmpl);
$mail->setBody( $tmpl );
$mail->send();
Related
I want to send back to a jquery post method a "view", html code (not the whole page, just a div or a table) with a database result, so then I can display with the jquery html method on the page where is the original form.
I remember that you can do that with Codeigniter (and json), you use "load-view" and add to it data from your database and the view will execute the php code with that data; but I'm not using any framework, it is possible to do this?
The only solution until now for me is to display in my php file that I send the post all the results in a variable with html code and then make a echo as a response, but when you have to display a lot of rows, it is very complicated.
This is an example:
$html = "
<table class='table table-bordered table-hover' id='servicios'>
<thead>
<tr>
<th>#</th>
<th>Tipo</th>
<th>Proveniencia</th>
<th>Nombre</th>
<th>Descripción</th>
<th>Valor</th>
<th>Iva</th>
</tr>
</thead>
<tbody>";
$i = 1;
foreach($resultado as $print){
$html .= "<tr class='dynamic-row' data-href='#' data-id='{$print['id_servicio']}'>
<th scope='row'>$i</th>
<td>{$print['nombre_tipo']}</td>";
$html .= "<td>";if($print['proveniencia'] == 'interno') $html .= "Interno";else $html .= "Externo";$html .= "</td>";
$html .= "<td>{$print['nombre_servicio']}</td>
<td>{$print['descripcion_servicio']}</td>
<td>";if(!empty($print['valor_servicio'])) $html .= "{$print['valor_servicio']}";$html .= "</td>
<td>";if($print['iva_servicio']) $html .= "Sí";else $html .= "No";$html .= "</td>
</tr>";
$i++;
}
$html .= "</tbody>
</table>";
So I'm looking for a better and simplier solution.
PS: I perfectly know how to use jquery post, and php with PDO. So my only problem is the one I mention.
<?php
include 'simple_html_dom.php';
// Create DOM from URL
$html = file_get_html('http://some site. com/'); ?>
<?php
foreach($html->find('a.cellMainLink') as $title)
echo '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
<td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
?>
<?php
foreach($html->find('td.nobr') as $size)
echo '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
<td>'.$size.'</td></tr></table></div>';
?>
<?php
foreach($html->find('td.green') as $seeds)
echo '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr> <td>'.$seeds.'</td></tr></table></div>'; ?>
<?php
foreach($html->find('td.red') as $leechs)
echo '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr> <td>'.$leechs.'</td></tr></table></div>'; ?>
<?php
foreach($html->find('strong a[1]') as $category)
echo '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr><td>'.$category->plaintext.'</td></tr></table></div>'; ?>
<?php
foreach($html->find('a[class=idownload icon16]') as $down)
echo '<div style="font-size:12px">'.$down->href.'</div><br />'; ?>
right now the result in multiple echos.. i want all these results in one echo and then i will store the result into my database..
Use .=, in a php variable. It is the concatenating assignment operator.
<?php
include 'simple_html_dom.php'; // Create DOM from URL
$html = file_get_html('http://some site. com/');
$what_you_echo = "";
?>
<?php
foreach($html->find('a.cellMainLink') as $title)
$what_you_echo .= '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
<td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
?>
<?php
foreach($html->find('td.nobr') as $size)
$what_you_echo .= '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
<td>'.$size.'</td></tr></table></div>';
?>
<?php
foreach($html->find('td.green') as $seeds)
$what_you_echo .= '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr> <td>'.$seeds.'</td></tr></table></div>'; ?>
<?php
foreach($html->find('td.red') as $leechs)
$what_you_echo .= '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr> <td>'.$leechs.'</td></tr></table></div>'; ?>
<?php
foreach($html->find('strong a[1]') as $category)
$what_you_echo .= '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr><td>'.$category->plaintext.'</td></tr></table></div>'; ?>
<?php
foreach($html->find('a[class=idownload icon16]') as $down)
$what_you_echo .= '<div style="font-size:12px">'.$down->href.'</div><br />'; ?>
<?php echo $what_you_echo; ?>
You can style your code, by removing these lots of <?php:
<?php
include 'simple_html_dom.php'; // Create DOM from URL
$html = file_get_html('http://some site. com/');
$what_you_echo = "";
foreach($html->find('a.cellMainLink') as $title)
$what_you_echo .= '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr><td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
foreach($html->find('td.nobr') as $size)
$what_you_echo .= '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr><td>'.$size.'</td></tr></table></div>';
foreach($html->find('td.green') as $seeds)
$what_you_echo .= '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr> <td>'.$seeds.'</td></tr></table></div>';
foreach($html->find('td.red') as $leechs)
$what_you_echo .= '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr> <td>'.$leechs.'</td></tr></table></div>';
foreach($html->find('strong a[1]') as $category)
$what_you_echo .= '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr><td>'.$category->plaintext.'</td></tr></table></div>';
foreach($html->find('a[class=idownload icon16]') as $down)
$what_you_echo .= '<div style="font-size:12px">'.$down->href.'</div><br />';
echo $what_you_echo; ?>
Use a variable, and append the values.
<?php
$out = '';
include 'simple_html_dom.php';
// Create DOM from URL
$html = file_get_html('http://some site. com/');
foreach ($html->find('a.cellMainLink') as $title) {
$out .= '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
<td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
}
foreach ($html->find('td.nobr') as $size) {
$out .= '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
<td>'.$size.'</td></tr></table></div>';
}
foreach ($html->find('td.green') as $seeds) {
$out .= '<div class="seeds"><table class="seeds" cellspacing="0" cellpadding="0"><tr>
<td>'.$seeds.'</td></tr></table></div>';
}
foreach ($html->find('td.red') as $leechs) {
$out .= '<div class="leechs"><table class="leechs" cellspacing="0" cellpadding="0"><tr>
<td>'.$leechs.'</td></tr></table></div>';
}
foreach ($html->find('strong a[1]') as $category) {
$out .= '<div class="cat"><table class="cat" cellspacing="0" cellpadding="0"><tr>
<td>'.$category->plaintext.'</td></tr></table></div>';
}
foreach ($html->find('a[class=idownload icon16]') as $down) {
$out .= '<div style="font-size:12px">'.$down->href.'</div><br />';
}
echo $out;
?>
I'm not sure but maybe this?:
<?php
include 'simple_html_dom.php'; // Create DOM from URL
$html = file_get_html('http://some site. com/');
$el_arr['a.cellMainLink'] = $html->find('a.cellMainLink');
$el_arr['td.nobr'] = $html->find('td.nobr');
$el_arr['td.green'] = $html->find('td.green');
$el_arr['td.red'] = $html->find('td.red');
$el_arr['strong [1]'] = $html->find('strong [1]');
$el_arr['a[class=idownload icon16]'] = $html->find('a[class=idownload icon16]');
foreach($el_arr as $key=>$el)
{
foreach($el as $node)
{
//echo your content accordingly here using $key and $node
//$key represent the type of element and $node represent a node of that type from html
}
}
creat a php array and put the results of each echo in this array and after echo the array
exemple:
<?php
$results=array[];
$our_var='';
foreach($html->find('a.cellMainLink') as $title)
{
$our_var='<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr>
<td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
array_push($results,$our_var);
}
?>
do this to the others and after play with your array as you wish
include 'simple_html_dom.php'; // Create DOM from URL
$html = file_get_html('http://some site. com/');
$what_you_echo = "";
foreach($html->find('a.cellMainLink') as $title)
echo '<div class="title"><table class="title" cellspacing="0" cellpadding="0"><tr><td style="font-size:12px;">'.$title->plaintext.'</td></tr></table></div>';
$size = current($html->find('td.nobr'));
echo '<div class="size"><table class="size" cellspacing="0" cellpadding="0"><tr>
<td>'.$size.'</td></tr></table></div>';
next($html->find('td.nobr'));
how about this
I'm trying to send an HTML email receipt from a shopping cart purchase but I don't get how to echo the session array within the EOF area. php doesn't seem to want to execute here. Thoughts?
//begin of HTML message
$message = <<<EOF
<html>
<body style="font-family: 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif;
font-size:10px;"><center>
<table width="750" cellpadding="0" cellspacing="0" class="view-cart" style="text-
align:center;padding:5px;"><tr>
<tr><td>Image</td><td>SKU</td><td>Description</td><td>QTY</td><td>Price</td><td> </td></tr>
<tr>
foreach ($_SESSION["products"] as $cart_itm)
{
echo '<td><img src="'.$cart_itm['image'].'"></td>';
echo '<td>';
echo '<div class="p-code">'.$cart_itm["code"].'</td>';
echo '<td align="left">'.$cart_itm["description"].'</td>';
echo '<td>'.$cart_itm["qty"].'</td>';
echo '<td><div class="p-price">'.$cart_itm["price"].' each</div></td>';
echo '<td></td>';
echo '</tr>';
}
</table>
</center>
</body>
</html>
EOF;
//end of message
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
The updated code now looks like this. I replaced the "$message +=" with "$message .=" and it seems to work fine now. :
//begin of HTML message
$message = <<<EOF
<html>
<body style="font-family: 'Myriad Pro', 'DejaVu Sans Condensed', Helvetica, Arial, sans-serif; font-size:10px;"><center>
<table width="750" cellpadding="0" cellspacing="0" class="view-cart" style="text-align:center;padding:5px;">
<tr><td>Image</td><td>SKU</td><td>Description</td><td>QTY</td><td>Price</td><td> </td></tr>
<tr>
EOF;
foreach ($_SESSION["products"] as $cart_itm)
{
$message .= "<td><img src=".$cart_itm['image'].">";
$message .= "</td><td>".$cart_itm['code'];
$message .= "</td><td>".$cart_itm['description'];
$message .= "</td><td>".$cart_itm['qty'];
$message .= "</td><td>".$cart_itm['price'] ;
$message .= "each</td><td></td></tr>";
}
$message2 = <<<EOF
</table>
<hr><table width="750" cellpadding="0" cellspacing="0" class="view-cart"><tr>
<td align="right"><b>Shipping: $ $shipping </b><br><b>Total: $ $total </b></td></tr></table>
</center>
</body>
</html>
EOF;
What you're doing is creating a string using the heredoc syntax; so everything that's after the first newline and before the identifier (EOF in your case) is considered part of that string, and no code will be executed inside of it besides variables and escape sequences for special characters.
As far as I know there is no solution besides ending the string just before the loop, then replacing all instances of echo with $message += in the loop and finally creating a new string (using heredoc or conventional quotes and escaped newlines) after the loop and appending that new string to the $message.
Another (bad in my opinion) approach would be to put a placeholder for the loop's contents in the heredoc, then creating a new string that will hold the output of the loop and finally replacing the placeholder with that string.
Edit: for your updated code, note that the identifier EOF1 should be at the beginning of a new line and be terminated with a semicolon.
Also, the identifiers aren't supposed to be unique, it's fine to use the same identifier for different strings, no need for EOF1, 2, etc.
Im trying to write code which will add javascript in a variable. Everything is going find until i get the error "SyntaxError: missing ; before statement" ... I have found out that if i remove a line then it works (i will put // ---- on that line). Please can someone help?
$this->paint_javascript_function_text .= '
function image_'.$this->archive_id.'_rollover (){
document.getElementById("details").innerHTML = "'.$this->archive_name.'<br />'.$this->archive_dimensions.' - Price £'.$this->archive_price.' inc Packaging";
}
function image_'.$this->archive_id.'_fullsize (){
document.getElementById("gallery").innerHTML = ';
$this->paint_javascript_function_text .= "'";
$this->paint_javascript_function_text .= '<table width="100%" height="400"border="0" align="center"><tr><td align="right" valign="middle">';
if($this->archive_id == 1) {
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/prev_faded.png" border="0" class="paint_nav" />';
}
else
{
$prev = $this->archive_id - 1;
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/prev.png" border="0" class="paint_nav" onclick="javascript:image_'.$prev.'_fullsize();" />';
}
$this->paint_javascript_function_text .= '</td><td width="400px" height="523px" align="center" valign="middle">'.$newimage.'<br />Back to Gallery</td><td align="left" valign="middle">';
if($total_num == $this->archive_id){
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/next_faded.png" border="0" class="paint_nav" />';
}
else
{
$next = $this->archive_id + 1;
$this->paint_javascript_function_text .= '<img src="images/paint_gallery/next.png" border="0" onclick="javascript:image_'.$next.'_fullsize();" class="paint_nav" />';
}
$this->paint_javascript_function_text .= '</td></tr></table>';
// ---- $this->paint_javascript_function_text .= '<center><span class="paintings_title">'.$this->archive_name.' - '.$this->archive_dimensions.' - '.$this->archive_price.' inc P&P</span><br /><img src="images/shop/basket_button.png" border="0" /></center>';
$this->paint_javascript_function_text .= "';
";
$this->paint_javascript_function_text .= '
$("#main_content").hide();
$("#gallery").show();
$("#paint_nav").hide();
}';
HTML Output:
function image_4_fullsize (){
document.getElementById("gallery").innerHTML = '<table width="100%" height="400" border="0" align="center"><tr><td align="right" valign="middle"><img src="images/paint_gallery/prev.png" border="0" class="paint_nav" onclick="javascript:image_3_fullsize();" /></td><td width="400px" height="523px" align="center" valign="middle"><img src="images/print_gallery/Countrymans Kitchen.jpg" /><br />Back to Gallery</td><td align="left" valign="middle"><img src="images/paint_gallery/next.png" border="0" onclick="javascript:image_5_fullsize();" class="paint_nav" /></td></tr></table><center><span class="paintings_title">Countryman's Kitchen - - inc P&P</span><br /><img src="images/shop/basket_button.png" border="0" /></center>';
$("#main_content").hide();
$("#gallery").show();
$("#paint_nav").hide();
}
UPDATED
Change below line
$this->archive_name contains a single quote, so you need to escape that first with json_encode
$this->paint_javascript_function_text .= 'function image_'.$this->archive_id.'_rollover () {
document.getElementById("details").innerHTML = '.json_encode( $this->archive_name.'<br />'.$this->archive_dimensions.' - Price £'.$this->archive_price.' inc Packagin' ).';
}
function image_'.$this->archive_id.'_fullsize() {
document.getElementById("gallery").innerHTML = ';
You are concatenating using shorthand ".="
And then concatenating "the normal way".
try this way:
$this->paint_javascript_function_text = $this->paint_javascript_function_text . '<center><span class="paintings_title">'.$this->archive_name.' - '.$this->archive_dimensions.' - '.$this->archive_price.' inc P&P</span><br /><img src="images/shop/basket_button.png" border="0" /></center>';
function my_fav_quote_show_optin_form() {
if (!empty($_POST['my_fav_quote_email'])) {
my_fav_quote_opt_in();
}
$out2 = '';
$out = '<form action="" method="post" id="requestQuote">';
$out .= '<table style="padding="0px" width="40px">';
$out .= '<tr><td>Name:*</td><td><input type="text" name="my_fav_quote_name" id="my_fav_quote_name"/></td></tr>';
$out .= '<tr><td colspan=1></td></tr>';
$out .= '<tr><td>Email:*</td><td><input type="text" name="my_fav_quote_email" id="my_fav_quote_email"/></td></tr>';
$out .= '<tr><td colspan=2></td></tr>';
$out .= '<tr><td>Message:</td><td><textarea cols=20 rows=5 wrap="hard" name="my_fav_quote_message" id="my_fav_quote_message"></textarea></td></tr>';
$out .= '<tr><td colspan=2></td></tr>';
$out .='<tr><td colspan="2">';
if ( function_exists( 'my_fav_quote_display' ) ){
$out .= my_fav_quote_display();
}
the thing is that this is a plugin of wordpress which i am modifying for my need their is misalignment of the word "message" i.e is it is appearing too low with respect to text areabox and also cols and row syntax are not working have tried applying style top ,bottom and also tried margin but the message word is not moving from the place .please click on add to quote for seeing the widget where the problem is a link
<td style="vertical-align: top;">Message</td>