Gimy
Gimy

Reputation: 1

Mail script won't send every field

Sorry for the stupid question (I'm just starting out) but I can't quite understand why the phone field of my PHP script won't appear in the email I receive. I've added the field in HTML in works fine, the error message if the phone field is empty works good but if I run it, the email I receive will not contain the field.

Here is the PHP mail script:

<?php 

require('email_config.php');

// sender information
$name = trim($_POST['name']);
$phone = trim($_POST['phone']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$error = "";

// check sender information
$pattern = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$^";

if(!preg_match_all($pattern, $email, $out)) {
    $error = $invalid_email; // for invalid email
}

if(!$email) {
    $error = $invalid_email; // for empty email field
}   

if (!$phone) {
    $error = $invalid_phone; // for empty phone field
}

if(!$message) {
    $error = $invalid_message; // for empty message field
}

if (!$name) {
    $error = $invalid_name; // for empty name field
}

// email header

$headers = "From: ".$name." <".$email.">\r\nReply-To: ".$email."";

if (!$error){

    // sending email
    $sent = mail($to_email,$subject,$message,$headers); 

    if ($sent) {
            // if message sent successfully
            echo "SEND"; 
        } else {
            // error message
            echo $sending_error; 
        }
} else {
    echo $error; // error message
}
?>

Thanks in advance!

Upvotes: 0

Views: 50

Answers (1)

Oluwaseye
Oluwaseye

Reputation: 695

Looks like you skipped adding the phone to the message, Your message should be something like this

$message = trim($_POST['message']).' \n Phone: '.trim($_POST['phone']);

Seems like you are sending with plan text email so i used the new line special character /n.

To learn more about the special string characters http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

Upvotes: 1

Related Questions