Sunday, January 25, 2009

Sending class object into email body using ob_* functions

It has been several times seen in my past career that developers ( mostly newbie) generally find much difficulty in sending the content of class object through email... Occasionally, it has been seen that even though we can print the object content on browser and can view the object content and the values the object is holding, it becomes sometime necessity that we get the content of object through email....

Following is the piece of code, which will definitely be helpful for your development...

// Start of code

ob_start();
$strBody="Hi, There is an Error in creating Class Object while script was in second loop. Record Id :".$records["recordDetailsId"];
print_r($objClass);
$strBody.=ob_get_contents();
ob_end_flush();
mail("foo@mycompany.com","Error in Creating Object ",$strBody,"From: My Site\n");

// End of code


So you can easily find how only three functions i.e. ob_start() , ob_get_contents() and ob_end_flush() serve the purpose ...

More details about these functions can be found at php.net

1 comment:

  1. Hi Uday,

    Yeah.. Correct you are, it can also be done with second parameter to print_r function. I was interested in explaining and giving basic introduction of buffering functions though. :)

    ReplyDelete