PHP错误日志HTML电子邮件标题

时间:2014-03-05 12:09:44

标签: php html email error-handling

快速的问题。 我为我正在制作的应用程序设置了一个自定义错误处理程序,写入错误日志,并使用下面的代码生成一个html电子邮件给主管理员。

function log_to_errors($number, $message, $file, $line, $vars){

if(ENVIRONMENT !== 'development'):
    ini_set('log_errors', 1);
    ini_set('error_log', '../error_logs.txt');

    $email = "
        <div style='background: rgb(224, 224, 224); padding: 5px 10px;'>
            <p>An error reference of ".$number." has occured on line: ".$line." in: <strong>".$file."</strong></p>
            <p>".$message."</p>
            <pre>".print_r($vars, 1)."</pre>
        </div>
    ";

    $email_header = 'Content-type: text/html; charset=iso-8859-1;';
    error_log($email, 1, ADMIN_EMAIL, $email_header); 
endif;
}

set_error_handler('log_to_errors');

我想修改这一行:

$email_header = 'Content-type: text/html; charset=iso-8859-1;'

这样我就可以设置其他电子邮件凭据的自定义信息,即电子邮件中的“来自和主题”元素,但是当我尝试执行以下操作时:

$email_header = 'From:errors@example.com; Content-type: text/html; charset=iso-8859-1;';

它打破了电子邮件中的html。 有人能指出我正确的方向,所以我可以为电子邮件设置一个消息主题等。

非常感谢, 路易斯

2 个答案:

答案 0 :(得分:0)

您在dot (.)之前错过$email_header ..

试试这个..

$email_header = 'Content-type: text/html; charset=iso-8859-1;'
$email_header .= 'From:errors@example.com; Content-type: text/html; charset=iso-8859-1;';

你不是concating $email_header字符串..

答案 1 :(得分:0)

    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . chr(13) . chr(10);
    $headers .= 'Content-type: text/html; charset=utf-8' . chr(13) . chr(10);
    $headers .= 'Content-Transfer-Encoding: 8bit' . chr(13) . chr(10);

    // Additional headers
    $headers .= 'To: '. $to . chr(13) . chr(10);
    $headers .= 'From: '. $from . chr(13) . chr(10); 
    $headers .= 'Reply-To: '. $clientmail . chr(13) . chr(10) . chr(13) . chr(10);