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...

 

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

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

 

 
Note : The whole o/p buffering stuff can be replaced with the following single line.

$strBody.= print_r($objClass, true);

i.e. with second parameter to print_r function.

However, I am more interested in achieving it with basic buffering functions. 

 

Comments

Hi Sandip,

The whole o/p buffering stuff can be replaced with the following single line.

$strBody.= print_r($objClass, true);

Thanks,
Uday

Add new comment