PHP发送电子邮件包含HTML

时间:2014-03-24 02:13:44

标签: php html email

我尝试了很多教程,但仍然无法正常工作。

以下是我发送电子邮件的PHP代码:

$from = "Someone <someone@example.com>";
$to = "Someone 1 <someone1@example.com>";
$subject = "Test Send Email";
$body = "<div>Test</div>";

$host = "mail.example.com";
$username = "someone";
$password = "blabla";

$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);

$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail))
{
    echo("<p>" . $mail->getMessage() . "</p>");
}
else
{
    echo("<p>Message successfully sent!</p>");
}

收到该电子邮件后,将继续显示HTML代码:

<div>Test</div>

有什么建议吗?

1 个答案:

答案 0 :(得分:6)

添加HTML标头:

$headers = array(
    'From'          => $from,
    'To'            => $to,
    'Subject'       => $subject,
    'Content-Type'  => 'text/html; charset=UTF-8'
);