简体   繁体   English

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

[英]Sending HTML Email From PHP - Results Erratic

I designed an HTML page and then converted it to use in PHP in order to send an HTML email. 我设计了一个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); 

Even though it looks perfect as a stand-alone html page (and I even made a test php page that echoes the $message array, and it still looks perfect) it will have weird things wrong with it in the email (after it's sent). 即使它作为独立的html页面看起来很完美(我什至制作了一个测试PHP页面,都与$ message数组相呼应,并且看起来仍然很完美),但在电子邮件中(发送后)它会有一些奇怪的问题。

Sometimes there will be a random ! 有时候会有随机的! in the middle of the text. 在文本中间。 Sometimes the styling in a tag will not show up in the email (when I 'inspect' the html of the email). 有时,标记中的样式不会显示在电子邮件中(当我“检查”电子邮件的html时)。 It seems erratic. 似乎不固定。

What am I missing here? 我在这里想念什么?

You can make one page emailresetTemplate.php In this page write these line's 您可以制作一页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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM