Reputation: 1
I am new to Stackoverflow, and thought I bring my first issue to you wonderful people.
I have a PHP Form that I need to out put as an HTML template. The success message does this fine. But the message that is sent via email doesn't do it. Here is my code example. The form works well except for the HTML output. I put tags in it to see if it works, but the tags do not convert.
Any help would be greatly appreciated.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = ($_POST['email']);
$email_subject = "Dear ".($_POST['first_name']). ", someone you know sent you this email.";
function died($error) {
// your error code can go here
echo "<h4>We are very sorry, but you left a field blank or incorrect. </h4>";
echo "Maybe you need to be slapped.<br /><br />";
echo $error."<br /><br />";
echo "Please press the back button to resend redo.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) // ||!isset($_POST['last_name'])
||!isset($_POST['email'])
//||!isset($_POST['telephone'])
//||!isset($_POST['comments'])
) {
died('We are very sorry, but left a field blank or incorrect.');
}
$first_name = $_POST['first_name']; // required
// $last_name = $_POST['last_name']; // required
$email_from = "[email protected]"; // required
// $telephone = $_POST['telephone']; // not required
// $comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
// if(!preg_match($string_exp,$last_name)) {
// $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
// }
// if(strlen($comments) < 2) {
// $error_message .= 'The Comments you entered do not appear to be valid.<br />';
// }
if(strlen($error_message) > 0) {
died($error_message);
}
// Set the Headers for HTML E-mail
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// email message sent to the person getting slapped
$email_message = "Someone has just sent you an email.\n\n";
function clean_string($string) {
$headers = "Content-Type: text/html; charset=iso-8859-1\n";
$email_message = " This is a test <strong> of the HTML </strong>";
}
$email_message = " This is a test <strong> of the HTML </strong>";
// $email_message .= "First Name: ".clean_string($first_name)."\n";
// $email_message .= "Last Name: ".clean_string($last_name)."\n";
// $email_message .= "Email: ".clean_string($email_from)."\n";
// $email_message .= "Telephone: ".clean_string($telephone)."\n";
// $email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for sending your <strong>email</strong>. <br />
Please press the back button
<?php
}
?>
Upvotes: 0
Views: 813
Reputation: 352
You're canceling out your headers:
$headers = "Content-Type: text/html; charset=iso-8859-1\n";
then you have:
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
instead try:
$headers = "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
the .= in the second assignment of $headers variable concatenates the 2 together, in your code, you're replacing $headers with the second assignment.
Upvotes: 1