How to send automatic response after form submission without plugin

I create an ajax form and I well received the email.

Now, I just want to do like CF7, send an automatic response to the sender to notify him that I well received the form. Which function used or which hook ? Apart from Reply_to with Headers of wp_mail I didn't find any solution to send confirmation or notification.

Anybody can fill the form.

Here php code to send the form :

function mail_form(){


if ( ! wp_verify_nonce( $_POST['nonce'], 'ajax-nonce' )  ! isset( $_POST[ 'formFields' ] ) ) {
     die ( 'ERREUR!');
 }else{
    parse_str( $_POST[ 'formFields' ], $formInfos );
    
    $name = wp_strip_all_tags($formInfos['name_mailform']);
    $adress = wp_strip_all_tags($formInfos['adress_mailform']);
    $subject = sanitize_email($formInfos['subject_mailform']);
    $message = nl2br(stripslashes(wp_kses($formInfos['message_mailform'], $GLOBALS['allowedtags'])));
    
    
    $to = '[email protected]';
    $subject = 'Question/Demande' . $subject ;
    $body = '!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
html xmlns=http://www.w3.org/1999/xhtml 
head 
meta http-equiv=Content-Type content=text/html; charset=utf-8 /
titleAssistance/title
/head
body style=margin: 0;padding: 0;min-width: 100% !important;background:#D9D08A;
table width=100%  border=0 cellpadding=10 cellspacing=0 class=bgColortrtd
table class=content align=center cellpadding=0 cellspacing=0 border=0trtd
div class=project floatLimg src=https://www.monsite.com/wp-content/uploads/logo-titre-black.png class=lgo
div class=subtitle_logoQuestion pour /div
        /div
        div class=ref_destinataire floatR
          img src=logo.png class=lgospan class=title_activitybr / Activity/span
        /div
      /td
    /tr
    tr
      td
        div class=title_para Le message /div
        div class=info_contact' . $message .'/div
                         div class=title_paraInformations de l\'expediteur :/div
        div class=info_contact'. $name .'/div
        p class=info_contact'. $adress .'/p
        p class=why_mail Cet email vous a été envoyé car .../p
        
                        
      /td
    /tr
                
  /table
          
/td/tr
/table
/body
/html';
add_filter( 'wp_mail_from_name', function( $name ) {
        return 'MonSite';
    } );
    add_filter( 'wp_mail_from', , function( $name ){
        return '[email protected]';
    }
    $headers = array('Content-Type: text/html; charset=UTF-8');
    
    

    wp_mail( $to, $subject, $body, $headers );
    

    
    // Envoi de l\'email
    /*add_filter('wp_mail_content_type', create_function('','return text/html;'));
    
    if(wp_mail('[email protected]', '[monSite] Bonjour', $mail, $headers)){
        wp_send_json('success');
    }else{
        wp_send_json('error');
    }       
    */
}
die();
}

Javascript code :

$(document).on('submit','.w-sender',function(e){
            e.preventDefault();
            var error = false,
                form = $(this).closest('form'),
                busy = null;
            
            
            var formSerialize = form.serialize();
            if(!error){
                if (busy)
                    busy.abort();
                
                busy = $.ajax({
                    url : ajaxSender.ajax_url,
                    data : {
                        action : 'mail_form',
                        nonce: ajaxSender.nonce,
                        formFields : formSerialize,
                    },
                    type : 'POST',
                    success : function(response){
                            $('.w-sender').addClass('sender_bg_success');
                            $('.v-sender').addClass('dnone-i');
                            $('.v-sender').detach().appendTo('#c0-global');
                            $('.close-sender').remove();
                            $('.mailForm_container').replaceWith(div class='success_mailForm'Parfait ! /div);
                            setTimeout(function() {
                                form.remove();
                            }, 4000);
                    },
                    error : function(response){  
                
                    }
                  })
            }
            return false;
        });

Topic wp-mail ajax notifications forms Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.