-1

I want an email to be sent to my email adress via a contact form.On the form a client will input their name,email address and a message.However when the email is sent to my address I can only see the users meessage. How can i make it so that the clients name,email address and message is displayed in the email.

if (empty($_POST) === false)  {
    $errors = array();

    $name     = $_POST['name'];
    $email    = $_POST['email'];
    $message  = $_POST['message'];

    if (empty($name) === true || empty($email) === true || empty($message) === true)   {
        $errors [] = 'Name ,email and message are required !';
    } else {
        if (filter_var($email, FILTER_VALIDATE_EMAIL) === false)  {
            $errors[] = 'That\'s not a valid email address';
        }

        if (ctype_alpha ($name) === false) {
            $errors[] = 'Name must only contain Letters';
        }
    }
    if (empty($errors) === true) {
        mail('someone@hotmail.com', 'Contact form', $message,  'From: ' . $email);
        header('Location: index.php?sent');
        exit();
    }       
}

<!DOCTYPE html>
<html>
        <head>
                <title>A contact form</title>
        </head>
        <body>
            <?php
            if (isset($_GET['sent']) === true) {
                echo '<p>Thanks for Contacting us !</p>';
            } else {
                if (empty($errors) === false) {
                    echo '<ul>';
                    foreach ($errors as $error) {
                        echo '<li>', $error, '</li>' ;
                }
                echo'</ul>';
            }
            ?>  
            <form action="" method="post">          
                <p>
                    <label for="name">Name:</label><br>
                        <input type="text" name="name" id="name" <?php if (isset($_POST['name']) === true) {echo 'value="', strip_tags($_POST['name']), '"';  } ?>>
                        </p>
                        <p>
                            <label for="email">Email:</label><br>
                            <input type="text" name="email" id="email" <?php if (isset($_POST['email']) === true) {echo 'value="', strip_tags($_POST['email']), '"';  } ?>>
                        </p>
                        <p>
                            <label for="message">Message</label><br>
                            <textarea name="message" id="message"><?php if (isset($_POST['message']) === true) {echo strip_tags($_POST['message']);  } ?></textarea>
                        </p>
                        <p>
                            <input type="submit" value="Submit">
                        </p>
                     </form>
                <?php
                }
                ?>
        </body>
</html>
fafl
  • 7,222
  • 3
  • 27
  • 50
MrMoobies
  • 9
  • 3
  • How should name, address and message be displayed? Like "From: Peter Parker peter-parker@gmail.com"? Please be more specific what you want to achieve. – elementzero23 Jan 17 '17 at 14:50
  • Put in the parameters correctly and append additional values to the $message string. For infos on sending see http://stackoverflow.com/questions/5335273/how-to-send-an-email-using-php – Blackbam Jan 17 '17 at 14:57
  • start by reading `mail()` documentation - http://php.net/manual/en/function.mail.php – Pedro Lobito Jan 17 '17 at 15:00
  • *"How can i make it so that the clients name,email address and message is displayed in the email."* ... by appending the client's name and email address to the message. – CD001 Jan 17 '17 at 15:02

3 Answers3

0
$name     = $_POST['name'];

$email    = $_POST['email'];

$message  = $_POST['message'];

$message = $message. " Email: $email". " Name: $name";

You can edit the message parameter to be longer and contain more information. You can even set it to be html.

i.e. http://php.net/manual/en/function.mail.php section 4

BlackVegetable
  • 12,594
  • 8
  • 50
  • 82
0

According to php mail() manual:

to: Receiver, or receivers of the mail.

The formatting of this string must comply with » RFC 2822. Some examples are:

    user@example.com
    user@example.com, anotheruser@example.com
    User <user@example.com>
    User <user@example.com>, Another User <anotheruser@example.com>

mail() description:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )

Example 1:

<?php
mail("john doe <nobody@example.com>", "my subject", "my message")
?>

Example 2:

<?php
$to      = 'john doe <nobody@example.com>';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
0

Just add the name, and email to the message. We use this method all the time with PHP mail. See the $newMessage variable below. It contains the message with the name and email added to the top of it for easy reference.

It's also easier to see what you're doing if you create a $headers variable as an array or string depending on how many additional parameters you'll have. The way I've done it here you will see the persons name in the "From" portion of the email.

<?php

if (empty($_POST) === false)  {
$errors = array();

$name     = $_POST['name'];
$email    = $_POST['email'];
$message  = $_POST['message'];

if (empty($name) === true || empty($email) === true || empty($message) === true)   {
    $errors [] = 'Name ,email and message are required !';
} else {
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false)  {
        $errors[] = 'That\'s not a valid email address';
    }

    if (ctype_alpha ($name) === false) {
        $errors[] = 'Name must only contain Letters';
    }

}
if (empty($errors) === true) {

    $headers = 'From: ' . $name . ' <' . $email . '>';
    // Create the new message to send with the name and email.
    $newMessage = 'Name: ' . $name . '<br/>';
    $newMessage .= 'Email: ' . $email . '<br/>';
    $newMessage .= $message;

    mail('someone@hotmail.com', 'Contact form', $newMessage, $headers);

    header('Location: index.php?sent');
    exit();
}

}
?>
Kazpilot25
  • 72
  • 1
  • 7