用php发送电子邮件

时间:2012-07-10 16:57:00

标签: php

您好我已经使用php验证了一个表单但是代码中的某些内容似乎有问题。验证后它应该发送一封电子邮件,但它没有。这就是我的代码:

  $firstName = $_POST["firstName"];
$lastName = $_POST["lastName"];
$email = $_POST["email"];
$message = $_POST["message"];
$to = "fox.team001@gmail.com";
$subject = $firstName . " " . $lastName;
$headers = "From: fox.team001@gmail.com\r\nReply-To: fox.team001@gmail.com";

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;
    }
}

1 个答案:

答案 0 :(得分:1)

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');
    }
}

您需要包含$to$headers作为函数的参数 - 它们超出了其范围,因此无法看到它们。