我们正在为我们的网站使用WordPress。我被要求在我们的新闻订阅中添加一个功能,该功能会自动将电子邮件发送到特定地址,该地址取决于所选择的表单值。从代码端和我的本地主机上工作正常,但是当它实现到live 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';
}
?>
答案 0 :(得分: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";
}