php
  • email
  • 2012-08-16 82 views 7 likes 
    7

    使用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>"; 
    

    回答

    10

    看看這個例子,這是足以發送郵件的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

    18

    你重新設置你的頭:

    $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

    2

    我發現與特定的郵件服務器相同的問題,在我的情況下,解決方案是設置「\ n」而不是「\ r \ n」作爲標題的行尾。

    相關問題