【问题标题】:posting a footer contact form to a different script not working将页脚联系表单发布到其他脚本不起作用
【发布时间】:2012-02-24 21:28:31
【问题描述】:

我有两个文件,一个是我的实际联系表单,一个是我将表单发布到的文件。

contactform.php(页脚模板的一部分)

<form id="contact" action="<?php bloginfo('template_url'); ?>/sendmail.php" method="post">    
   <label for="name">Your name: *</label>
   <input type="text" id="nameinput" name="name" value=""/>

   <label for="email">Your email: *</label>
   <input type="text" id="emailinput" name="email" value=""/>

   <label for="comment">Your message: *</label>
   <textarea cols="20" rows="7" id="commentinput" name="comment"> </textarea><br />
</form>

发送邮件.php

<?PHP 
        if(isset($_POST['submit'])) {

      error_reporting(E_NOTICE);
      function valid_email($str)
      {
        return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
      }

      if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
      {

          $to = preg_replace("([\r\n])", "", hexstr($_POST['receiver']));
          $from = preg_replace("([\r\n])", "", $_POST['email']);
          $subject = "Website contact message from ".$_POST['name'];
          $message = $_POST['comment'];

          $match = "/(bcc:|cc:|content\-type:)/i";
            if (preg_match($match, $to) ||
                preg_match($match, $from) ||
                preg_match($match, $message)) {
              die("Header injection detected.");
            }
          $headers = "From: ".$from."\r\n";
          $headers .= "Reply-to: ".$from."\r\n";

    if(wp_mail($to, $subject, $message, $headers,'',true))
          {
              echo 1; //SUCCESS
          }
          else {
              echo 2; //FAILURE - server failure
          }
      }
      else {
      echo 3; //FAILURE - not valid email

      }
      }else{

         die("Direct access not allowed!");
       }

        function hexstr($hexstr) {
              $hexstr = str_replace(' ', '', $hexstr);
              $hexstr = str_replace('\x', '', $hexstr);
              $retstr = pack('H*', $hexstr);
              return $retstr;
            }

  ?>

问题是这不知道 wp_mail 功能。我知道我需要包含一些东西,以便 wp_mail 可用,但我要添加什么?该功能确实存在。包含定义了 wp_mail 的文件的问题在于,在该函数内部它需要一些核心 php 函数(wp_mail 被 cimy_swift 插件覆盖)

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    嗨,为什么不尝试将表单提交到基础 wpurl?然后在你的 header.php 文件中复制并粘贴你的代码?

    ie:使用隐藏字段,您可以检查它是否已发布,在这种情况下,隐藏字段称为“操作”,其值为“发送邮件”。

    表格

    <form id="contact" action="<?php bloginfo('wpurl'); ?>" method="post"> 
    //form stuff
    <input type="hidden" name="action" value="sendemail" />
    </form>
    

    Header.php 在头文件中,我们调用来检查表单是否已发布,

    <html>
    <head>
    <title><?php wp_title();?></title>
    <?php 
    if( isset($_POST['action']) && ($_POST['action']=='sendemail')  ) {
    // run your code
    
    }
    ?>
    </head>
    

    如果你不想走这条路,并希望使用你的主题文件夹来保存 php 脚本,那么在你的 sendmail.php 文件中包含以下内容

    define('WP_USE_THEMES', FALSE);
    require('../../../wp-blog-header.php');
    //above is assuming your file is located in the theme root, not a sub folder.
    

    这将使您可以访问所有的 wordpress 功能和简码等..等等.. 希望对您有所帮助..

    马蒂

    【讨论】:

    • 谢谢你。当我回到我的开发系统时,我会试试这个,如果它有效,我会告诉你。
    • 这仍然不起作用.. 放置或使用任何函数“停止”脚本。显示也没有错误
    猜你喜欢
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2014-05-17
    • 2019-10-21
    • 1970-01-01
    相关资源
    最近更新 更多