Lee Lee - 1 year ago 43
PHP Question

PHP Mail Formatting

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);


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(
'type' => 'error',
'text' => 'Request must come from Ajax'


//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!'));

//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!'));
if (!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) { //email validation
$output = json_encode(array('type' => 'error', 'text' => 'Please enter a valid email!'));
if (strlen($user_Message) < 5) { //check emtpy message
$output = json_encode(array('type' => 'error', 'text' => 'Too short message! Please enter something.'));

//proceed with PHP email.
$headers = 'From: ' . $user_Email . '' . "\r\n" .
'Reply-To: ' . $user_Email . '' . "\r\n" .
'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.'));
} else {
$output = json_encode(array('type' => 'message', 'text' => 'Hi ' . $user_Name . ' Thank you for your email'));

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!

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download