Redirect to another page after submission using wp_mail

I'm trying to redirect to user to another page after the form has been submitted using wp_mail.

// Hooking up our functions to WordPress filters
add_filter( 'wp_mail_from', 'wpb_sender_email' );
add_filter( 'wp_mail_from_name', 'wpb_sender_name' );

// Email Sender
add_action('wp_ajax_mySendEmail', 'mySendEmail');
add_action('wp_ajax_nopriv_mySendEmail', 'mySendEmail');
function mySendEmail() {
    $name        = $_POST['name'];
    $email       = $_POST['email'];
    $contact_no  = $_POST['contact_no'];
    $debts_value  = $_POST['debts_value'];
    $how_much  = $_POST['how_much'];
    $employment_type  = $_POST['employment_type'];
    $page_id  = $_POST['page_id'];
    $keyword  = $_POST['keyword'];
    $to = [ "[email protected]" ];
    $subject = '';
    $mybody = '';
    $mybody .='table';
    $mybody .='';
    $mybody .='pThe following information has been submitted via your website./p';
    $mybody .='trtdbHow/b/tdtd'.$how_much.'/td/tr';
    $mybody .='trtdbHow/b/tdtd'.$debts_value.'/td/tr';
    $mybody .='trtdb/b/tdtd'.$employment_type.'/td/tr';
    $mybody .='trtdbName/b/tdtd'.$name.'/td/tr';
    $mybody .='trtdbEmail/b/tdtd'.$email.'/td/tr';
    $mybody .='trtdbContact No/b/tdtd'.$contact_no.'/td/tr';
    $mybody .='trtdbPage/b/tdtd'.$page_id.'/td/tr';
    $mybody .='trtdbKeyword/b/tdtd'.$keyword.'/td/tr';
    $mybody .='pThank you,/p';
    $mybody .='p/p';
    $mybody .='/table';
    $body = $mybody;
    $headers = array('Content-Type: text/html; charset=UTF-8');
    $sendStatus = wp_mail( $to, $subject, $body, $headers );

    if ( $sendStatus ){
        wp_redirect( '/thank-you/', 301);
        exit;
    }

    // print_r($sendStatus); die;
}

It just dosent seem to redirect, not sure what im doing wrong?

Topic phpmailer wp-mail php Wordpress

Category Web


I think home url not found. And make sure this condition true. if ( $sendStatus ){ }

Please try like this.

<?php wp_redirect( home_url('/thank-you/', 301) ); exit; ?>

About

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