我正在尝试使用PHP从表单发送基本电子邮件。使用jquery.validate.js验证表单(工作正常),但我无法收到要发送的电子邮件。我只是继续获取我提交时在操作中声明的php文件。
网站为here,表单如下:
<form method="post" action="send-email.php" id="contact-form" class="form-horizontal" role="form">
<fieldset>
<div class="col-12 col-md-6 col-lg-6 contact--left">
<div class="form-group">
<label for="name" class="col-12 control-label brand brand-font">Name</label>
<div class="col-12">
<input type="text" class="form-control brand-font--standard" name="name" id="name">
</div>
</div>
<div class="form-group">
<label for="email" class="col-12 control-label brand brand-font">Email</label>
<div class="col-12">
<input type="email" class="form-control brand-font--standard" name="email" id="email">
</div>
</div>
<div class="form-group">
<label for="phone" class="col-12 control-label brand brand-font">Telephone</label>
<div class="col-12">
<input type="tel" class="form-control brand-font--standard" name="phone" id="phone">
</div>
</div>
</div>
<div class="col-12 col-md-offset-1 col-md-5 col-lg-offset-1 col-lg-5 contact--right">
<div class="form-group">
<label for="message" class="col-12 control-label brand brand-font">Message</label>
<div class="col-12">
<textarea class="form-control brand-font--standard" rows="6" name="message" id="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-12"> </div>
<div class="col-12">
<button type="submit" class="btn btn-block btn--orange brand-font">Submit</button>
</div>
</div>
</div>
</fieldset>
</form>
...而send-email.php文件如下所示:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "contact@designbydarren.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // required
$message = $_POST['message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$name)) {
$error_message .= 'The name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The message you entered do 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 .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
我尝试过使用我在网上找到的其他几个例子,但还没有任何东西可以坚持下去。为noob esque问题道歉,但我更喜欢这样的事情,所以对这类事情一无所知(无论你们看起来多么基本)
谢谢你的时间!
答案 0 :(得分:1)
您必须更改send-email.php
的以下行中的参数if(!preg_match($email_exp,$email_from)){
因为您已经在$ email_from
中使用了$ _POST ['email']使用默认的PHP邮件功能时,请更改php.ini文件中的SMTP设置
希望它能帮助!!!
答案 1 :(得分:0)
您已按如下方式定义了访问者的电子邮件地址:
$email_from = $_POST['email']; // required
但在第33行中,您尝试使用$email
,这是未定义的:
if(!preg_match($email_exp,$email)) {
将其更改为:
if(!preg_match($email_exp,$email_from)) {
这应该可以解决问题,并且您的表单应该有效。
无关旁注:PHP的mail()
函数是一个糟糕的选择。我建议切换到PHPMailer或SwiftMailer,因为它们可以让你更好地控制并且更强大。