Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Send email with php

Hi I have validated a form with php but something seems to be wrong in the code.After validating it should send an email ,but it doesn't.Here is my code:

  $firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["email"];
$message = $_POST["message"];
$to = "[email protected]";
$subject = $firstName . " " . $lastName;
$headers = "From: [email protected]\r\nReply-To: [email protected]";

validate($firstName , $lastName , $email , $message);

function validate ($firstName , $lastName , $email , $message){
    if(!empty($firstName) && !empty($lastName)  && !empty($email) && !empty($message)){
        if(validateEmail($email)){
            $mail_sent = @mail($to , $subject , $message , $headers);
            header('refresh:5;url=http://www.foxteam.net');
        }else{
           header('refresh:0;url=http://www.foxteam.net/contact.php');
        }
    }else{
        header('refresh:0;url=http://www.foxteam.net/contact.php');
    }
}

function validateEmail($email) {
    $pattern = "^[A-Za-z0-9_\-\.]+\@[A-Za-z0-9_\-]+\.[A-Za-z0-9]+$";
    if(preg_match("/{$pattern}/", $email)) {
        return true;
    }else{
        return false;
    }
}

Upvotes: 0

Views: 168

Answers (1)

andrewsi
andrewsi

Reputation: 10732

function validate ($firstName , $lastName , $email , $message){
    if(!empty($firstName) && !empty($lastName)  && !empty($email) && !empty($message)){
        if(validateEmail($email)){
            $mail_sent = @mail($to , $subject , $message , $headers);
            header('refresh:5;url=http://www.foxteam.net');
        }else{
           header('refresh:0;url=http://www.foxteam.net/contact.php');
        }
    }else{
        header('refresh:0;url=http://www.foxteam.net/contact.php');
    }
}

You need to include $to and $headers as parameters for your function - they're outside of its scope, so it can't see them.

Upvotes: 1

Related Questions