Solved

ob_get_clean();

Posted on 2012-03-15
3
299 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
3 Comments
 
LVL 82

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 108

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now