Site icon Hip-Hop Website Design and Development

How to auto send email when publishing a custom post type using ACF field values

I would like to automatically send out an email when a custom post type gets published using the ACF field values. I go through the solution here. but this send email to all subscribed users, In my case, I need to send email only to the user that fills out the contact form. the code snippet is here:

    //Send Email
    function send_mails_on_publish( $new_status, $old_status, $post )
        {
        if ( 'publish' !== $new_status or 'publish' === $old_status
            or 'praelegal-enquires' !== get_post_type( $post ) )
            return;
        
        $subscribers = get_users( array ( 'role' => 'subscriber' ) );
        $emails = array ();
        
        foreach ( $subscribers as $subscriber )
            $emails[] = $subscriber->user_email;
        print_r(get_post_custom($post->ID));
        echo "Testing Purpose $meme";
        
        
        foreach ( $subscribers as $subscriber )
            $emails[] = $subscriber->user_email;

        $body = sprintf( 'Hey there is a new entry!
            See <%s>',
            get_permalink( $post )
            );


        wp_mail( $emails, 'New entry!', $body );
        }
    add_action( 'transition_post_status', 'send_mails_on_publish', 10, 3 );
    //End Send Email

My website has a contact form that publishes its data to a custom post type. I want to send an email to the user’s email that fills the contact form. I can access the Post itself using the $post variable, but I can not access the ACF email field value from that post. this is the custom post type JSON response.


{
    "id": 300,
    "date": "2021-12-06T15:25:51",
    "date_gmt": "2021-12-06T10:55:51",
    "guid": {
        "rendered": "http://wpbackend.navisa.af/praelegal-enquires/asking-for-praelegal-services42/"
    },
    "modified": "2021-12-06T15:25:51",
    "modified_gmt": "2021-12-06T10:55:51",
    "slug": "asking-for-praelegal-services42",
    "status": "publish",
    "type": "praelegal-enquires",
    "link": "http://wpbackend.navisa.af/praelegal-enquires/asking-for-praelegal-services42/",
    "title": {
        "rendered": "Asking for Praelegal Services42"
    },
    "content": {
        "rendered": "",
        "protected": false
    },
    "featured_media": 0,
    "template": "",
    "acf": {
        "name": "emal",
        "email": "yesrebosmani@gmail.com",
        "message": "hi there!"
    },
    "_links": {
        "self": [
            {
                "href": "http://wpbackend.navisa.af/wp-json/wp/v2/praelegal-enquires/300"
            }
        ],
        "collection": [
            {
                "href": "http://wpbackend.navisa.af/wp-json/wp/v2/praelegal-enquires"
            }
        ],
        "about": [
            {
                "href": "http://wpbackend.navisa.af/wp-json/wp/v2/types/praelegal-enquires"
            }
        ],
        "wp:attachment": [
            {
                "href": "http://wpbackend.navisa.af/wp-json/wp/v2/media?parent=300"
            }
        ],
        "curies": [
            {
                "name": "wp",
                "href": "https://api.w.org/{rel}",
                "templated": true
            }
        ]
    }
}

I need to get access to the ACF email field and send out an email. I really need this, I googled a lot but I can’t fix it, any help would be really appreciated.