php mail()返回false

时间:2016-08-03 08:35:49

标签: php phpmailer

我想在有人提交联系表单时从服务器发送电子邮件。这是示例php代码。

<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){

$name = strip_tags(trim($_POST['name']));
$email = strip_tags(trim($_POST['email']));
$dept = strip_tags(trim($_POST['dept']));
$message = strip_tags(trim($_POST['message']));

if((!isset($email) || empty($email)) || (!isset($name) || empty($name)) || (!isset($dept) || empty($dept)) || (!isset($message) || empty($message))){
    echo -1;
    return;
}
if(filter_var($email, FILTER_VALIDATE_EMAIL)===false){
    echo -2;
    return;
}

$to = 'farooqahmadkhan003@gmail.com'; 
$email_subject = "Splendenti Eye Wear | Contact Us";
$email_body = "Contacted by\nName: $name\nEmail: $email\nDepartment: $dept\nMessage: $message";
$headers = "noreply@farooq.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: ".$_POST['email'];
if(mail($to,$email_subject,$email_body,$headers)){
    echo 1; // email sent
}
else{
    echo "failed to send email";
}
}else{
echo "Invalid Request";
}
?>

但邮件功能返回false。任何人都可以指导吗?

2 个答案:

答案 0 :(得分:0)

我建议您使用PHPMailer从PHP文件发送电子邮件。 检查此link并下载。关于PHPMailer在线有很多文档。您可以使用PHPMailer控制有关邮件的所有详细信息。

答案 1 :(得分:-3)

尝试使用@来禁止警告:

if(@mail($to,$email_subject,$email_body,$headers)){
    echo 1; // email sent
}

如果这对您的配置问题没有帮助。