簡體   English   中英

Wordpress-發布到自定義PHP文件-錯誤500

[英]Wordpress - Post to a custom PHP File - ERROR 500

我們在網站上使用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>

mail.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來自您的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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM