Solved

ob_get_clean();

Posted on 2012-03-15
3
317 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 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 109

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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 …

789 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