?
Solved

ob_get_clean();

Posted on 2012-03-15
3
Medium Priority
?
346 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 84

Accepted Solution

by:
Dave Baldwin earned 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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

Technology Partners: 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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 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