I've read the PHP documentation, but it fails to give any practical examples.
I'm using some open source code, and it contains this email function. I'm trying to understand why these ob_XXX()
functions are here and what they do (because I need to use PHPMailer instead of mail()). When I print the return value before the return, it's always empty.
// Send using PHP mail() function
ob_start();
mail($to,$subject,$message,$headers);
$tmp = trim(ob_get_contents());
ob_end_clean();
return (strlen($tmp)) ? $tmp : true;
I understand that it's something to do with output buffering, and while I have some idea of what buffering is, I'm not sure why it is being used here. Thanks!
ob_get_contents()
returns the output that was generated (e.g., viaecho
orprintf
) sinceob_start()
butmail()
doesn't generate any output. It's unclear what you're trying to capture here, as you've already got the contents of the email in$message
. What are you expecting it to capture in$tmp
?ob_start(); include 'foo.php'; $captured = ob_get_clean();
is commonly used for things like views. As an example of output buffering use. Or to capture erroneous/accidental output that may borksession_start()
or other headers that need to be sent before other output.mail()
doesn't produce output?