【问题标题】:PHP Contact Form Not Working on WordpressPHP 联系表单在 Wordpress 上不起作用
【发布时间】: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”在哪里?

标签: php wordpress email


【解决方案1】:

在 WordPress 中有很多方法可以完成您想要做的事情。

当前您的表单操作指向contact_form_script.php,它假定此脚本位于当前目录中。但是该当前目录与您的contact_form.php 无关。它是相对于用户当前路径的。例如 www.mysite.com/contact/。

解决您的问题的一种快速(但懒惰)的方法是将contact_form_script.php 移动到您网站的根 WordPress 目录中。然后更改form action="/contact_form_script.php"(注意/),使其发布到根站点目录中名为contact_form_script.php 的文件中。

更好的方法是将contact_form_script.php 保留在您的模板目录中并将其包含在您的functions.php 中。

如果我们使用 email_form 提交输入让 WordPress 知道需要加载 contact_form_script.php 脚本,这应该允许您加载整个 wordpress 核心并且仍然让您的自定义表单处理脚本处理数据:

functions.php:

if (!empty($_POST['email_form'])) {
    require_once(__DIR__.'/contact_form_script.php');
    // You could put a die() here if you wanted the script to stop executing.
}

contact_form.php:

include( 'header.php');

if(have_posts()): while (have_posts()): the_post();


<div class="wrapper">
    <div class="message">

      <div class="text">
        <?php the_content(); ?>
        <form action="" method="post"> <!-- Notice we are now submitting our data to wordpress and not directly to our form script -->
            <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>

同样,这并不理想,但我怀疑这正是您目前正在寻找的东西,而且这是朝着正确方向迈出的一步。将代码保存在模板中。

【讨论】:

  • 谢谢,这有帮助!我没有完全遵循它,但是由于我是 WP 新手,所以我忘记了路径问题。我将 `
    ` 更改为 '
    method="post">' 和它作品。这还能接受吗?
  • 当然......任何适合你的都是可以接受的。 :)
【解决方案2】:

更改以下内容

 mail($to, $subj, $msg, $headers);

进入

wp_mail($to, $subj, $msg, $headers);

这将使 WordPress 正确路由您的邮件。

作为一个整体...您应该使用 wp_ajax 来执行此操作,您可以在不编写任何 javascript 的情况下使用它...创建一个包含在以下函数中的文件。

function process_contact_form() {

$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)){
    wp_mail($to, $subj, $msg, $headers);
};

header('Location:'.$_REQUEST['_wp_http_referer']);
wp_die();

}
    add_action( 'wp_ajax_process_contact_form', 'process_contact_form' );
    add_action( 'wp_ajax_nopriv_process_contact_form', 'process_contact_form' );

将您的模板文件更改为

 <?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="<?php echo admin_url( 'admin-ajax.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>
            <?php wp_referer_field(true); ?>
            <input type="hidden" name="action" value="process_contact_form">

            <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');
?>

【讨论】:

  • 谢谢。使用 mail() 和 wp_mail() 有什么区别? “正确路由您的邮件”是什么意思。 ?为什么要使用 wp_ajax 而不是原始方法?这只是偏好还是要求?只是想更好地理解,由于第一个答案它正在工作,但不确定这是更合适的解决方案,还是只是一个替代方案?
  • 抱歉,没有上下文,让我澄清一下。
  • mail() 是用于邮件发送的默认 PHP 函数。 wp_mail() 是相同的包装器,可以使用插件等进行扩展。它具有日志记录和其他一些使其有用的功能。在尝试做事时,它也被认为是更好的做法 使用 wp_ajax 的 WordPress 方式只是一种偏好。它允许您将处理联系表单的应用程序代码与模板分开。使用 wp_referrer_fields 可以提高联系表单的可重用性,因为每次您想将它们发送到其他地方时都不必复制模板。
  • 当我说正确路由您的邮件时,我的意思是允许插件或系统上的其他任何东西在您的电子邮件发出之前对其进行处理。 (记录、修改、复制等),甚至可以使用您的机器默认邮件服务器修改为使用类似 gmail 帐户的东西在测试机器上发送电子邮件。
  • 谢谢,这很有帮助。另一个答案直接修复了它。这很有帮助,因为我现在亲眼看到它为什么有用,因为我不能在 contact_form_script.php 脚本上使用 WP 代码。所以我无法获取电子邮件地址的 get_theme_mod(),或者包含页眉/页脚来设置页面样式。
【解决方案3】:

我们最近在联系表单中遇到了类似的问题。显然,这个问题不是我们的 PHP 问题的结果,而是共享主机策略的变化。当然,如果您使用的是 VPS,这将不是问题。

但是,如果您有任何形式的使用 cPanel 的共享主机和任何形式的基于脚本的电子邮件(包括联系表 7),这可能是一个潜在的解决方案。

使用“纸灯笼”主题,选择“Registered Mail IDs”选项,如下所示:

然后您应该可以选择添加其他电子邮件 ID。只需输入您在脚本中使用的电子邮件 ID。大约 8 小时后,ID 应该已经传播完毕,您就可以使用您的脚本了。

希望这会有所帮助!我们花了很长时间才弄清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多