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

时间:2013-08-23 02:39:24

标签: php html email html-email phpmailer

我设计了一个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时)。这似乎不稳定。

我在这里缺少什么?

1 个答案:

答案 0 :(得分:-1)

你可以制作一个页面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>