Emil Rosenius
Emil Rosenius

Reputation: 1001

PHP mail script not sending textarea and duplicating everything else in body

Im trying to send an e-mail with the following script I've made. But seem to encounter a weird problem that I need help with.

The mail script

// Get field values.
$name = strip_tags($_POST["name"]);
$email = strip_tags($_POST["email"]);
$message = $_POST["msg"];

// Check if e-mail address is valid.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  // Set e-mail and subject.
  $to = "[email protected]";
  $subject = "You have a new message.";

  // Set header values.
  $headers  = "From: " . $email . "\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";

  // Set request body.
  $message  = "<html>";
  $message .=   "<body>";
  $message .=     "<p><b>From:</b><br>" . $name . "</p>";
  $message .=     "<p><b>Email:</b><br>" . $email . "</p>";
  $message .=     "<p><b>Message:</b><br>" . $message . "</p>";
  $message .=   "</body>";
  $message .= "</html>";

  mail($to, $subject, $message, $headers);
  echo "Your email was sent!";

} else {

  echo "Invalid Email, please provide an correct email.";

}

The HTML

<form id="contact-form" data-toggle="validator" data-disable="true" role="form">
               <div class="form-group">
                 <label for="name">Navn</label>
                 <input type="text" name="name" id="contact-name" class="form-control" data-minlength="2" data-error="Please provide a valid name." required>
                 <div class="help-block with-errors"></div>
               </div>
               <div class="form-group">
                 <label for="email">E-mail</label>
                 <input type="email" name="email" id="contact-email" class="form-control" data-minlength="5" data-error="Please provide a valid e-mail address." required>
                 <div class="help-block with-errors"></div>
               </div>
               <div class="form-group">
                 <label for="message">Your message:</label>
                  <textarea name="msg" id="contact-email" data-minlength="10" data-error="Your message must be at least 10 characters long." class="form-control" required></textarea>
                  <div class="help-block with-errors"></div>
               </div>
               <div class="form-group">
                 <button id="submit" value="send" class="btn btn-primary">Send</button>
                 <div id="success"></div>
               </div>
             </form>

The Javascript

$(document).ready(function(){

  $('#success').css('display', 'none');

  $('#submit').click(function(e) {
    e.preventDefault();
    $.ajax({
      url: "php/form.php",
      data: $("#contact-form").serialize(),
      type: 'POST',
      statusCode: {
        500: function(data) {
          $('#success').css('display', 'none');
          $('#success').css('color', '#A94442');
          $('#success').html('Your message was not sent.');
          $('#success').fadeIn(200);
        },
        404: function(data) {
          $('#success').css('display', 'none');
          $('#success').css('color', '#A94442');
          $('#success').html('Your message was not sent.');
          $('#success').fadeIn(200);
        },
        200: function(data) {
          console.log(data);
          $('#success').css('display', 'none');
          $('#success').css('color', '#74C274');
          $('#success').html('Your message was sent.');
          $('#success').fadeIn(200);
        }
      }
    });
  });
});

The e-mail is sent and received, but the textarea is not getting sent through, and it seems to sent the "email" and "name" field twice in the message body.

The e-mail output looks like this:

From:
Someone

Email:
[email protected]

Besked:

From:
Someone

Email:
[email protected]

Help will be very much appreciated. Have been trying to fix this for hours now.

Upvotes: 2

Views: 273

Answers (1)

Rapha&#235;l Vig&#233;e
Rapha&#235;l Vig&#233;e

Reputation: 2045

The error is located here :

$message .=     "<p><b>Message:</b><br>" . $message . "</p>";

You are using the same variable for the message to be sent and the message received by your PHP.

This code will be working :

// Get field values.
$name = strip_tags($_POST["name"]);
$email = strip_tags($_POST["email"]);
$message_text = $_POST["msg"];

// Check if e-mail address is valid.
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  // Set e-mail and subject.
  $to = "[email protected]";
  $subject = "You have a new message.";

  // Set header values.
  $headers  = "From: " . $email . "\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";

  // Set request body.
  $message  = "<html>";
  $message .=   "<body>";
  $message .=     "<p><b>From:</b><br>" . $name . "</p>";
  $message .=     "<p><b>Email:</b><br>" . $email . "</p>";
  $message .=     "<p><b>Message:</b><br>" . $message_text . "</p>";
  $message .=   "</body>";
  $message .= "</html>";

  mail($to, $subject, $message, $headers);
  echo "Your email was sent!";

} else {

  echo "Invalid Email, please provide an correct email.";

}

Upvotes: 1

Related Questions