Sending HTML email with PHP

Go To StackoverFlow.com

0

I am trying to send an html email with PHP and it keeps coming just as text. All of the values are generated correctly from the php it is just text in the email. Here is the code:

    $to='xxxxxxx@xxxxxxx.com';
    $from = 'xxxxxxx@xxxxxxxx.com';
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers = "From: $from \r\n";
    $subject = "Print Run: " . $run . " is ordered";
    $body ="<html>
            <head>
            </head>
            <body>
            <table>
                <tr>
                    <th>Company</th>
                    <th>Quantity</th>
                    <th>Size</th>
                    <th>Date Added</th>
                    <th>
                    </th>
                </tr>";
            for($i = 0; $i < $arraySize; $i++){     
                $body .= "<tr><td>" . $companyName[$i] . "</td><td>" . $quantity[$i] . "</td><td>" . $cardSize[$i] . "</td><td>" . $dateAdded[$i] . "</td></tr>";
            }
            $body .= "<tr>
                        <td style=\"font-weight:bold; border-style:solid; border-top-width:1px;\">Totals</td>
                        <td style=\"font-weight:bold; border-style:solid; border-top-width:1px;\">" . $totals . "</td>
                        <td></td>
                        <td></td>
                        <td></td>
                        </tr>
                        </table>
                        </body>
                        </html>";

    mail($to,"Print Run: " . $run . " is ordered",$body,$headers);
2012-04-05 15:19
by shinjuo
did you check if it's not caused by your email client settings - mishu 2012-04-05 15:21


11

You overwrite the header on the last of the three lines:

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers = "From: $from \r\n";

Should be (note the dot):

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $from \r\n";
2012-04-05 15:21
by Simon Forsberg
+1 for an eagle eye - Michael Berkowski 2012-04-05 15:21
Wasn't so hard to discover it, really : - Simon Forsberg 2012-04-05 15:23
One other gotcha i've seen is the "\r\n". This will fail on some systems and should be replaced with PHP_EOL - Mr Griever 2012-04-05 15:24
Thanks for the catc - shinjuo 2012-04-05 15:24
@Matt H: Your comment is misleading, you should back that up with the RFC and PHP source-code. "\r\n" is absolutely correct, see as well Which line break in php mail header, \r\n or \n? - hakre 2012-04-05 15:43
Ads