Support

Account

Home Forums ACF PRO Sending ACF Form to Admin & Users

Unread

Sending ACF Form to Admin & Users

  • Hi,

    I have setup a front end form using ACF which when submitted, sends an email to all users of a specific User Role. It also emails the admin of the website.
    My issue is, that the admin receive multiple emails. ie: if there are 20x users, then admin gets 20x emails, all with the users’ names and details.

    How can I send one email to the users, and a separate email, to the admin? (So that it is admin specific)

    Thanks in advance

    function sendEmail( $admin_email, $post, $post_id, $cat_list, $user_name, $user_email ) {
    	
    		// Email Content
    		$subject = $post->post_title;		
    		$body  = '<p style="margin-bottom: 20px;">Hello ' . $user_name . ',
    ';
    		$body .= 'Your Email address is: ' . $user_email . '';		
    		$body .= '<p style="margin-bottom: 20px;">Can you help answer a question that has been submitted to the Knowledgebase?
    ';		
    		$body .= '<div style="padding: 20px; background-color: #eee; margin-bottom: 40px; border-radius: 6px;">';
    		$body .= '<h5 style="font-style: normal;">Question: ' . $post->post_title . '</h5>';
    		$body .= '<h6 style="font-style: normal;">Category/Categories: ' . $cat_list . '</h6>';
    		$body .= '<hr />';
    		$body .= $post->post_content;
    		$body .= '<a href="'.get_permalink( $post_id ).'">View and answer this question</a>
    ';
    		$body .= '</div>';		
    		$body .= '<p style="margin-bottom: 40px;">Do you have a question for our members?';
    		$body .= '<a href="/knowledgebase/#ask-question-form">Submit your own question</a>
    ';	
    		$body .= '<small>If you object to this question being published on the Knowledge Base, please notify <a href="mailto:[email protected]">[email protected]</a> by email stating your reasons.</small>
    ';
    		$headers = array(
    		 'From: '. get_bloginfo('name') .' <'.$admin_email.'>',
    		 //'To:  '.$admin_email.'<'.$admin_email.'>',
    		 'Bcc: '.$user_name.'<'.$user_email.'>'
    	  );		
    	  
    	  // Send email!
    	  wp_mail($admin_email, $subject, $body, $headers);
    	  
    }
    
    /** ACF Front End Form Settings **/
    
    add_action('acf/save_post', 'my_save_post', 20);
    
    function my_save_post($post_id) {
    	
    	// Bail early if not a knowledgebase post
    	if( get_post_type($post_id) !== 'knowledgebase' ) {		
    		return;
    	}	
    
    	// Bail early if editing in admin
    	if( is_admin() ) {		
    		return;		
    	}	
    	
    	// Variables	
    	$post = get_post($post_id);
    
    	// Get Admin Email Address from General Settings
    	$admin_email = get_bloginfo('admin_email');
    		
    	// Get Category or Categories	
    	$cats_list = array();
    	if ( get_field('kbase_form_categories', $post_id) ) :
    	$cats = get_field('kbase_form_categories', $post_id);
    		foreach ( $cats as $cat ) :
    			$cats_list[] = $cat->name;
    		endforeach;
    	endif;	
    	$cat_list = implode(', ', $cats_list);		
    
    	// Get users and their roles
    	$user_args = array(
    		'role__in' => 'new_role', 
    	  'orderby'  => 'user_nicename',
    	  'order'    => 'ASC'
    	);	
    	$users = get_users($user_args);	
    	
    	foreach ( $users as $user ) :
    		$user_name  = $user->display_name;		
    		$user_email = $user->user_email;
    		sendEmail( $admin_email, $post, $post_id, $cat_list, $user_name, $user_email );
    	endforeach;		
    }
Viewing 1 post (of 1 total)

The topic ‘Sending ACF Form to Admin & Users’ is closed to new replies.