Support

Account

Home Forums General Issues wp_mail not working in acf/pre_save_post Reply To: wp_mail not working in acf/pre_save_post

  • 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.