Farjana
Farjana

Reputation: 59

Validation an Email in PHP?

     <?php
          if (isset($_POST["sendMessage"])) {
          $firstName = $_POST['firstName'];
          $lastName = $_POST['last-name'];
          $email = $_POST['email'];
          $phone = $_POST['phone'];
          $message = $_POST['message'];
          $from= 'info@address.com';
          $to = 'some@gmail.com';
          $subject = 'Message from Contact Demo ';
          $txt= 'demo';
          $headers = "From: webmaster@example.com" . "\r\n" .
         "CC: somebodyelse@example.com";
        $body = "From: First-Name: $firstName\n Last-Name: $lastName\n                    

        E-Mail: $email\n Phone: $phone\n Message: $message";
                        $errFirstName='';$errLastName='';$errEmail='';$errPhone='';$errMessage='';



// If there are no errors, send the email

        if (mail ($to, $subject,$txt,$headers)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }

?>

i am trying to make a mail validation but it will not work.Here some text which will give a success message or error message.but it show nothing . i can't understand my fault. please help someone.

Upvotes: 1

Views: 89

Answers (3)

Sunny Khatri
Sunny Khatri

Reputation: 535

Try this code on your live server

    $firstName = "sunny";
    $lastName = "khatri";
    $email = '******@gmail.com';
    $phone = '********';
    $message ='Hello this is test';
    $from= '*****@gmail.com';
    $to = '***@gmail.com';
    $subject = 'Message from Contact Demo ';
    $txt= 'demo';
    $headers = "From: ******@*****.com" . "\r\n" .
    "CC: ********@live.in";
    $body = "From: First-Name: $firstName\n Last-Name: $lastName\n                    

    E-Mail: $email\n Phone: $phone\n Message: $message";
                $errFirstName='';$errLastName='';$errEmail='';$errPhone='';$errMessage='';

    // If there are no errors, send the email

    if (mail ($to, $subject,$txt,$headers)) {
          $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
         $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
    echo $result;

Upvotes: 1

husen
husen

Reputation: 180

First You Have To Check Email Is Valid Using This Php Function

$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format";
}

Then If Its Not Valid Give A Error Else

if (mail ($to, $subject,$txt,$headers)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }

try it..

Upvotes: 0

NoOorZ24
NoOorZ24

Reputation: 3277

Just use default function from PHP manual:

<?php
    $email_a = 'joe@example.com';
    $email_b = 'bogus';

    if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
        echo "This ($email_a) email address is considered valid.\n";
    }
    if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
        echo "This ($email_b) email address is considered valid.\n";
    } else {
   echo "This ($email_b) email address is considered invalid.\n";
   }
?>

Upvotes: 0

Related Questions