在ajax中使用JS调用php函数发送邮件

问题描述 投票:0回答:1

我正在为functions.php内的wordpress反馈页面制作自定义反馈表单,反馈结果将发送到我的电子邮件。我添加了“Protected by reCAPTCHA”,这是 Google 的用于安全和垃圾邮件防护的 reCAPTCHA 服务。用户点击提交按钮后,将直接返回反馈页面,并显示感谢或错误消息。

这是我的代码:

function sendEmail($txtName, $txtEmail, $txtSubject, $txtMsg) {
   // Your email sending logic here
   $to = "[email protected]";
   $subject = 'Subjecy "' . $txtSubject . '"';
   $cc = $txtEmail; 
   $bcc = '[email protected]'; 
   $replyTo = $txtEmail; 

   $headers = 'From: ' . $txtEmail . "\r\n";
   $headers .= 'Cc: ' . $cc . "\r\n";
   $headers .= 'Bcc: ' . $bcc . "\r\n";
   $headers .= 'Reply-To: ' . $replyTo . "\r\n";
   $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

   $message = "From: $txtName &lt;$txtEmail&gt;<br>Subject: $txtSubject<br><br>Message Body:<br>$txtMsg;

   if (wp_mail($to, $subject, $message, $headers)) 
   {
       echo '</br><div id="thank-you-message"><b>';
       'Thank you for your message. It has been sent.';
       echo '</b></div></br>';                      
   } 

   else 
   {
       echo '</br><div id="thank-you-message">';
       'There was an error trying to send your message. Please try again later.';
       echo '</div></br>';
   }
}

function send_email_ajax_callback() 
{
   if (isset($_POST['your-name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['message'])) 
   {
       $txtName = sanitize_text_field($_POST['your-name']);
       $txtEmail = sanitize_email($_POST['email']);
       $txtSubject = sanitize_text_field($_POST['subject']);
       $txtMsg = sanitize_textarea_field($_POST['message']);

       sendEmail($txtName, $txtEmail, $txtSubject, $txtMsg);
   }

   wp_die(); // Always include this to exit WordPress properly
}

function feedback_form_shortcode() 
{ 
   ob_start(); // Start output buffering

   ?>

   <form action="" method="post" id="customForm" onsubmit="return validateForm();">
       <!-- my form fields here -->
       <input type="submit" name="submit" value="Submit">
   </form>

   <script>
       function validateForm() 
       {    

           // Get the reCAPTCHA response token
           var token;
           grecaptcha.ready(function () {
               grecaptcha.execute('my_site_key', { action: 'feedback' }).then(function (response) {
                   token = response;
                   console.log("token", token);

                   // Add the reCAPTCHA token to the form data
                   jq("#g-recaptcha-response").val(token); // Set the input field value                 
                
                   if (token) { 
                       // Send the form data to the server
                       jq.ajax({
                           type: 'POST',
                           url: window.location.href,
                           data: jq("#customForm").serialize(), // Serialize the form data
                           success: function () {
                               console.log("Function success4");
                               send_email_ajax_callback();
                           },
                           error: function (x, e) {
                               console.log("Error Response:", e);
                               jq("#lblError").text('Error processing the request.');
                               jq("#lblError").removeClass("hidden");
                           }
                       });                      
                   }
               });
           });
           // Ensure the form is not submitted twice
           return false;
       }
   </script>

   <?php

   return ob_get_clean(); // Return the buffered output
}
add_shortcode('feedback_form', 'feedback_form_shortcode');

之后,我在控制台中收到“send_email_ajax_callback 未定义”错误。不知道如何修复它。我想要表单代码和所有过程在同一个文件(functions.php)中完成。

javascript php html ajax
1个回答
0
投票

您应该从 PHP 调用

send_email_ajax_callback()
,而不是 JavaScript。

if (isset($_POST['g-recaptcha-response'])) {
    if (code to validate the reCAPTCHA token) {
        send_email_ajax_callback();
    }
}
    
function sendEmail($txtName, $txtEmail, $txtSubject, $txtMsg) {
   // Your email sending logic here
   $to = "[email protected]";
   $subject = 'Subjecy "' . $txtSubject . '"';
   $cc = $txtEmail; 
   $bcc = '[email protected]'; 
   $replyTo = $txtEmail; 

   $headers = 'From: ' . $txtEmail . "\r\n";
   $headers .= 'Cc: ' . $cc . "\r\n";
   $headers .= 'Bcc: ' . $bcc . "\r\n";
   $headers .= 'Reply-To: ' . $replyTo . "\r\n";
   $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

   $message = "From: $txtName &lt;$txtEmail&gt;<br>Subject: $txtSubject<br><br>Message Body:<br>$txtMsg;

   if (wp_mail($to, $subject, $message, $headers)) 
   {
       echo '</br><div id="thank-you-message"><b>';
       'Thank you for your message. It has been sent.';
       echo '</b></div></br>';                      
   } 

   else 
   {
       echo '</br><div id="thank-you-message">';
       'There was an error trying to send your message. Please try again later.';
       echo '</div></br>';
   }
}

function send_email_ajax_callback() 
{
   if (isset($_POST['your-name']) && isset($_POST['email']) && isset($_POST['subject']) && isset($_POST['message'])) 
   {
       $txtName = sanitize_text_field($_POST['your-name']);
       $txtEmail = sanitize_email($_POST['email']);
       $txtSubject = sanitize_text_field($_POST['subject']);
       $txtMsg = sanitize_textarea_field($_POST['message']);

       sendEmail($txtName, $txtEmail, $txtSubject, $txtMsg);
   }

   wp_die(); // Always include this to exit WordPress properly
}
// rest of your code here
© www.soinside.com 2019 - 2024. All rights reserved.