perkface
perkface

Reputation: 113

PHP & HTML Mailer Only Sending One Parameter in the Body

This is my first time posting here, so please excuse me if I don't use the code formatting correctly. Also, PHP is not my strong-point.

I'm attempting to send a user-populated e-mail with HTML and PHP. I've gotten the e-mail to successfully send, but I'm having issues with the body of the e-mail. Only the phone number is populating the e-mail, nothing else, not the name, email, or message. Any ideas?

I know that the parameters are being populated. I inspect the page, and watch the Network events through Firebug on Firefox. The parameters are being passed to PHP, I'm fairly certain.

//building message body
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

//adding to the body variable
$body = "Name: " + $name + "\r\n" + "E-mail: " + $email + "\r\n" + "Phone: " + $phone + "\r\n" + "Message: " + $message + "\r\n";

//email info
$to = '[email protected]';
$subject = 'You have been contacted via the web form!';
$from = '[email protected]';


mail($to, $subject, $body);

<!-- Mailer -->
<form role="form" method="post" action="mail/contact_me.php">
  <div class="row">
    <div class="col-md-6">
      <div class="form-group">
        <input type="text" data-validation-required-message="Please enter your name." required="" id="name" name="name" placeholder="Your Name *" class="form-control" value="">
        <p class="help-block text-danger"></p>
      </div>
      <div class="form-group">
        <input type="email" data-validation-required-message="Please enter your email address." required="" id="email" name="email" placeholder="Your Email *" class="form-control" value="">
        <p class="help-block text-danger"></p>
      </div>
      <div class="form-group">
        <input type="tel" data-validation-required-message="Please enter your phone number." required="" id="phone" name="phone" placeholder="Your Phone *" class="form-control" value="">
        <p class="help-block text-danger"></p>
      </div>
    </div>
    <div class="col-md-6">
      <div class="form-group">
        <textarea data-validation-required-message="Please enter a message." required="" id="message" name="message" placeholder="Your Message *" class="form-control" style="height:132px;" value=""></textarea>
        <p class="help-block text-danger"></p>
      </div>
    </div>
    <div class="col-lg-12 text-center">
      <div id="success"></div>
      <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary" />
    </div>
    <div class="form-group">
      <!-- Will be used to display an alert to the user -->
    </div>
  </div>
</form>
<!-- End Mailer -->

Here is the URL for a picture showing my parameters populating in Network mode (Firebug). **Stack overflow won't let me post pictures due to my lack of reputation.

http://tinypic.com/r/2znpdme/8

Upvotes: 1

Views: 81

Answers (2)

Chris Brendel
Chris Brendel

Reputation: 700

You need to use the . concatenation operator for strings instead of + in PHP. In fact, you can just interpolate the variables right in the double-quoted string (interpolation is more usual in PHP in this situation):

<?php
//building message body
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

//adding to the body variable
$body = "Name: $name\r\nE-mail: $email\r\nPhone: $phone\r\nMessage:$message\r\n";

//email info
$to = '[email protected]';
$subject = 'You have been contacted via the web form!';
$from = '[email protected]';


mail($to, $subject, $body);
?>

Upvotes: 0

taxicala
taxicala

Reputation: 21769

You are concatenating wrongly, probably you come from JS ;)

$body = "Name: " + $name + "\r\n" + "E-mail: " + $email + "\r\n" + "Phone: " + $phone + "\r\n" + "Message: " + $message + "\r\n";

Should be

$body = "Name: " . $name . "\r\n" . "E-mail: " . $email . "\r\n" . "Phone: " . $phone . "\r\n" . "Message: " . $message . "\r\n";

Upvotes: 3

Related Questions