我知道我的问题被问了很多次,我阅读了所有关于这个问题的讨论,但我没有找到正确的答案。
我的问题是总是在 wordpress 上调用返回的admin-ajax
请求。我的代码是:ajax
0
前端调用
$.ajax({
url: cubetechform_ajax.ajax_url,
type: "POST",
data: {
action: 'cubetech_contact_form_send_email',
name: name,
phone: phone,
email: email,
message: message
},
cache: false,
success: function(response) {
console.log(response);
},
error: function(XMLHttpRequest, ajaxOptions, thrownError) {
//console.log(XMLHttpRequest.status + '<br/>' + ajaxOptions + '<br/>' + thrownError);
},
});
后端
/*
* Action hooks
*/
function __construct() {
// Enqueue plugin styles and scripts
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_cubetech_form_scripts' ) );
// Setup Ajax action hook
add_action( 'wp_ajax_cubetech_send_email', array( $this, 'cubetech_send_email' ) );
add_action( 'wp_ajax_nopriv_cubetech_send_email', array( $this, 'cubetech_send_email' ) );
//$this->cubetech_send_email();
}
/**
* Register plugin styles and scripts
*/
public function register_contactme_scripts() {
wp_register_script( 'contactme_script', get_template_directory_uri() . '/js/contact_me.js', array('jquery','bootstrapValidationScript'), '', true);
}
/**
* Enqueues plugin-specific scripts.
*/
public function enqueue_cubetech_form_scripts() {
wp_enqueue_script( 'contactme_script', plugins_url( '/js/contact_me.js', __FILE__ ), array('jquery','bootstrapValidationScript'), '', true);
//wp_enqueue_script( 'contactme-script' );
wp_localize_script( 'contactme_script', 'cubetechform_ajax', array( 'ajax_url' => admin_url('admin-ajax.php')) );
}
public function cubetech_send_email() {
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// definisco il messaggio formattato in HTML
$email_body = '<html><body>';
$email_body .= '<p>Nome: '.$name.'<br>';
$email_body .= 'N. Telefono: '.$phone.'<br>';
$email_body .= 'Richiesta: '.$message.'</p>'.'<br>';
$email_body .= '</body></html>';
// definisco il messaggio formattato in HTML
$email_body = '<html><body>';
$email_body .= '<p>Nome: '.$name.'<br>';
$email_body .= 'N. Telefono: '.$phone.'<br>';
$email_body .= 'Richiesta: '.$message.'</p>'.'<br>';
$email_body .= '</body></html>';
$to = get_option('admin_email');
$subject = "Richiesta da: ". $email_address;
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtps.aruba.it"; // specify main and backup server
//$mail->SMTPDebug = 3; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->Username = "info@cubetech.it"; // SMTP username
$mail->Password = "*******"; // SMTP password
$mail->From = "info@cubetech.it";
$mail->FromName = "Cube Tech Bologna";
$mail->AddAddress("info@cubetech.it", "Cube Tech Bologna" ); // name is optional
$mail->AddReplyTo("info@cubetech.it", "Reply");
$mail->AddCC($email_address);
$mail->IsHTML(true); // set email format to HTML
$mail->SetFrom("info@cubetech.it", "Cube Tech Bologna");
$mail->Subject = $subject;
$mail->Body = $email_body;
$mail->AddAddress("info@cubetech.it");
$error = '';
if(!$mail->Send()) {
$error = 'Mail error: '.$mail->ErrorInfo;
echo $error;
exit();
die();
} else {
$error = 'ok';
echo $error;
exit();
die();
}
}
我尝试了我读到的所有内容die();
exit();
$mail = new PHPMailer();
$mail = new PHPMailer(true);
以及其他很多内容。问题总是与 ajax 调用有关,因为如果我尝试直接调用该函数cubetech_send_email()
,它可以完美地工作并返回 ok 并 phpmailer 发送它。我用服务器电子邮件在本地主机上尝试了所有这些,一切都很完美。有人可以帮我解决它吗?谢谢