Support

Account

Home Forums General Issues wp_mail not working in acf/pre_save_post

Solved

wp_mail not working in acf/pre_save_post

  • I am try to send a confirmation to the user once they used the contact form.

    Below is what I have in the functions.php:

    
    function my_pre_save_post( $post_id ) {
    
    	// check if this is to be a new post
    	if( $post_id != 'new_post' ) {
    
    		return $post_id;
    
    	}
    		// Create a new post
        $post = array(
            'id' => $post_id,
            'post_status'  => 'publish',
            'post_title' => $_POST['acf']['_post_title'],
            'email' => $_POST['acf']['field_5d49dcb49a31f'],
            'bedrag' => $_POST['acf']['field_5d49dd749a321'],
            'periodiek' => $_POST['acf']['field_5d49de569a322'],
        );
        
        // // insert the post
        $post_id = wp_insert_post( $post ); 
        
        add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
        $name = get_field('post_title', $post_id);
        $email = get_field('email', $post_id);
        $body_text = get_field('email_tekst', 'option');
        
        $to = $name . ' <' . $email . '>' . "\r\n";
        $headers = array('Content-Type: text/html; charset=UTF-8', 'From: Some name <[email protected]> <noreply@'.$_SERVER['HTTP_HOST'].'>');
        $subject = 'Bedankt voor uw donatie: ' . get_field('bedrag', $post_id) . ' ' . get_field('periodiek', $post_id);
        $body = $body_text;
        
        wp_mail($to, $subject, $body, $headers );
        
        remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
    	// return the new ID
    	return $post_id;
    
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post');
    

    I am not receiving any email, but there is also no error, I also have a debug on the wp_mail, log doesn’t show any error.

    Please help.

    BTW, I am using a smpt plugin to send email via google api. That usually works.

  • First, you are inserting meta values when inserting a post incorrectly, see this https://wordpress.stackexchange.com/questions/218131/wp-insert-post-add-meta-input

    Second, acf/pre_save_post runs before ACF has updated any values, these lines are probably returning nothing.

    
    
        $name = get_field('post_title', $post_id);
        $email = get_field('email', $post_id);
        $body_text = get_field('email_tekst', 'option');
    

    no email is sent because $email has no value?

    another issue could be the post ID you set when calling acf_form. Does it match
    if( $post_id != 'new_post' ) {

    You should probably be using an acf/save_post action here rather than an acf/pre_save_post action.

  • @hube2, thanks for your reply.

    The saving works, so thats not the issue.

    I also tried using: $_POST[‘acf’][‘field_5d49dcb49a31f’] instead of get_field(’email’, $post_id), makes no difference.

    I am out of ideas at the moment.

    Maybe the placement of the code is not correct?

    Cheers

  • I also have trying to set fixed value for the wp_mail and I am using a plugin “WP Mail SMTP”, which works as I can send a test email.

    So even with the fixed value the wp_mail won’t send an email.

  • Anybody an idea? I am still struggling with this.

  • Ok, I know what was going wrong.

    I didn’t used the correct priority.

    Now using the following code:

    function wpdocs_set_html_mail_content_type() {
        return 'text/html';
    }
    
    add_action('acf/save_post' , 'my_save_post', 15);
    function my_save_post($post_id) {
    
    	if( get_post_type($post_id) !== 'donations' ) {
    		return;
    	}
    
    	add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    	
    	$name = get_field('post_title', $post_id);
    	$email = get_field('email', $post_id);
    	$body_text = get_field('email_tekst', 'option');
    
    	$to = $email;
    	$headers = array('From: some name <some email> <noreply@'.$_SERVER['HTTP_HOST'].'>');
    	$subject = 'Bedankt voor uw donatie: ' . get_field('bedrag', $post_id) . ' ' . get_field('periodiek', $post_id);
    	$message = $body_text;
    
    	wp_mail($to, $subject, $message, $headers );
    
    	remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    } 

    The priority is now 15, so runs after the save was done.

  • Is there anything else in your page code?

    Actually I have the following code in page-contact.php:

    <?php acf_form(array(
    										'id'			=> '138',
    										'post_id'		=> 'new_post',
    										'updated_message'	=> 'Success!',
    										'new_post'		=> array(
    											'post_type'		=> 'contact',
    											'post_status'	=> 'draft',
    											'post_title'	=>	$userfirst . ' ' . $userlast .' <'.$useremail.'>'
    										),
    										'return' => home_url() . '/thank-you',
    										'submit_value'  => 'Send'
    									)); ?>

    In my functions.php I pasted your code, just changed to static text to test if it’s working, but without success. My host doesn’t allow mail() but I am using SMTP and it’s sending fine.

    Code:

    function wpdocs_set_html_mail_content_type() {
        return 'text/html';
    }
    
    add_action('acf/save_post' , 'my_save_post', 15);
    function my_save_post($post_id) {
    
        if( get_post_type($post_id) !== 'contact' ) {
            return;
        }
    
        add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
        
        $name = get_field('post_title', $post_id);
        // $body_text = get_field('email_tekst', 'option');
    
        $to = '[email protected]';
        $headers = array('From: some name <some email> <noreply@'.$_SERVER['HTTP_HOST'].'>');
        $subject = 'Test!';
        $message = 'Test!';
    
        wp_mail($to, $subject, $message, $headers );
    
        remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    }

    domain.com -> client domain

    If anynone could help it would be great! Stuck in it a few days. Thanks

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘wp_mail not working in acf/pre_save_post’ is closed to new replies.