我一直在单页网站上工作。它有8页。最后一个是联系表格。我有一个PHP函数" send_mail.php"将数据发布到我的Gmail帐户。此代码仅在我将其放在单独的html中时才有效,但如果我通过我的index.html调用此函数,则它不会发送电子邮件。
<section id="contact">
<div class="container-wrapper">
<div class="container">
<div class="row">
<div class="col-sm-4 col-sm-offset-8">
<div class="contact-form">
<h3>Contact Info</h3>
<form id="main-contact-form" name="contact-form" method="post" action="mail/send_mail.php">
<div class="form-group">
<input type="text" name="user_name" class="form-control" placeholder="Name" required>
</div>
<div class="form-group">
<input type="email" name="email_address" class="form-control" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" name="email_subject" class="form-control" placeholder="Subject" required>
</div>
<div class="form-group">
<textarea name="comments" class="form-control" rows="8" placeholder="Message" required></textarea>
</div>
<input type="submit" value="Submit" />
</form>
</div>
</div>
</div>
</div>
</div>
</section><!--/#bottom-->
然后我的php
<?php function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}}$email_address = $_REQUEST['email_address'] ;
$name = $_REQUEST['user_name'] ;
$subject = $_REQUEST['email_subject'] ;
$comments = $_REQUEST['comments'] ;
// If the user tries to access this script directly, redirect them to feedback form,
if (!isset($_REQUEST['email_address'])) {
header( "Location: index.html" );
}
// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: index.html" );
}
// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: index.html" );
}
// If we passed all previous tests, send the email!
else {
mail( "abcd@gmail.com", $subject,
$comments, "From: $email_address" );
header( "Location: index.html" );
}
?>
不知何故,我发现了错误位置,但无法缩小范围。如果我删除了我的js引用,我可以发送电子邮件。