0

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" />
<title>Assistance</title>
</head>
<body style="margin: 0;padding: 0;min-width: 100% !important;background:#D9D08A;">
<table width="100%"  border="0" cellpadding="10" cellspacing="0" class="bgColor"><tr><td>
<table class="content" align="center" cellpadding="0" cellspacing="0" border="0"><tr><td>
<div class="project floatL"><img src="https://www.monsite.com/wp-content/uploads/logo-titre-black.png" class="lgo">
<div class="subtitle_logo">Question pour </div>
        </div>
        <div class="ref_destinataire floatR">
          <img src="logo.png" class="lgo"><span class="title_activity"><br /> Activity</span>
        </div>
      </td>
    </tr>
    <tr>
      <td>
        <div class="title_para"> Le message </div>
        <div class="info_contact">' . $message .'</div>
                         <div class="title_para">Informations 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;
        });
3
  • 1
    If you coded the form yourself then the code to send a response would just go with the rest of your code that handles the form. What’s your code so far? Nov 22, 2021 at 14:58
  • use the edit link to modify your question to include the full code for the form your question is asking about, both PHP and javascript. It's not possible to answer this without seeing the code
    – Tom J Nowell
    Nov 22, 2021 at 15:20
  • I added php and javascript code in my question, but I still haven't found a wordpress function to allow sending an automatic answer to the sender to confirm I well received his message. Which parameters can I use and add before or inside wp_mail() to allow that ?
    – imagIne
    Nov 23, 2021 at 11:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.