我想在有人提交聯繫表單時從服務器發送電子郵件。這裏是示例php代碼。php mail()返回false
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 = '[email protected]';
$email_subject = "Splendenti Eye Wear | Contact Us";
$email_body = "Contacted by\nName: $name\nEmail: $email\nDepartment: $dept\nMessage: $message";
$headers = "[email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected]
$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。任何人都可以指導?
+0
是您的電子郵件服務器配置(sendmail/smtp) –
+0
我該如何檢查@SureshKamrushi? –
+0
[閱讀文檔](http://php.net/manual/en/function.mail.php)和用戶評論。 –