1

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!

4
  • 1
    This code makes no sense. ob_get_contents() returns the output that was generated (e.g., via echo or printf) since ob_start() but mail() 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? Commented Mar 8, 2022 at 20:49
  • 1
    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 bork session_start() or other headers that need to be sent before other output.
    – Progrock
    Commented Mar 8, 2022 at 20:58
  • I didn't write this code, so I have no idea. That's why I am so confused as well. I just wondered if there was an obvious (or not) reason why this code would be in here. But I appreciate the explanation of what ob captures. Is it only echo or printf statements? Commented Mar 8, 2022 at 20:59
  • @Progrock I appreciate the explanation. So there would be no purpose for it here, since mail() doesn't produce output? Commented Mar 8, 2022 at 21:02

1 Answer 1

2

If you say this in PHP:

echo 'Hello';

it will result in the string Hello being sent to the browser more or less immediately. This is fine in contexts where you're outputting stuff directly, but sometimes you want to use that workflow, but capture the output instead of sending it to the browser. So instead, you can do this:

ob_start();
echo 'Hello';
$tmp = ob_get_contents();
ob_end_clean();

This will result in nothing being sent to the browser, but $tmp will now contain Hello.

This is useful in email because what you might want to do is render a template to a string, and then use that as an email message body rather than sending it to a browser.

With PHPMailer, you might use that workflow to create a message body, and then pass it to PHPMailer:

$mail->Body = $tmp;

The example you posted is a bit strange, because the mail() function doesn't typically output anything anyway, so it will capture nothing.

1
  • 1
    Greatly appreciated, best explanation so far. I guess whoever wrote this code initially must have had some reason, but whatever it was, we'll never know. Commented Mar 8, 2022 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.