【问题标题】:Wordpress - Post to a custom PHP File - ERROR 500Wordpress - 发布到自定义 PHP 文件 - 错误 500
【发布时间】:2018-09-08 07:16:16
【问题描述】:

我们的网站使用 WordPress。我被要求在我们的时事通讯订阅中添加一个功能,该功能会根据表单的选定值自动向特定地址发送电子邮件。从代码端和我的本地主机上工作正常,但是在将它实施到实时 wordpress 系统中时,我遇到了一个错误。情况:

jQuery.AJAX 脚本将表单数据发布到 wp-content 文件夹中的文件“mail.php”。 AJAX 成功函数然后提交原始表单(因为数据还需要发布到管理我们的时事通讯订阅的提供商)。这在非 wordpress 本地主机上运行良好。

通过 javascript 控制台和 firebug 搜索后,我意识到在脚本尝试将数据发布到 email.php 后,服务器返回 500 错误,就好像它不允许发布到此文件一样。

我没有以任何方式注册 mail.php 或脚本,而是将其添加到电子邮件表单后面的 html 代码中。我错过了什么吗?

谢谢!

<script>
jQuery(document).ready(function() {
    jQuery( "#subscribeform" ).one( "submit", function(event) {
        event.preventDefault();
        var pFirstName = jQuery("#firstname").val();
        var pLastName = jQuery("#name").val();
        var pSalutation = jQuery("#salutation").val();
        var peMail = jQuery("#email").val();
        var pDOB = jQuery("#dob").val();
        var pMailTo = jQuery("#shop").val();

        var data = {
            firstname: pFirstName,
            name: pLastName,
            salutation: pSalutation,
            email: peMail,
            dob: pDOB,
            mailto: pMailTo
        };

        $.ajax({
            type: "POST",
            url: "/cms/mail.php",
            data: data,
            success: function(){
                jQuery('#subscribeform').attr('action', "theExternalProviderURL").submit();
            }
        });
    });
});
</script>

ma​​il.php

<?php

include_once '/cms/phpmailer/PHPMailerAutoload.php';

if($_POST){
    $shopname = $_POST['mailto'];
    $salutation = $_POST['salutation'];
    $firstname = $_POST['firstname'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $dateofbirth = $_POST['dob'];
    $recipient = $_POST['mailto'];

    switch ($recipient) {
        case "Value1":
            $recipient = "mail1@mail.com";
            break;
        case "Value2":
            $recipient = "mail2@mail.com";
            break;
        default:
            $recipient = "admin@mail.com";
    }

    $oMailer = new PHPMailer;
    $oMailer->CharSet = 'UTF-8'; 
    $oMailer->isSMTP();
    $oMailer->Host = 'mail.host.com';
    $oMailer->Username = 'xxx';
    $oMailer->Password = 'xxx';
    $oMailer->SMTPAuth = true;
    $oMailer->SMTPSecure = 'tls';
    $oMailer->Port = 587; 
    $oMailer->From = 'email@email.com';
    $oMailer->FromName = 'From Email';
    $oMailer->addAddress('adress@adress.com'); 
    $oMailer->isHTML( true );
    $oMailer->Subject = 'E-Mail Subject';
    $oMailer->Body = 'Text Text Text';
    $oMailer->AltBody = strip_tags( $oMailer->Body );
    $oMailer->SMTPDebug = 2;

    if ( !$oMailer->send() ) {

        echo "Error sending Mail: " . $oMailer->ErrorInfo;
        exit;

    }
    echo 'Successfully sent mail to ' . $recipient . ' Shop'; 

}

?>

【问题讨论】:

  • 我们需要查看您的代码以提供帮助
  • HTTP 错误 500 表示“内部服务器错误”。这意味着可以正确访问 PHP 页面,但随后会遇到 (PHP) 错误。
  • 您好马里奥,感谢您的评论。该错误没有出现在我的本地主机上,并且我有多个检查器检查代码说没有错误(mail.php)
  • 检查正在运行的服务器的error_logs
  • 我刚刚发布了我的代码 - 当然,出于隐私原因,这些值已被编辑,但我测试了脚本,它在本地主机上运行良好

标签: javascript jquery html ajax wordpress


【解决方案1】:

如前所述,HTTP 500 来自您的 server/mail.php 代码中的问题。此外,还有一个特殊的钩子可以处理 WP 中的 ajax 请求,请参见此处:https://codex.wordpress.org/AJAX_in_Plugins

你需要的是这样的:

 var data = {data:yourdata, action: "yourajaxaction"};
 $.post(ajaxurl,{data: data});

add_action( 'wp_ajax_yourajaxaction', 'your_action' );

function your_action() { 
   include "mail.php";
}

【讨论】:

  • 太好了,我可以用它做点什么。感谢您的提示:o)
猜你喜欢
  • 2017-12-28
  • 1970-01-01
  • 2019-01-18
  • 2013-01-13
  • 1970-01-01
  • 2021-10-25
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多