PHP邮件不发送HTML邮件

时间:2017-10-11 23:17:11

标签: php html email

我的php邮件脚本正在发送邮件,但即使标题告诉它是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 :(得分:1)

你按错误的顺序做到了:

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

您还必须使用单引号"\r\n"的双引号'\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 Mail:

$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 :(得分:0)

下面:

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

将其更改为:

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

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