I'll start by saying that I'm not a PHP coder at all (I usually use Coldfusion), however I've been tasked to fix this for an old project.
The form that triggers this works fine, and the email does send, however the formatting is off. This is the PHP we have right now (contact.php);
<?php
if ($_POST) {
$to_Email = "me@domain.com"; //Replace with recipient email address
$subject = 'Contact via Domain'; //Subject line for emails
//check if its an ajax request, exit if not
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type' => 'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if (!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userTelephone"]) ||!isset($_POST["userMessage"])) {
$output = json_encode(array('type' => 'error', 'text' => 'Input fields are empty!'));
die($output);
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
$user_Telephone = filter_var($_POST["userTelephone"], FILTER_SANITIZE_STRING);
//additional php validation
if (strlen($user_Name) < 4) { // If length is less than 4 it will throw an HTTP error.
$output = json_encode(array('type' => 'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if (!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) { //email validation
$output = json_encode(array('type' => 'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if (strlen($user_Message) < 5) { //check emtpy message
$output = json_encode(array('type' => 'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
//proceed with PHP email.
$headers = 'From: ' . $user_Email . '' . "
" .
'Reply-To: ' . $user_Email . '' . "
" .
'X-Mailer: PHP/' . phpversion();
$sentMail = @mail($to_Email, $subject, $user_Message . ' - ' . $user_Name. ' - ' . $user_Telephone, $headers);
if (!$sentMail) {
$output = json_encode(array('type' => 'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
} else {
$output = json_encode(array('type' => 'message', 'text' => 'Hi ' . $user_Name . ' Thank you for your email'));
die($output);
}
}
?>
The mail that is sent comes through like this;
tst 4 - Test - 123456
What I need to do is change it so it looks like this;
Name: tst 4
Email: sender@domain.com
Phone: 123456
Message: Test
The email address line isn't critical as the mails already come through with that in the 'from'.
The way this PHP file is setup and mail is sent doesn't match up with any samples I can find online, hence me being stuck!
The problem is the third parameter you are passing to the mail()
function which is the content for your email. Currently, you are just providing a hyphen seperated list so you need to create a new variable that looks something like the below:
$email_content = "
Name: $user_Name
Email: $user_Email
Phone: $user_Telephone
Message: $user_Message";
Then update your mail()
call to this:
$sentMail = @mail($to_Email, $subject, $email_content, $headers);
PHP mail() function:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
The last parameter, the headers, are optional for the function but required for sending HTML email, as this is where we are able to pass along the Content-Type declaration telling email clients to parse the email as HTML.
Content-Type: text/html; charset=UTF-8
In fact, the headers area gives us the opportunity to do lots of important email functions. This is where we can set the From: and Reply To: settings if need be, as well as CC and BCC other recipients
<?php
if ($_POST) {
$to_Email = "me@domain.com"; //Replace with recipient email address
$subject = 'Contact via Domain'; //Subject line for emails
//check if its an ajax request, exit if not
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
//exit script outputting json data
$output = json_encode(
array(
'type' => 'error',
'text' => 'Request must come from Ajax'
));
die($output);
}
//check $_POST vars are set, exit if any missing
if (!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userTelephone"]) ||!isset($_POST["userMessage"])) {
$output = json_encode(array('type' => 'error', 'text' => 'Input fields are empty!'));
die($output);
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
$user_Telephone = filter_var($_POST["userTelephone"], FILTER_SANITIZE_STRING);
//additional php validation
if (strlen($user_Name) < 4) { // If length is less than 4 it will throw an HTTP error.
$output = json_encode(array('type' => 'error', 'text' => 'Name is too short or empty!'));
die($output);
}
if (!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) { //email validation
$output = json_encode(array('type' => 'error', 'text' => 'Please enter a valid email!'));
die($output);
}
if (strlen($user_Message) < 5) { //check emtpy message
$output = json_encode(array('type' => 'error', 'text' => 'Too short message! Please enter something.'));
die($output);
}
//proceed with PHP email.
$headers = 'From: ' . $user_Email . '' . "
" .
'Reply-To: ' . $user_Email . '' . "
" .
'X-Mailer: PHP/' . phpversion() . "
" .
'Content-Type: text/html; charset=UTF-8
';
$msg = <<<MSG
Name: {$user_name} <br />
Email: {$user_Email} <br />
Phone: {$user_Telephone} <br /> <br />
Message: {$user_Message}
MSG;
$sentMail = @mail($to_Email, $subject, $msg, $headers);
if (!$sentMail) {
$output = json_encode(array('type' => 'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
die($output);
} else {
$output = json_encode(array('type' => 'message', 'text' => 'Hi ' . $user_Name . ' Thank you for your email'));
die($output);
}
}
?>