Treat php variable as html

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Treat php variable as html

Post by Pavilion »

Hello Folks:

I've a fairly straight-forward input where users can enter data in html (bolding, font-size, inserting links, etc...).

After entering data, the user submits to save the data and send the contents to coworkers in the form of email. The email portion is working fine, except for the content. The content is not being passed as html. Following is my current attempt at getting php to pass the $body variable as html:

Code: Select all

		//Send Confirmation email. Following are the variables for the email 
		$sendto = $email; // this is the email address collected from the foreach routine. 
		$e_subject = $subject; // Subject 
		$message = "<html>'" . $body . "'</html>"; //There are single quotes within the double-quotes, but variable is still not passing as html.

// the following block is commented out. I left it in so you can see another approach I've taken to treat $body as html.
		/*$message = " 
			<html>
			<body>
			<p>Hello " . $participant . ":</p>
			<div>" . $body . "</div>
			</body>
			</html>
			";	 */	
		// Always set content-type when sending HTML email
		$header = "MIME-Version: 1.0" . "\r\n";
		$header .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

		$header .= "From: " . $user_email . "\r\n"; 
		// Collect variables from above and insert into the mail() function. 
		mail($sendto, $e_subject, $message, $header);
Whatever I try to pass $body in the email as html fails. Links don't work because php has inserted slashes, formatting is not going through correctly, etc...

What syntax do I have to use to force php to pass variable content as html?

Thanks Much - Pavilion
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Treat php variable as html

Post by social_experiment »

Pavilion wrote:Links don't work because php has inserted slashes
Sounds like magic_quotes is on here; you can use stripslashes() to remove those;

The snippet you pasted is similar to what i use and the only difference i can see is the header which defines the content type; I'm not sure if the spaces are important in it but here it is anyway

Code: Select all

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Treat php variable as html

Post by Pavilion »

social_experiment wrote:
Pavilion wrote:Links don't work because php has inserted slashes
Sounds like magic_quotes is on here; you can use stripslashes() to remove those;

The snippet you pasted is similar to what i use and the only difference i can see is the header which defines the content type; I'm not sure if the spaces are important in it but here it is anyway

Code: Select all

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Thank you so much Social experiment:

My emails are flying through with all formatting intact. I really appreciate your advice. Now I have one other question.

In my testing, I've purposely sent emails to addresses that don't exist, even though my header designates the user's email address as the "from",

Code: Select all

$header .= "From: " . $user_email . "\r\n";
I'm not getting any pings about bad email addresses from the receiving server:

Is there anything I can do about this. My clients email to unused addresses often enough that this is an issue.

Thanks much:

Pavilion
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Treat php variable as html

Post by social_experiment »

Pavilion wrote:I'm not getting any pings about bad email addresses from the receiving server:
I'm afraid i wouldn't be of much assistance here, my knowledge of this subject is very limited, closer to non-existant really
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Treat php variable as html

Post by Celauran »

I don't know that there's much you can do about it at the PHP level. If the receiving mail server cannot find the recipient, it can either bounce the email back to the sender, or forward it to a catch-all email address. Neither of these would interact with your PHP app.
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Treat php variable as html

Post by Pavilion »

Celauran wrote:I don't know that there's much you can do about it at the PHP level. If the receiving mail server cannot find the recipient, it can either bounce the email back to the sender, or forward it to a catch-all email address. Neither of these would interact with your PHP app.
Under the heading of, "A Little bit of research goes a long way", I found this:
How can I get emails that bounce, when using the PHP mail() function, to go to a particular email address?

When sending email messages using PHP, you must set the envelope sender in order to have bounces handled correctly. In order to set the envelope sender, use the -f sendmail option in the additional_parameters parameter of the mail command. Here is an example:

mail("to@example.com", "subject", "body of message", "From: sender@example.com", "-fsender@example.com");

Without the last parameter, "-fsender@example.com", bounces would come back to a system administrator account, instead of the From address, which is usually not what is intended.

Refer to the PHP documentation for more info:
http://php.lamphost.net/manual/en/function.mail.php
OK - so the sample string from above is as follows:
mail("to@example.com", "subject", "body of message", "From: sender@example.com", "-fsender@example.com");
I need to get the last (-f) parameter to pick up my user email address (so that bounces go to the user who sends the email). So... I structured my mail() as follows:

Code: Select all

mail($sendto, $e_subject, $message, $header, '-f' . $user_email); 
This mail() works great as far as sending goes, but I'm not getting a bounce back. For my invalid email address I'm using duck@mybusinesssite.com (replacing with businesssite being the actual url to my own business site. So I know there is no duck account. But... in sending an email to Donald Duck (and other valid addresses) the valid emails go through, there is no duck@ account and I'm not getting a "bouncing duck" if you get my drift. :D

Am I not combining -f with the $user_email correctly?

Thanks Much:

Pavilion
Pavilion
Forum Contributor
Posts: 301
Joined: Thu Feb 23, 2012 6:51 am

Re: Treat php variable as html

Post by Pavilion »

More information on generating invalid email address bounces can be found here
Additionally, with many PHP installations with Apache servers that have a default sendmail or postfix configurations, the way that mail() and PHP communication with the local MTA causes the SMTP "envelope sender" to usually be apache@hostname - where hostname is the actual DNS server name that sits behind any given virtual host. The "envelope sender" is the address listed in the "Return-Path:" header - and controls where the email is sent to in the event that a recipient address bounces.

The implication for both the From: and for having a valid Return-Path is twofold: 1) if the mail bounces, or otherwise has a delivery error, you'll never know about it (the return code from the mail() function is only an indication that PHP handed it off to the local MTA) and 2) it clogs up the mailboxes that are used by your server administrators for directing the postmaster mail for the servers.

Using the -f Mail parameter

Because of this "envelope sender" issue, perhaps the most important best practice in this code snippet is the addition of the $mailParams part of the mail() function.

This addition forces PHP to set the "envelope sender" (or the Return-Path: header) to the address you specify.
Now I just have to get it to work??? :banghead:

Pavilion
Post Reply