我正在尝试使用此联系表单发送电子邮件,但我无法让它工作。它的输出是发送但我没有得到任何。测试发送邮件与简单的PHP形式,我收到邮件就好了,所以它不是我认为的服务器。
任何解释都高度赞赏。
<form id="contact-form" action="email.php" method="post" class="clearfix">
<div class="contact-box-hide">
<div class="col-sm-6">
<input type="text" class="form-control" id="first_name" name="first_name" required placeholder="First Name">
<span class="first-name-error"></span>
</div>
<div class="col-sm-6">
<input type="text" class="form-control" id="last_name" name="last_name" required placeholder="Last Name">
<span class="last-name-error"></span>
</div>
<div class="col-sm-6">
<input type="email" class="form-control" id="contact_email" name="contact_email" required placeholder="Email Address">
<span class="contact-email-error"></span>
</div>
<div class="col-sm-6">
<input type="text" class="form-control" id="subject" name="contact_subject" required placeholder="Subject">
<span class="contact-subject-error"></span>
</div>
<div class="col-sm-10">
<textarea class="form-control" rows="5" id="message" name="message" required placeholder="Message"></textarea>
<span class="contact-message-error"></span>
</div>
<div class="col-sm-2">
<button id="contact-submit" class="btn custom-btn col-xs-12" type="submit" name="submit"><a href=""></a><i class="fa fa-rocket"></i></button>
<span id="contact-loading" class="btn custom-btn col-xs-12"> <i class="fa fa-refresh fa-spin"></i> </span>
</div>
</div><!-- /.contact-box-hide -->
<div class="contact-message"></div>
</form><!-- /#contact-form -->
PHP
<?php
if($_REQUEST['first_name'] == '' || $_REQUEST['contact_email'] == '' || $_REQUEST['message'] == ''):
return "error";
endif;
if (filter_var($_REQUEST['contact_email'], FILTER_VALIDATE_EMAIL)):
$subject = 'Contact form'; // Subject of your email
// Receiver email address
$to = 'mail@domain.com'; //Change the email address by yours
// prepare header
$header = 'From: '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "\r\n";
$header .= 'Reply-To: '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "\r\n";
$header .= 'X-Mailer: PHP/' . phpversion();
$message .= 'Name: ' . $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . "\n";
$message .= 'Email: ' . $_REQUEST['contact_email'] . "\n";
$message .= 'Subject: ' . $_REQUEST['contact_subject'] . "\n";
$message .= 'Message: '. $_REQUEST['message'];
// Send contact information
$mail = mail( $to, $subject , $message, $header );
echo 'sent';
else:
return "error";
endif;
?>
JS
$('#contact-submit').click(function () {
$('.first-name-error, .last-name-error, .contact-email-error, .contact-subject-error, .contact-message-error').hide();
var first_nameVal = $('input[name=first_name]').val();
var last_nameVal = $('input[name=last_name]').val();
var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/igm;
var emailVal = $('#contact_email').val();
var contact_subjectVal = $('input[name=contact_subject]').val();
var messageVal = $('textarea[name=message]').val();
//validate
if (first_nameVal == '' || first_nameVal == 'First Name *') {
$('.first-name-error').html('<i class="fa fa-exclamation"></i> First name is required.').fadeIn();
return false;
}
if (last_nameVal == '' || last_nameVal == 'Last Name *') {
$('.last-name-error').html('<i class="fa fa-exclamation"></i> Last name is required.').fadeIn();
return false;
}
if (emailVal == "" || emailVal == "Email Address *") {
$('.contact-email-error').html('<i class="fa fa-exclamation"></i> Your email address is required.').fadeIn();
return false;
} else if (!emailReg.test(emailVal)) {
$('.contact-email-error').html('<i class="fa fa-exclamation"></i> Invalid email address.').fadeIn();
return false;
}
if (contact_subjectVal == '' || contact_subjectVal == 'Subject *') {
$('.contact-subject-error').html('<i class="fa fa-exclamation"></i> Subject is required.').fadeIn();
return false;
}
if (messageVal == '' || messageVal == 'Message *') {
$('.contact-message-error').html('<i class="fa fa-exclamation"></i> Please provide a message.').fadeIn();
return false;
}
var data_string = $('.contact-form').serialize();
$('#contact-submit').hide();
$('#contact-loading').fadeIn();
$('.contact-error-field').fadeOut();
$.ajax({
type: "POST",
url: "email.php",
data: data_string,
//success
success: function (data) {
$('.contact-box-hide').slideUp();
$('.contact-message').html('<i class="fa fa-check contact-success"></i><div>Your message has been sent.</div>').fadeIn();
},
error: function (data) {
$('.btn-contact-container').hide();
$('.contact-message').html('<i class="fa fa-exclamation contact-error"></i><div>Something went wrong, please try again later.</div>').fadeIn();
}
}) //end ajax call
return false;
});
答案 0 :(得分:1)
你总是收到消息“发送”,因为你没有检查是否发送邮件,所以添加额外的“if-else”来检查邮件是否发送,代码如下所示下面的所有内容和更新后的代码
在php中也有错误, 去掉 ”。”来自消息(仅一次)其他$消息不会被初始化。在js中发现另一个错误,你已经调用了一个类而不是id 更正了
<?php if($_REQUEST['first_name'] == '' || $_REQUEST['contact_email'] == '' || $_REQUEST['message'] == ''): return "error"; endif; if (filter_var($_REQUEST['contact_email'], FILTER_VALIDATE_EMAIL)): $subject = 'Contact form'; // Subject of your email // Receiver email address $to = 'mail@domain.com'; //Change the email address by yours // prepare header $header = 'From: '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "\r\n"; $header .= 'Reply-To: '. $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . ' <'. $_REQUEST['contact_email'] .'>'. "\r\n"; $header .= 'X-Mailer: PHP/' . phpversion(); $message= 'Name: ' . $_REQUEST['first_name'] . " " .$_REQUEST['last_name'] . "\n"; $message.= 'Email: ' . $_REQUEST['contact_email'] . "\n"; $message.= 'Subject: ' . $_REQUEST['contact_subject'] . "\n"; $message.= 'Message: '. $_REQUEST['message']; // Send contact information $mail = mail( $to, $subject , $message, $header ); if($mail) { // added if-else check echo 'sent'; } else { echo "mail couldnt send"; } else: return "error"; endif; ?>
现在用这个替换你的js代码(如下所示)
<script> $('#contact-submit').click(function () { $('.first-name-error, .last-name-error, .contact-email-error, .contact-subject-error, .contact-message-error').hide(); var first_nameVal = $('input[name=first_name]').val(); var last_nameVal = $('input[name=last_name]').val(); var emailReg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/igm; var emailVal = $('#contact_email').val(); var contact_subjectVal = $('input[name=contact_subject]').val(); var messageVal = $('textarea[name=message]').val(); //validate if (first_nameVal == '' || first_nameVal == 'First Name *') { $('.first-name-error').html('<i class="fa fa-exclamation"></i> First name is required.').fadeIn(); return false; } if (last_nameVal == '' || last_nameVal == 'Last Name *') { $('.last-name-error').html('<i class="fa fa-exclamation"></i> Last name is required.').fadeIn(); return false; } if (emailVal == "" || emailVal == "Email Address *") { $('.contact-email-error').html('<i class="fa fa-exclamation"></i> Your email address is required.').fadeIn(); return false; } else if (!emailReg.test(emailVal)) { $('.contact-email-error').html('<i class="fa fa-exclamation"></i> Invalid email address.').fadeIn(); return false; } if (contact_subjectVal == '' || contact_subjectVal == 'Subject *') { $('.contact-subject-error').html('<i class="fa fa-exclamation"></i> Subject is required.').fadeIn(); return false; } if (messageVal == '' || messageVal == 'Message *') { $('.contact-message-error').html('<i class="fa fa-exclamation"></i> Please provide a message.').fadeIn(); return false; } var data_string = $('#contact-form').serialize(); console.log(data_string); $('#contact-submit').hide(); $('#contact-loading').fadeIn(); $('.contact-error-field').fadeOut(); $.ajax({ type: "POST", url: "email.php", data: data_string, //success success: function (data) { console.log("response from php is"+data); if(data == "sent") //success { $('.contact-box-hide').slideUp(); $('.contact-message').html('<i class="fa fa-check contact-success"></i><div>Your message has been sent.</div>').fadeIn(); } else // anything other than sent will be error { $('.btn-contact-container').hide(); $('.contact-message').html('<i class="fa fa-exclamation contact-error"></i><div>Something went wrong, please try again later.</div>').fadeIn(); } }, error: function (data) { console.log("ajax failed"); $('.btn-contact-container').hide(); $('.contact-message').html('<i class="fa fa-exclamation contact-error"></i><div>Something went wrong, please try again later.</div>').fadeIn(); } }); //end ajax call return false; }); </script>
提交的屏幕截图
答案 1 :(得分:0)
即使mail()函数失败,您也正在打印'已发送'。
// Send contact information
$mail = mail( $to, $subject , $message, $header );
// Check the return value of mail()
if ($mail)
echo 'sent';
else
echo 'error';
// End of check
else:
return "error";
endif;