PHP邮件html格式不起作用

问题描述:

使用php,并且通过此代码,我收到电子邮件作为纯文本,我错过了什么吗?因为我需要发送可能包含链接的格式化电子邮件。PHP邮件html格式不起作用

$to = "[email protected]"; 
$subject = "Password Recovery"; 

$body = ' 
<html> 
    <head> 
     <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
     <title>Birthday Reminders for August</title> 
    </head> 
    <body> 
     <p>Here are the birthdays upcoming in August!</p> 
     <table> 
      <tr> 
       <th>Person</th><th>Day</th><th>Month</th><th>Year</th> 
      </tr> 
      <tr> 
       <td>Joe</td><td>3rd</td><td>August</td><td>1970</td> 
      </tr> 
      <tr> 
       <td>Sally</td><td>17th</td><td>August</td><td>1973</td> 
      </tr> 
     </table> 
    </body> 
</html> 
'; 

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers = "From: [email protected]\r\n"."X-Mailer: php"; 
if (mail($to, $subject, $body, $headers)) 
echo "Password recovery instructions been sent to your email<br>"; 

看看这个例子,这是足以发送邮件的php:

<?php 
    //change this to your email. 
    $to = "[email protected]"; 
    $from = "[email protected]"; 
    $subject = "Hello! This is HTML email"; 

    //begin of HTML message 
    $message =" 
<html> 
    <body> 
    <p style=\"text-align:center;height:100px;background-color:#abc;border:1px solid #456;border-radius:3px;padding:10px;\"> 
     <b>I am receiving HTML email</b> 
     <br/><br/><br/><a style=\"text-decoration:none;color:#246;\" href=\"www.example.com\">example</a> 
    </p> 
    <br/><br/>Now you Can send HTML Email 
    </body> 
</html>"; 
    //end of message 
    $headers = "From: $from\r\n"; 
    $headers .= "Content-type: text/html\r\n"; 

    //options to send to cc+bcc 
    //$headers .= "Cc: [email][email protected][/email]"; 
    //$headers .= "Bcc: [email][email protected][/email]"; 

    // now lets send the email. 
    mail($to, $subject, $message, $headers); 

    echo "Message has been sent....!"; 
?> 
+3

请将其标记为答案或upvote'它是否正确'。 – GLES 2012-08-16 20:02:23

你重新设置你的头:

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers = "From: [email protected]\r\n"."X-Mailer: php"; 

你错过了在最后一行点,这是在写作前两个:

$headers = 'MIME-Version: 1.0' . "\r\n"; 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
$headers .= "From: [email protected]\r\n"."X-Mailer: php"; 
+0

...好赶上... – j08691 2012-08-16 20:01:13

+0

@andrewsi谢谢我也有类似的问题与失踪。 – Muk 2013-12-27 06:50:07

我发现与特定的邮件服务器相同的问题,在我的情况下,解决方案是设置“\ n”而不是“\ r \ n”作为标题的行尾。