Adding HTML to $_POST elements in email

I have read through a lot of threads and have found a great deal of information but still having trouble. I want to have a confirmation email sent to people come up in HTML format. I have no problem making regular text come up with HTML attributes but am having no luck with information being retrieved from the Form.

I want the following to have html charteristics:
$message .="First Name: " .$_POST[‘firstname’]

You guys have never let me down. Thanks in Advance.
-wbarrett26

I’m not sure i’m clear here, what is it you need to be formatted in HTML?

Whate exactly are you referring to as “html characteristics”?

Just a stab at it, try defining another variable first.


$firstname = $_POST['firstname'];

$message = "First Name: " . $firstname;

a user will fill out a form putting in thier name and other general info.
a confirmation email is sent to them with the info they submitted.
I can get the general text to come up with html, ie. color size changes, however the fields that are retrieved from the form are only coming up a plain text. I have used a few examples from the forum and other sites as a guide but had no luck.

original confirmation code


function mailuser() {
$to=$_POST['email'];
$subject="Acknowledgement";
$message ="This email confirms your registration."."
";
$message .=""."
";
$message .="First Name: " .$_POST['firstname'] ."
";
$message .="Last Name: " .$_POST['lastname']."
";
$message .="Email Address: " .$_POST['email']."
";
$message .=""."
";
$message .="If there are any errors, please go back to the registration site and re-submit your information."."
";
$headers = "From:". "Registrar";
$headers .= "<registrar@site.com>" ."
";
mail($to, $subject, $message, $headers);
}
?>

new confirmation email, not complete


function mailuser() {
$to=$_POST['email'];
$subject="Acknowledgement";
$message = <<<EOF
<html>
	<body bgcolor="#FFFF99" align="center">
        <font face="Arial, Helvetica, sans-serif" color="#000099">
	This email confirms your registration.<br>
	</font>
	</body>
</html>
EOF;
$message .="Last Name: " .$_POST['lastname']."
";
$message .="Email Address: " .$_POST['email']."
";
$message .=""."
";
$message .="If there are any errors, please go back to the registration site and re-submit your information."."
";
$headers = "From:". "Registrar";
$headers .= "<registrar@site.com>" ."
";
$headers .= "Content-type: text/html
";
mail($to, $subject, $message, $headers);
}
?>

I would like a way that i could make those POST elements to be in a format other then plain text, either in color or a different font style but when i try to add html code either the POST items would disappear or just not take on HTML styles

You have to designate the mime type of the e-mail as html

even if i use the following i still have a problem
"MIME-Version: 1.0
" .
“Content-type: text/html; charset=iso-8859-1”);

have ytou tried simplifying it an just making the entire e-mail and header a string?

Just a guess here, but you have your HTML in the $message variable, which when you originally set it has the opening and closing HTML tags. You then append to the end of that string all the information you want from $_POST. Try putting that infomation inside the start and end HTML tag:


function mailuser() {
$to=$_POST['email'];
$subject="Acknowledgement";
$message = <<<EOF
<html>
    <body bgcolor="#FFFF99" align="center">
        <font face="Arial, Helvetica, sans-serif" color="#000099">
    This email confirms your registration.<br>
EOF;

$message .="Last Name: " .$_POST['lastname']."
";
$message .="Email Address: " .$_POST['email']."
";
$message .=""."
";
$message .="If there are any errors, please go back to the registration site and re-submit your information."."
";

$message .= "
    </font>
    </body>
 </html>";


$headers = "From:". "Registrar";
$headers .= "<registrar@site.com>" ."
";
$headers .= "Content-type: text/html
";
mail($to, $subject, $message, $headers);
}
?>

BTW - I didn’t check the code, you may need to change the "
" to <br> if the "
" is inside the HTML code.

when i do that it wont retrieve the variables form the form. I thought someone might of know how to do it with a command like print


print("<div align=\"center\" class=\"style12\">First Name</div>");

have ytou tried simplifying it an just making the entire e-mail and header a string?

I agree, sometimes that makes it work. Not sure why.

As Ankou already posted in here – your hangin your message at the end of that html block.

Did you try something like this?

<?
mailuser($_POST['lastname'],$_POST['email']);
function mailuser($lastName,$to) { 
$myEmail = "registrar@site.com";
$myName = "myWebpageName";
$subject= "Acknowledgement"; 
$author = "Last Name: " .$lastName; 
$authorEmail ="Email Address: " .$to; 
$myMessage ="If there are any errors, please go back to the registration site and re-submit your information."; 
$headers = "From: ".$myName."<".$myEmail.">
"; 
$headers .= "MIME-Version: 1.0
"; 
$headers .= "Content-type: text/html
"; 
$message = <<<EOF
<html>
<body bgcolor="#FFFF99">
<p align="center">
<font face="Arial, Helvetica, sans-serif" color="#000099">
<i>This email confirms your registration.</i><br>
$author<br><u>$authorEmail</u><br><br><b>$myMessage</b>
</font>
</p>
</body>
</html>
EOF;
mail($to, $subject, $message, $headers); 
} 
?>

I hadn’t noticed the mail() function was inside a function, probably best practice to actually pass the data to your function like Voccart has shown.

Depending on a number of factors you may be able to call $_POST[] inside a function and get the value stored in the variable. Depends on the set up of your server and registered globals/superglobals (one or both, I forget at the moment :shifty:).