Solved

ob_get_clean();

Posted on 2012-03-15
3
325 Views
Last Modified: 2012-03-18
The code attached below works. An html email message is sent in the function called display_email(); using this code:
ob_start();
display_email();
$msg .= ob_get_clean();

I have constructed the actual message in a function as the email contained quite a bit of dynamic informaion and it seemed to be the only way I could get the email to send its content.

My question relates to an errror I recieve in de bug mode that states that the variable $msg is undefined. My code works, everying seems to function just fine hower I would preref to figure out what the undefined $msg variabke is all about. Especially as the $msg is there when it pop out as an email message.
Thanks,
N
$headers  = 'MIME-Version: 1.0' . "\r\n";
				$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
				$headers .= "Reply-To: ".$_POST['fp']['Contact Email Address'][0]." \r\n";
				$headers .= "From: ".$_POST['fp']['Contact Email Address'][0]."\r\n";
				
           // Send
                $subject = $_POST['subject'];
           		$to      = "someemail@somedoman.com" ;
			
		
ob_start(); 
display_responce($_POST['heading'], $_POST['subject'], $_POST['fp'], $_POST['sf']);
$msg .= ob_get_clean(); 
 
        mail($to, $subject, $msg, $headers);

Open in new window

0
Comment
Question by:Neil_Bradley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 37727985
$msg .= ob_get_clean();

That uses the '.' syntax for concatenating something onto a string.  Just put $msg = ""; near the beginning of your page and then it will be defined.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37728666
Unlike PHP's habit of creating an array when you use the "append" notation, PHP does not create a string when you use the "append" notation. Instead it raises a Notice when you attempt to append to an undefined string, then it creates the string using the content you asked for.  It is an annoying inconsistency in the language.  You can predefine your string variables like DaveBaldwin suggested (and that is my preferred way to handle this situation).  You can suppress the Notice with this.

error_reporting(E_ALL ^ E_NOTICE);

That is the default PHP error reporting setting.  I prefer to see the Notice messages because it usually means I have a typographical error!
// WORKS TO ADD A NEW ELEMENT TO THE END OF $x
// CREATES THE ARRAY $x IF IT DOES NOT ALREADY EXIST
// GIVES NO NOTICE THAT IT CREATED AN UNDEFINED VARIABLE
$x[] = 'Hello';

// WORKS TO ADD A STRING TO THE "END" OF VARIABLE $y
// CREATES THE STRING $y IF IT DOES NOT ALREADY EXIST
// RAISES A NOTICE ABOUT AN UNDEFINED VARIABLE
$y .= 'Hello';

Open in new window

0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 37735331
Thanks  Dave an Ray, between you, you have solved and answered my question thoroughly. I am pleased to resolve the $msg .= ob_get_clean(); issue as its the only way I have found to send an email containing code other than simple echoed  strings.
Cheers,
N
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question