1

Based on the answer from: Ajax Form With Google Invisible Recaptcha, on my website I have a form that uses, PHP, Ajax and JQuery for the process. I am trying to integrate Recaptcha from Google Invisible Version 2 but I have not been able to achieve it, the recaptcha badge appears correctly and the validation seems to be also executed but when I submit the form the email does not arrive.

I have also been trying to implement the official google documentation but without results, I hope someone can help me I would appreciate it.: Documentation oficial google

vendor-contact.php

<?php 
$post_author_id     = get_post_field( 'post_author', $post->ID );
$user_email             = get_the_author_meta( 'user_email', $post_author_id);

$item_vendor_email        =   get_post_meta($post->ID, 'item_vendor_email', true);
if(empty($item_vendor_email))
{
  $item_vendor_email = $user_email;
}
?>
<div class="vendor-enquery-block">
    <h4 class="vendor-enquery-heading"><?php esc_html_e('Contact Vendor','weddlist');?></h4>
      <form class="ajax-auth" id="booknowbox" method="post">
      <?php wp_nonce_field('ajax-vendor-send-me', 'security'); ?>
      <div class="form-group">
        <input type="text" class="form-control required" id="name" name="name" placeholder="<?php esc_attr_e('Your Name *','weddlist'); ?>"/>
      </div>
      <div class="form-group">
        <input type="email" class="form-control required" id="email" name="email" placeholder="<?php esc_attr_e('Email Address *','weddlist'); ?>"/>
      </div>

      <div class="form-group">
        <div class="date-picker">
          <input type="text" class="form-control book_date check_book_date" id="date" name="book_date" placeholder="<?php esc_attr_e('Wedding Date','weddlist'); ?>"/>
          <label><i class="fa fa-calendar" aria-hidden="true"></i></label>
        </div>
      </div>
      <div class="form-group">
        <textarea id="details" name="details" placeholder="<?php esc_attr_e('Your Message','weddlist'); ?>" class="form-control"></textarea>
      </div>

      <div class="form-group">
        <button type="submit" id="book-now-on" class="btn btn-pink" class="g-recaptcha" data-sitekey="MySiteKey" data-callback="onSubmit" style="background: #4d1e25;"><?php esc_html_e('Submit','weddlist'); ?></button>
       </div>
       <div class="status"></div>
       <input type="hidden" id="user_email_id" value="<?php echo esc_attr($item_vendor_email);?>"  />
       <input type="hidden" id="vendorid" value="<?php echo esc_attr($post_author_id);?>"  />
       <input type="hidden" id="post_id" value="<?php echo get_the_ID();?>"  />
      </form><script src="https://www.google.com/recaptcha/api.js" async defer></script>

</div>

ajax-call-function.php

    add_action( 'wp_ajax_nopriv_weddlist_ajax_sendme', 'weddlist_ajax_sendme' );
add_action( 'wp_ajax_weddlist_ajax_sendme', 'weddlist_ajax_sendme' );  

function weddlist_ajax_sendme()
{
  global $current_user,$weddlist;
  wp_get_current_user();
  
  $allowed_html   =   array();

  $vendorid     =   $_POST['vendorid']; 
  $client_name      =   wp_kses( $_POST['name'] ,$allowed_html) ;
  $client_phone   =   wp_kses( $_POST['phone'] ,$allowed_html) ;
  $client_email     =   wp_kses( $_POST['email'] ,$allowed_html) ;
  $client_wedding_date=   $_POST['date'];
  $client_message   =   wp_kses( $_POST['details'] ,$allowed_html);
  $listing_name     =   get_the_title($_POST['post_id']) ;
  $client_contact   = $_POST['sendme'];
  $listing_url    = esc_url($_POST['item_url']);
  $vendor_email     = $_POST['user_email_id'];

  // Email Subject
  $subject = $weddlist['weddlist_subject_listing_vendor_enquiry'];
  $website_url = site_url();
  $website_name = get_option('blogname');

  $formated_subject = weddlist_sprintf("$subject", array(
    'website_url' => "$website_url",
    'website_name' => "$website_name",
    'listing_name' => "$listing_name",
    'listing_url' => "$listing_url",
    'vendor_email' => "$vendor_email",
  ));
  
  $mail_content = $weddlist['weddlist_content_listing_vendor_enquiry'];
  
  $formated_mail_content = weddlist_sprintf("$mail_content", array(
    'website_url' => "$website_url",
    'website_name' => "$website_name",
    'listing_name' => "$listing_name",
    'listing_url' => "$listing_url",
    'vendor_email' => "$vendor_email",
    'client_name' => "$client_name",
    'client_email' => "$client_email",
    'client_phone' => "$client_phone",
    'client_wedding_date' => "$client_wedding_date",
    'client_contact' => "$client_contact",
    'client_message' => "$client_message",
  ));

  $headers[] = 'Content-Type: text/html; charset=UTF-8';

  weddlist_header_email_append_filter();
  $mail = wp_mail($vendor_email,$formated_subject,$formated_mail_content,$headers); 

  if($mail)
  {
    $on_off_admin_copy = $weddlist['weddlist_listing_vendor_enquiry_admin_copy_enable'];
    if($on_off_admin_copy==1)
    {
      wp_mail(get_option('admin_email'), $formated_subject, $formated_mail_content, $headers);
    }
    echo '<div class="alert alert-success" role="alert"><button aria-label="Close" data-dismiss="alert" class="close" type="button">
      <span aria-hidden="true">&times;</span>
    </button>'.esc_html__('Your request has been successfully sent to vendor.','weddlist').'</div>';  

    $couple_inquiry=get_user_meta( $vendorid, 'couple_inquiry',true) ;
  
    if(!empty($couple_inquiry))
    {
      $final_couple_inquiry=$couple_inquiry+1;
      update_user_meta( $vendorid, 'couple_inquiry', $final_couple_inquiry) ;
    }
    else
    {
      update_user_meta( $vendorid, 'couple_inquiry', 1 ) ;
    }
  }
  else{
    echo '<div class="alert alert-warning" role="alert"><button aria-label="Close" data-dismiss="alert" class="close" type="button">
      <span aria-hidden="true">&times;</span>
    </button>'.esc_html__('Fail, Please try again.','weddlist').'</div>';     
  }  
  die();      
}

login.js

    jQuery('form#booknowbox').on('submit', function(e){
    if (!jQuery("#booknowbox").valid()) return false;       
    jQuery('.status', this).show().text(ajax_login_object.loadingmessage);
    action = 'weddlist_ajax_sendme';
    name = jQuery('#booknowbox #name').val();
    phone = jQuery('#booknowbox #phone').val();
    email = jQuery('#booknowbox #email').val();
    date = jQuery('#booknowbox #date').val();
    details = jQuery('#booknowbox #details').val();
    post_id = jQuery('#booknowbox #post_id').val();
    item_title = jQuery('.item_title').text();
    user_email_id = jQuery('#booknowbox #user_email_id').val();
    vendorid = jQuery('#booknowbox #vendorid').val();
    security = jQuery('#booknowbox #security').val();

    var sendme='';
    jQuery('input[name="sendme"]:checked').each(function() {
       sendme=sendme+'--'+this.value;
    }); 
    
    jQuery('.status').html('Loading..... ');
    jQuery.ajax({
        type: 'POST',
        url: ajax_login_object.ajaxurl,
        data: {
            'action': action,
            'name': name,
            'phone': phone,
            'email': email,
            'date': date,
            'details': details,
            'sendme': sendme,
            'item_title':item_title,
            'user_email_id':user_email_id,
            'post_id':post_id,
            'vendorid':vendorid,
            'item_url':window.location.href,                
            'security': security,               
        },
        success: function (data) {
            jQuery('.status').show();
            jQuery('.status').html(data);
            jQuery('#booknowbox #name').val('');
            jQuery('#booknowbox #phone').val('');
            jQuery('#booknowbox #email').val('');
            jQuery('#booknowbox #date').val('');            
        }
    });
    e.preventDefault();
}); 
Williams
  • 421
  • 4
  • 18
  • 1
    how are you sending the email? – Howard E Aug 30 '21 at 23:39
  • Debug your code to verify that it actually reaches the point where the mail is supposed to be send, and then go check https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail – CBroe Aug 31 '21 at 07:46
  • @Howard E, Update my code, add the file that makes the shipment, I hope you can help me please. – Williams Sep 03 '21 at 13:37
  • Ok... I think next step is like @CBroe is saying... debug the wp_mail. I would use this though https://developer.wordpress.org/reference/hooks/wp_mail_failed/ and dump output to error_log since it's part of an ajax function you won't get screen output. – Howard E Sep 03 '21 at 15:19

0 Answers0