homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Send html email
body, form, php,

Msg#: 4599673 posted 9:23 am on Aug 6, 2013 (gmt 0)

Can Anyone explain how to code the following.
After submitting a form, the user recieves an email confirmation with the data entered. I would like to spike up the mail with some layout features. I assume I will have to give the email body some data to accomplish this. I just don't know where to put this in my code and how exactly to write this. My form code is the following.

// Set email variables
$email = $_POST['email'];
$email_to = 'xxxxxxxxxxxxxx@xxxxxxxxxxxxx.xxxxxx';
$email_subject = 'Inschrijving EHBO cursus';
$email_subject_user = 'U bent ingeschreven voor de cursus EHBO en reanimatie aan kinderen';

// Set required fields
$required_fields = array('naam','email','emailControle','telefoon','algemene_voorwaarden');

// set error messages
$error_messages = array(

'naam' => 'Geef uw naam op svp!',
'email' => 'Geef een geldig email op!',
'emailControle' => 'Uw email adres komt niet overeen!',
'telefoon' => 'Geef uw telefoon nummer op svp!',
'algemene_voorwaarden' => 'U moet accoord gaan met de algemene voorwaarden.'

// Set form status
$form_complete = FALSE;

// configure validation array
$validation = array();

// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));

// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);

// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);

// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);

//Eigen opbouw van email check
if ($field == 'emailControle') if($_POST[$field] != $_POST['email']) array_push($validation, $field);
//einde eigen opbouw email check

//telefoon check
if (!ctype_digit($_POST['telefoon']) || strlen($_POST['telefoon']) != 10) array_push($validation, $field);

//algemene voorwaarden check
if ($field == 'algemene_voorwaarden') if (!isset ($_POST['algemene_voorwaarden'])) array_push($validation, $field);

// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'Inschrijving EHBO cursus: ' . "\n\n";
$email_content_user = 'Gefeliciteerd met uw inschrijving voor de cursus EHBO en Reanimatie aan kinderen: '. "\n\n";

// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n\n";
if($key != 'submit') $email_content_user .= $key . ': ' . $value . "\n\n";

// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content . '', 'From: ' . $email);
// if validation passed ok then send the email to user
mail($email, $email_subject_user, $email_content_user . '', 'From: ' . $email_to);

// Update form switch
$form_complete = TRUE;

function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;

function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Kunt U het leven van een kind redden? Nee! Volg dan nu een EHBO cursus.</title>

it must almost certainly be sent with the mail function.

This the layout I want to give the email.
"body" : "<!DOCTYPE html>\n<html dir=\"ltr\" lang=\"en\">\n<head>
<title>U heeft zich ingeschreven voor de cursus EHBO en Reanimatie voor kinderen</title></head>\n
<body style=\"background-color: #f9f9f9; padding-left: 11%; padding-top: 7%; padding-right: 2%; max-width: 700px; font-family: Helvetica, Arial;\">\n
<style type=\"text/css\">\nbody {background-color: #f9f9f9;padding-left: 110px;padding-top: 70px; padding-right: 20px;max-width:700px;font-family: Helvetica, Arial;}\np{font-size: 12px; color: #666666;}\nh2{font-size: 28px !important;color: #666666 ! important;margin: 0px; border-bottom: 1px dotted #00A2FF; padding-bottom:3px;}\ntable{width:80%;}\ntd {font-size: 12px !important; line-height: 30px;color: #666666 !important; margin: 0px;border-bottom: 1px solid #e9e9e9;}\ntd:first-child {font-size: 13px !important; font-weight:bold; color: #333 !important; vertical-align:text-top; min-width:10%; padding-right:5px;}\na:link {color:#666666; text-decoration:underline;} a:visited {color:#666666; text-decoration:none;} a:hover {color:#00A2FF;}\nb{font-weight: bold;}\n</style>\n<h2 style=\"font-size: 22px !important;color: #666666 ! important;margin: 0px; border-bottom: 1px dotted #00A2FF; padding-bottom:3px;\">Wij danken u voor uw inschrijving kinder EHBO en Reanimatie cursus.. <br/>Hieronder treft u de gegevens aan zoals deze door u zijn opgegeven:</h2>\n<div>\n[i][_form_results_][/i]\n<br />\n<p>U bent akkoord gegaan met de algemene voorwaarden. Deze kunt u hier downloaden. http://example.com/documenten/algemene_voorwaarden.pdf </p>\n</div>\n</body>\n</html>\n"

Any help or suggestions are more than welcome.


[edited by: eelixduppy at 4:29 pm (utc) on Aug 7, 2013]
[edit reason] exemplified url [/edit]



WebmasterWorld Senior Member 5+ Year Member

Msg#: 4599673 posted 10:24 pm on Aug 6, 2013 (gmt 0)

You can tell an Email to send in HTML with a content-type mail header:

$headers = "From: admin@example.com\r\n"
.= "Reply-to: admin@example.com\r\n"
.= "Content-type: text/html; charset=ISO-8859-1";

$to = 'me@mysite.com';
$subject = 'A HTML Email';
$body = '<br><br><br><span style="color: #0000ff;">Some blue text!</span>';

mail($to, $subject, $body, $headers);


Msg#: 4599673 posted 6:51 pm on Aug 7, 2013 (gmt 0)

Thx Readie,

I got it working with $headers after experimenting a bit. Do you know why outlook does not set the html correctly. It seems that this only is with two email addresses I have a third one but it can not be checked right now.


WebmasterWorld Senior Member 5+ Year Member

Msg#: 4599673 posted 7:51 pm on Aug 7, 2013 (gmt 0)

Outlook is... Special. Different versions of outlook behave differently too, which makes it all the more fun.

I have seen a doctype destroy non inline css before on outlook 2003, and certain css selectors cause havoc too. Best advice is play around with it, reduce it down, then add stuff in bit by bit. When it stops working, you've found your problem.


Msg#: 4599673 posted 7:11 am on Aug 9, 2013 (gmt 0)

Thx Readie,
I got it working fine. I removed the \r out of my $headers and outlook opens it nicely.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved