从PHP发送HTML电子邮件-结果不稳定


Sending HTML Email From PHP - Results Erratic

我设计了一个HTML页面,然后将其转换为在PHP中使用,以便发送HTML电子邮件。

$message = '<!DOCTYPE html>';
$message .= '<html>';
$message .= '<body bgcolor="#E8E8E8 ">';
$message .= '<table bgcolor="white" >';
$message .= '<tr>';
$message .= '<td style="font-family:''Helvetica Neue'',Helvetica,Arial,sans-serif;">';
$message .= '<img src="#" width="200px">';
$message .= 'This is a test page.';
$message .= '</td>';
$message .= '</tr>';
$message .= '</table>';
$message .= '</body>';
$message .= '</html>';
$to = "you@example.com";
$subject = "Pulling my hair out";
$headers = "From: me@example.com";
$headers .= "MIME-Version: 1.0'r'n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1'r'n";
mail($to,$subject,$message,$headers); 

尽管它作为一个独立的html页面看起来很完美(我甚至制作了一个与$message数组相呼应的测试php页面,它看起来仍然很完美),但它在电子邮件中(发送后)会出现奇怪的问题。

有时会有一个随机!在文本的中间。有时标签中的样式不会显示在电子邮件中(当我"检查"电子邮件的html时)。这似乎不稳定。

我在这里错过了什么?

您可以制作一页emailresetTemplate.php在这一页写下这些行的

<?php ob_start();?>
//do your html stuff here ....... Example Below..........
<div style="width:698px; margin:0 auto; position:relative;">
    <div>
        <div style="background:url(<?php echo ABSOLUTE_PATH; ?>images/email/restpass/header.png) no-repeat; width:680px; height:127px; margin:0 0 0 10px;"></div>
    </div>
    <?php    
        $contents = ob_get_contents();
        ob_clean();
        include("emailresetTemplate.php");
        $to = $email;
        $subject = 'Your Password Reset Request';
        $message = $contents;
        $headers = 'MIME-Version: 1.0' . "'r'n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
        $headers .= 'From: email@example.com' . "'r'n" .
                'Reply-To: email@example.com' . "'r'n" .
                'X-Mailer: PHP/' . phpversion();
        if(@mail($to, $subject, $message, $headers)){
            print "An email containing the password has been sent to you at " . $row["eMail"];
        } else {
            echo("No such login in the system. please try again.");
        }
   ?>
</div>