0

I have a php script containing a function which when run gives output. I am using that output as output buffer by using ob_start and ob_get_clean functions and emailing that output which contains some html as a report. Problem is, email doesn't first few lines of output, tried many times but every time first few lines are missing.

How to resolve this?

<?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);
?>

Email Output:

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.

Original Code is very long, this is its small clone!

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
g13
  • 139
  • 4
  • 11

1 Answers1

1

Are you using ob_get_contents(); also?

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

Edit:

Example test script, test this and see, note you should always validate user input else you could be a spam host in the future, the example has no validation.

<?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';
}
?>

Hope it helps

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
  • offtopic ... could you tell me how to write profile name like yours ? that is awesome – NullPoiиteя May 11 '13 at 14:56
  • 1
    @NullPointer by using the utf8 Spacing modifier letters, N̨ul̕L͑P̯͍̭ȏͣ͛iƞ7ę̴̽̓ͯ̿̃̈͊ͨͥ̒̀͏͓͈̟͇̻̯̦̟̝̳̟̩̘̩͉̯͇͈̖r̷͕̜̘̗̀ͨ̏͌̚̚l‌​̭̹͖̺͈̞̖̩̰̱͇̖͗ͫ͐̇ͦ́̎̒ͪ̾ͫ̃̏ͮ̓̀͝ – Lawrence Cherone May 11 '13 at 15:00
  • @user2299278, you are not using `ob_get_contents()` as lawrence suggested. What are your results when you use that function? – Patrick Kostjens May 11 '13 at 15:04
  • just tested this with what you said, it gave same results!! – g13 May 11 '13 at 15:08
  • @l̕aͨŵƦȆ̴̟̟͙̞ͩ͌͝ƞCͭ̏ȇƇhƐȓ0nè above is exactly what I'm using! – g13 May 11 '13 at 15:11
  • @l̕aͨŵƦȆ̴̟̟͙̞ͩ͌͝ƞCͭ̏ȇƇhƐȓ0nè this is working fine, without missing anything! – g13 May 11 '13 at 15:32
  • Make sure you have a doctype on the email, just as if it is a valid html page, tho prob not the problem, it might be the shortcut ob_get_clean() function. not 100% lol could be anything really, also dont forget the parenthesis on function `mailasreport()` and the concatenating of `$headers` before it set, tho prob a typo – Lawrence Cherone May 11 '13 at 15:36
  • @l̕aͨŵƦȆ̴̟̟͙̞ͩ͌͝ƞCͭ̏ȇƇhƐȓ0nè , I worked your way but its not working as intended, it is still missing those lines – g13 May 11 '13 at 16:13