March 19, 2012
HTML mail displayed wrong
Question by Kichu
When I send html message through an mail sending it will break the design in mail account. The code is below:
<?php
$center_part2 = "";
$mail_ID = "exmaple.gmail.com";
$subject = "UnTouched Ticket Notification";
$headers = "From: example.comrn";
$headers .= "MIME-Version: 1.0rn";
$headers .= "Content-Type: text/html; charset=ISO-8859-1rn";
$part = '<html><body>';
$part .= '<div style="background-color: #EEEEEE;border-bottom: 1px solid #DCDCDC;padding: 10px 10px 20px;width: 500px; float:left;font-size:12px;font-weight:bold;">';
$part1 = '<div style="width:500px;">';
$part1 .= '<div style="width:500px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc;">';
$part1 .= '<div style="width:450px;background-color:#eeeeee;padding: 10px 0 20px;border-bottom:1px solid #dcdcdc; height:auto; float:left;font-size:12px;margin-left:50px; font-weight:bold;">';
$part3 = '</div></div></div>';
$part3 .= '</body></html>';
$center_part = 'You have opened '.$inr.' tickets . But the following tickets are not updated yet ,';
$center_part2.="<div style='width:345px;color:#000000;font-size:12px;margin-left:75px; font-weight:bold; height:auto; margin-top:10px;'>";
for ($k=1; $k<=70; $k++){
$new.="<p>Ticket #".$K." Last Updated ".$K." days ".$K." hours ago !</p>";
}
$center_part2.=$new."</div>";
if($mail_ID){
$message = "";
$center_part1 = 'Dear Admin ,';
$message = $part.$center_part1.$part1.$center_part.$center_part2.$part3;
mail($mail_ID, $subject, $message, $headers);
}
?>
When I opened the gmail I get the mail like this:
So what is the problem in my code?
Answer by MrCode
I’ll post my comment as an answer as davidethell suggested, in case anyone else lands here with the same problem.
The display issue was due to the <div>
tags having float:left
and a fixed width, causing them to become stacked side by side where the width permitted.
The !
problem where random !
are inserted into the email content is due to exceeding the max characters per line. Keeping each line under 512 characters will resolve this. Use a n
to break up the content as required.
Answer by Starx
You have not closed of <div>
of the following code.
$part .= '<div style="background-color: #EEEEEE;border-bottom: 1px solid #DCDCDC;padding: 10px 10px 20px;width: 500px; float:left;font-size:12px;font-weight:bold;">';