使用PHP发送HTML电子邮件?

时间:2014-11-07 18:06:15

标签: php email

我尝试使用PHP发送HTML电子邮件。身体目前看起来像这样:

$body = "Hello," . "<br />" .  "<br />" . "We sent you an e-mail to confirm your account has been "
. "created." . "<br />" . "<br />" . "Thank you!";

这会正确分开线条吗?当我声明我的标题时,我是否还需要使用相同的格式来换行?

    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";     
    $headers .= "From: donotreply@webmaster.net";

我是否必须将每个标题末尾的新行从"\r\n"切换为"<br />"

1 个答案:

答案 0 :(得分:1)

使用库更好,但有想法试试这个:

function    sendemail( $to, $from, $subject, $title, $html, $cc = "", $bcc = "" )
    {
    $message  = "\n";
    $message .= "<!doctype html><html><head><title>$title</title></head><body>";
    $message .= $html;
    $message .= "</body></html>";

    $headers  = "MIME-Version: 1.0 \r\n";
    $headers .= "Content-type: text/html; charset=utf-8 \r\n";
    $headers .= "From: $from \r\n";
    $headers .= "Reply-To: $from \r\n";
    $headers .= ( $cc != "" ) ? "Cc: $bcc \r\n" : "";
    $headers .= ( $bcc != "" ) ? "Bcc: $bcc \r\n" : "";

    return mail( $to, $subject, $message, $headers, "" );
    }