首页 文章

PHP邮件不发送HTML邮件[重复]

提问于
浏览
0

这个问题在这里已有答案:

我的php邮件脚本正在发送邮件,但即使 Headers 告诉它是html,它也会忽略它并将邮件作为文本发送 . 所有'\ r \ n'中断最终被忽略,$ header内容粘贴在电子邮件中的$ body下方 . 下面是代码 .

//mail out copy
$to = $email;
$subject = 'Thank you for signing up to Learn to Play';
$headers = 'From: donotreply@xxx.com' . '\r\n';

Always set content-type when sending HTML email
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

$body = 'Thank you for registering with us for your Learn to Play Table Games Lesson. Your registration has been confirmed for ';
$body .= $date_requested;
$body .= ' for a group of '; 
$body .= $guests;

$body .= '.  Please check in at the Guest Services Desk at 6:45PM where a member of our Table Games Team will meet you for your session. ';
$body .= 'Should there be any conflict with your reservation a member of our team will contact you to re-schedule your lesson.';

mail($to, $subject, $headers, $body) or die('Error sending mail');

}

2 回答

  • 0

    你按错误的顺序做到了:

    mail($to, $subject, $body, $headers) or die('Error sending mail');

    您还必须使用双引号 "\r\n" instate of single quotes '\r\n' .

    示例

    用于文本邮件:

    $subject = 'Thank you for signing up to Learn to Play';
    
    $body    = 'Thank you for registering with us for your Learn to Play Table Games Lesson. Your registration has been confirmed for '
             . $date_requested
             . ' for a group of '
             . $guests
             . '.  Please check in at the Guest Services Desk at 6:45PM where a member of our Table Games Team will meet you for your session. '
             . 'Should there be any conflict with your reservation a member of our team will contact you to re-schedule your lesson.'
    ;
    
    $headers = "From: donotreply@example.com\r\n"
             . "Reply-To: donotreply@example.com\r\n"
             . 'X-Mailer: PHP/' . phpversion()
    ;
    
    mail($email, $subject, $body, $headers) or die('Error sending mail');
    

    对于HTML邮件:

    $subject = 'Thank you for signing up to Learn to Play';
    
    $body    = 
    '<html>
        <head>
            <title>' . $subject . '</title>
        </head> 
        <body>
            <h1>Thank you for registering with us for your Learn to Play Table Games Lesson.</h1>
            <p>Your registration has been confirmed for '
            . $date_requested
            . ' for a group of '
            . $guest
            . '. Please check in at the Guest Services Desk at 6:45PM where a member of our Table Games Team will meet you for your session. Should there be any conflict with your reservation a member of our team will contact you to re-schedule your lesson.
            </p>
        </body>
    </html>'
    ;
    
    $headers = "MIME-Version: 1.0\r\n"
             . "Content-type: text/html; charset=iso-8859-1\r\n"
             . "From: donotreply@example.com\r\n"
             . "Reply-To: donotreply@example.com\r\n"
             . 'X-Mailer: PHP/' . phpversion()
    ;
    
    mail($email, $subject, $body, $headers) or die('Error sending mail');
    

    http://php.net/manual/en/function.mail.php

  • 1

    这里:

    mail($to, $subject, $headers, $body) or die('Error sending mail');
    

    改为:

    mail($to, $subject, $body, $headers) or die('Error sending mail');
    

    因为 $headers 应该是最后一个参数 .

相关问题