0

我有一个 php 脚本,其中包含一个在运行时给出输出的函数。我通过使用 ob_start 和 ob_get_clean 函数将该输出用作输出缓冲区,并通过电子邮件发送包含一些 html 作为报告的输出。问题是,电子邮件没有前几行输出,尝试了很多次,但每次都缺少前几行。

如何解决这个问题?

<?php
function abc{
echo 'Server Time: '.date('l jS \of F Y h:i:s A').'<br>';
echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit. <br>
Vestibulum semper purus ut felis interdum at tempus turpis imperdiet. <br> 
Donec varius accumsan magna nec blandit. <br>
Donec a pellentesque ligula. Curabitur accumsan mauris non velit vehicula sodales. <br>
Phasellus vestibulum vestibulum nunc ut lobortis. Ut eu rutrum orci. Phasellus tempor eleifend congue. <br>
In mollis porta arcu. Integer congue fringilla lorem.";
}

function mailasreport($message) {
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail('example@hotmail.com', $subject, $message, $headers);
}
ob_start();
abc();
$report = ob_get_clean();
mailasreport($report);
?>

电子邮件输出:

Donec varius accumsan magna nec blandit.
Donec a pellentesque ligula. Curabitur accumsan mauris non velit vehicula sodales. 
Phasellus vestibulum vestibulum nunc ut lobortis. Ut eu rutrum orci. Phasellus tempor eleifend congue. 
In mollis porta arcu. Integer congue fringilla lorem.

原码很长,这是它的小克隆!

4

1 回答 1

1

ob_get_contents();也在用吗?

ob_start();
//do your stuff
...
$buffer = ob_get_contents();
ob_end_clean();

编辑:

示例测试脚本,对此进行测试并查看,注意您应该始终验证用户输入,否则您将来可能成为垃圾邮件主机,该示例没有验证。

<?php
function makeMail(){
    return '
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>

<body>
<h1>Time: '.date("l jS \of F Y h:i:s A").'</h1>
<ol>
    <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
    <li>Vestibulum semper purus ut felis interdum at tempus turpis imperdiet.</li>
    <li>Donec varius accumsan magna nec blandit.</li>
</ol>
<h3 style="color:red;">
            Donec a pellentesque ligula. Curabitur accumsan mauris non velit vehicula sodales.<br />
            Phasellus vestibulum vestibulum nunc ut lobortis. Ut eu rutrum orci. Phasellus tempor eleifend congue.<br />
            In mollis porta arcu. Integer congue fringilla lorem.</h3>
<p>yada ;p</p>
</body>
</html>';
}

/**
 * Send email
 */
function sendMail($to, $from, $subject, $content){
    $headers ='MIME-Version: 1.0'."\r\n";
    $headers.='Content-type: text/html; charset=utf8'."\r\n";
    $headers.='From:<'.$from.'>'."\r\n";
    $headers.="X-Mailer: PHP"."\r\n";
    return @mail($to, $subject, $content, $headers) ? true : false;
}

ob_start();
//do your stuff
echo makeMail();
$email_contents = ob_get_contents();
ob_end_clean();

if(sendMail('to@example.com', 'from@examlpe.com', 'Email Subject', $email_contents)){
    //sent
    echo 'sent';
}else{
    //fail
    echo 'fail';
}
?>

希望能帮助到你

于 2013-05-11T14:52:33.107 回答