【发布时间】:2018-10-27 19:33:15
【问题描述】:
我使用 mail() 构建了一个简单的 php 联系表单。这个表格已经在我的网站上使用了多年。
不过,我最近在 wordpress 上建立了一个新网站。我几乎复制并粘贴了相同的表格,但是,我没有收到任何电子邮件。当我点击提交时,表单只是重定向到主页/索引布局,但有联系表单 url。
然而,在原始网站上,会在空白屏幕上打印一条消息,确认电子邮件已发送。我还在原网站上测试了原代码,它可以工作。
contact_form.php:
<?php
/*
Template Name: Contact Form
`*/
include( 'header.php');
?>
<?php
if(have_posts()): while (have_posts()): the_post();
?>
<div class="wrapper">
<div class="message">
<div class="text">
<?php the_content(); ?>
<form action="contact_form_script.php" method="post">
<p>Your email address:</p> <input type="text" name="emailAddress">
<p>Subject:</p> <input type="text" name="subject">
<p>Message:</p> <textarea name="message"></textarea>
<input type="text" name="honeyPot" style="display: none;">
<br />
<input type="submit" name="email_form" value="SUBMIT"/>
</form>
<br />
<br />
</div>
</div>
</div>
<?php
endwhile;
endif;
?>
<?php
include('footer.php');
?>
contact_form_script.php
<?php
$to= 'myEmailAddress@email.com';
$from = "FROM: Email@myDomain.com";
$replyTo=$_POST['emailAddress']; /*whatever the user input*/
$headers= $from ."\r\n" .'Reply-To: '. $replyTo;
//Have also tried just $_POST['subject']; Still doesn't work
$subj="Subject: "+$_POST['subject'];
$msg=$_POST['message'];
$spam=$_POST['honeyPot'];
if (empty($spam)){
mail($to, $subj, $msg, $headers);
};
Print "Thank you for email! <br /> <br /> <a href='/'><--BACK</a>"
?>
此外,除了检查我是否收到电子邮件之外,还有更好的方法来测试这样的表单以查看错误或排除故障吗?
【问题讨论】:
-
你在这里缺少一个结尾引用:
$to= 'myEmailAddress@email.com; -
谢谢。那是一个错字。固定在问题中。仍然不发送报价
-
您是否对 $spam 进行了 var_dumped 以确保分配了一个值?
-
您的脚本存储在服务器上的什么位置?查看contact_form.php,这看起来像是主题/模板的一部分。 “contact_from_script.php”在哪里?