-1

I'm pretty new to PHP. I've created a contact form which includes reply to sender. The problem is when form is submitted, the name and phone number of the sender doesn't show in the email.

How can I include the name and phone number of the sender in the email, for example in the signature field, or what's the best way to make this information show up?

Here is the code I have so far:

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $message = $_POST['message'];
    $to = 'email@example.com'; 
    $subject = 'A new message via contact form';

    $headers = 'Reply-To:'. $email . "\r\n" .
                'X-Mailer: PHP/' . phpversion();

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Your full name is required.';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Your email address is required.';
    }

    //Check if number has been entered
    if (!$_POST['number']) {
        $errNumber = 'Your phone number is required.';
    }       
    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please provide your message.';
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errNumber) {
        if (mail($to, $subject, $message, $headers, "From: " . $name)) {
            $result='<div class="alert alert-success">Thank you for your interest! You will be contacted within 24 hours.</div>';
        } else {
            $result='<div class="alert alert-danger">We apologize! An error has ocurred. Call us on +47 XX XXX XX</div>';
        }
    }
        }
 ?>
NohmanJ
  • 167
  • 3
  • 15
  • 1
    you're using 5 parameters for one thing [`mail($to, $subject, $message, $headers, "From: " . $name)` rather than 4.](http://php.net/manual/en/function.mail.php). Plus, `From:` expects an email address, not a name. – Funk Forty Niner Aug 27 '15 at 15:24

2 Answers2

1

Add the $name and $number to $message. Like:

$message .= "\r\n$name\r\n$number";

Edit: to clarify, this answer addresses the issue of the phone number and name not appearing in the email's message body. If this wasn't the actual problem I guess I have misunderstood the question.

Edit 2: as Fred -ii pointed out, there is an issue with the headers. Include "From:" in the fourth parameter instead of adding it as a fifth. And From: must also include an email address.

igorshmigor
  • 792
  • 4
  • 10
  • That is not the correct answer. Please adjust before getting downvotes! – B001ᛦ Aug 27 '15 at 15:25
  • @bub: I'm pretty sure that it IS the right answer. While "From:" should contain an email address he doesn't complain that the emails don't arrive. He complains that phone and name don't show up in the email that he receives. This is pretty obvious because he never added these parameters to the message that's to be sent. The other answer suggested that he used one parameter too many, but that's not true. You may call the mail function with five parameters. – igorshmigor Aug 27 '15 at 15:37
  • *"You may call the mail function with five parameters."* - RTFM http://php.net/manual/en/function.mail.php `bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )` - *"The additional_parameters parameter can be used to pass additional flags as command line options to the program configured to be used when sending mail, as defined by the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the `-f` sendmail option."* – Funk Forty Niner Aug 27 '15 at 15:42
  • as per your edit, all you needed to do was point out the headers issue and fix it the way the manual states; it's a simple fix that I'm sure you can add to your answer. The other part of your answer is fine. – Funk Forty Niner Aug 27 '15 at 15:54
  • Thanks for pointing it out. I should have looked more carefully at the mail configuration. – igorshmigor Aug 27 '15 at 15:59
0

To send value's with php there has to be set an session. In this session your value will be send with a name. If you declare the names that have to be saved, it will remember this. After that the page that gets the info can use this by getting POST or GET.

Add this line to the top of the page:
<?php session_start();
$_SESSION["name"] = $name;
$_SESSION["email"] = $email;
$_SESSION["number"] = $number;
$_SESSION["message"] = $message;
?>

If that is not what you need, then i got this solution: Just add:

 . $number

After $name the if statement that says if no errors mail send

Edit: im suggesting igorshmigor anwser because the phone number is getting into a POST but isn't declared in the mail officail POST