不能让这 3 个脚本很好地发挥作用。试图让这个邮件功能正常工作并发送邮件。我已经删除了敏感信息并用通用术语替换了它。我还有另一个使用 preg_match 和其他验证方法的 php 邮件功能,但是它不起作用,所以我决定删除它直到它起作用,然后尝试再次添加验证服务器端..
我不太擅长 PHP,但我决定解决这个问题,所以任何帮助都将不胜感激。
我通常在我的 wamp 上测试所有这些,但有一个实时开发服务器 @marinetechegypt.com/contact.php/ 它可能不是最新的。
这是所有相关文件的内容。
HTML 表单 (contact.php)
<div class="col one-half">
<h3> Send us an Email </h3>
<div class="ajax_message"></div>
<form class="contact-form" method="post" action="php-mailer.php">
<input type="hidden" name="ac" value="login" />
<p>
<label for="fname"> Your Name <span class="required"> * </span> </label>
<input type="text" name="fname" class="input-short" id="fname" placeholder="Your Full Name" />
<?php if(isset($errName1)) echo $errName1; ?>
</p>
<p>
<label for="email"> Your Email <span class="required"> * </span> </label>
<input type="text" name="email" class="input-long" id="email" placeholder="Your Email" />
<?php if(isset($errEmail)) echo $errEmail; ?>
</p>
<p>
<label> Subject </label>
<input name="subject" type="text" id="subject" class="input-short" placeholder="Subject"/>
</p>
<p>
<label for="message"> Your Message <span class="required"> * </span> </label>
<textarea name="message" class="textarea-message" id="textarea" placeholder="Your Message"></textarea>
<?php if(isset($errMessage)) echo $errMessage; ?>
</p>
<p><input name="submit" type="submit" value="Send Message" class="button small grey contact-submit" /></p>
</form>
<div class="error-container" style="display:none;"> Please fill the above required fields correctly! </div>
</div>
php-mailer.php 文件:
<?php
if(isset($_POST['submit'])) {
$to = "RecipientEmailInbox";
$subject = $_POST['subject'];
$first_name_field = $_POST['fname'];
$email_field = $_POST['email'];
$message = $_POST['textarea'];
$message = "
Name: $first_name_field
Email: $email_field
Message: $message";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: <RecipientEmailInbox>' . "\r\n";
$headers .= '[From] <SenderEmailInbox>' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
include("OnCompleteURL");
ini_set("sendmail_from","EmailHeaderFrom");
ini_set("SMTP","SMTPAddress");
mail($to, $subject, $message, $headers);
} else {
echo "Error phpmailer";
}
?>
JS 客户端验证
$(document).ready(function(){
$.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || value != param;
}, "Please choose a value!");
$(".contact-form").each(function(){
$container = $(this).next("div.error-container");
//Validate Starts
$(this).validate({
onfocusout: function(element) { $(element).valid(); },
errorContainer: $container,
rules:{
fname:{required:true,minlength:3,notEqual:'Your Full Name'},
email:{required:true,email:true,notEqual:'Your Email'},
subject:{notEqual:'Subject'},
message:{notEqual:'Your Message'}
,message:{required:true,minlength:10}
}
});//Validate End
//Submit Starts
$(this).submit(function(e){
$frm = $(this);
$name = $frm.find('*[name=fname]').val();
$email = $frm.find('*[name=email]').val();
$subject = $frm.find('*[name=subject]').val();
$message = $frm.find('*[name=message]').val();
if($frm.find('*[name=fname]').is('.valid') && $frm.find('*[name=email]').is('.valid')) {
var action = $frm.attr('action');
$frm.find('*[name=submit]').attr('disabled', 'disabled').after('');
$frm.prev(".ajax_message").slideUp(750, function () {
$ajax_container = $(this);
$ajax_container.hide();
$.post(action, {
name: $name,
email: $email,
subject: $subject,
message: $message
}, function (data) {
$ajax_container.html(data);
$ajax_container.slideDown('slow');
$frm.find('*[name=submit]').attr('disabled', '');
if (data.match('success') != null) $frm.slideUp('slow');
});
});
}
e.preventDefault();
});
});
});