Mike
Mike

Reputation: 5788

Broken HTML-Mail because of space character

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?

Upvotes: 1

Views: 810

Answers (3)

Peter Szalay
Peter Szalay

Reputation: 386

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("[email protected]","Some subject",$message);

www.w3schools.com explanation, example

Upvotes: 2

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

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

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

Upvotes: 0

Legeran
Legeran

Reputation: 117

See this line here?

$html = '<table border="0" width="100%";>';

after width="100%" you do not need this symbol -> ; Try removing it.

Upvotes: 0

Related Questions