我正在尝试为我的网站实施联系表单,但是当我提交表单时,我的电子邮件地址中没有电子邮件。验证工作正常。 这是PHP文件:
<?php
if(isset($_POST['email'])){
$email_to = "you@yourdomain.com";
$email_subject = "AHU Enquiry";
function died($error){
echo "We are very sorry, but there are error(s) found with the form you submitted.";
echo "These error(s) appear below.<br/><br/>";
echo $error."<br/><br/>";
echo "Please go back and fix these error(s).<br/><br/>";
die();
}
if(!isset($_POST['first_name'])||
!isset($_POST['last_name'])||
!isset($_POST['email'])||
!isset($_POST['telephone'])||
!isset($_POST['comments'])){
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp, $email_from)){
$error_message .= 'The Email address you entered does not appear to be valid.<br/>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp, $first_name)){
$error_message .= 'The Firt Name you entered does not appear to be valid.<br/>';
}
if(!preg_match($string_exp, $last_name)){
$error_message .= 'The Last Name you entered does not appear to be valid.<br/>';
}
if(strlen($comments)<2){
$error_message .= 'The Comments you entered does not appear to be valid.<br/>';
}
if(strlen($error_message)>0){
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string){
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", $string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$headers = 'From: $first_name $last_name<$email_from>\r\n';
mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
有人可以指出我正确的方向吗?
以下是错误的屏幕截图。
答案 0 :(得分:1)
我猜您通过$ post提供的信息中存在错误,或者您没有设置邮件服务器
尝试注释掉整个代码块并尝试在php mail api网站上使用示例2
http://php.net/manual/en/function.mail.php
如果这样可行,那么您的代码就会出现问题,如果它无效,那么您的邮件服务器就会出现问题
错误日志也应该提供信息。
答案 1 :(得分:0)
我没有设置邮件服务器。每次您希望在提交联系表单后发送电子邮件时,您应该具有传出(SMTP)邮件服务器设置,以便您的电子邮件实际发送到所需的电子邮件地址。最简单的方法是使用Swift Mailer PHP代码。它比使用传统的mail()函数简单得多。以下是示例代码:
require_once 'SwiftMailer/lib/swift_required.php';
if(isset($_POST['email'])){
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_from = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
$email_message = "Form details below.<br/><br/>";
$email_message .= "First Name: ".$first_name."<br/>";
$email_message .= "Last Name: ".$last_name."<br/>";
$email_message .= "Email: ".$email_from."<br/>";
$email_message .= "Telephone: ".$telephone."<br/>";
$email_message .= "Comments: ".$comments."<br/>";
ini_set('max_execution_time', 300);
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('youremail@gmail.com')
->setPassword('yourpassword');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('AHU Enquiry')
->setFrom(array('youremail@gmail.com'=>'Sales Enquiries'))
->setTo(array('youremail@gmail.com'=>'Lead Recipients'))
->setSubject('Enquiry')
->setBody($email_message, 'text/html');
$result = $mailer->send($message);
}