I have this form done it complete and working fine when the form is submitted the information is emailed to an email id but all the content is shown in plain text when it de
Make sure to send the email in HTML format and change the respective line to
Email: <b>$email</b> \n
Be sure to add headers, as specified on Mail page documentation
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
then use markup to compose your email
You'll need to add the Content-type
header, and add HTML tags to the email you're sending. So:
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email\n";
$headers .= "Content-type: text/html";
Also, you'll need to convert \n
to HTML line breaks, so:
$email_body = nl2br($email_body);
mail($to,$email_subject,$email_body,$headers);
I took the liberty of re-writing quite a bit, mostly because there is so much repetition, and a huge security issue.
One could make several further improvements on this as well, but that's left as an exercise to the reader.
<?php
$errors = '';
$myemail = 'abc@email.com';//<-----Put Your email address here.
$fields = array(
'fullname' => 'Name',
'martialstatus' => 'Martial Status',
'dateofbirth' => 'Date of Birth',
'email' => 'Email',
'telephone' => 'Telephone',
'cell' => 'Cell',
'graduation' => 'Year of Graduation',
'yearatt' => 'Years Attended',
'department' => 'Department',
'program' => 'Program Attended',
'permanentaddress' => 'Permanent Address',
'currentemp' => 'Currently Employeed with',
'designation' => 'Designation',
'selfemp' => 'Self Employeed',
'officeemail' => 'Office Email',
'officetele' => 'Office Telephone',
'portfolio' => 'Portfolio',
'membership' => 'Type of MemberShip',
);
// Don't allow HTML in form.
foreach ($_POST as $key => $value) {
$_POST[$key] = strip_tags($value);
}
$missing_fields = array_diff_assoc($fields, $_POST);
if (count($missing_fields) > 0) {
$errors .= "\n Error: all fields are required";
}
if (empty($errors)) {
$to = $myemail;
$email_subject = 'Membership Information: ' . $_POST['fullname'];
$email_body = 'You have received a new message. Here are the details:<br /> Personal Information <br/ ><br/ >';
foreach (array('fullname', 'martialstatus', 'dateofbirth', 'email', 'telephone', 'cell', 'graduation', 'yearatt', 'department', 'program', 'permanentaddress') as $key) {
$email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
}
$email_body .= "Career Information <br /><br />";
foreach (array('currentemp', 'designation', 'selfemp', 'officeemail', 'officetele', 'portfolio') as $key) {
$email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
}
$email_body .= "MemberShip <br /><br />";
foreach (array('membership') as $key) {
$email_body .= $fields[$key] . ': <strong>' . $_POST[$key] . "</strong><br />";
}
$headers = "From: $myemail\r\n";
$headers .= "Reply-To: $myemail\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
You headers must always end with `\r\n"
This is wrong
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
It should be like this
$headers = "From: $myemail\r\n";
$headers .= "Reply-To: $email\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
You need to add HTML tags in your mail
Example
Name: <strong>$fullname</strong><br />
Bold text can be done with an HTML email, set the content type header:
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html;\r\n";
Use the <strong>
tag:
<strong>Name:</strong> $fullname <br />
Don't forget \n
won't show a new line in an HTML email, you'll need to use <br />
.