Title: Send conditional come back emails
Last modified: March 27, 2022

---

# Send conditional come back emails

 *  Resolved [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * (@honoluluman)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/)
 * Hello,
 * Thank you for this great plugin.
    Would it be possible to send to only specific
   role of users. For example, only to editors.Is there any add_filter for that?
 * Thank you very much

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/send-conditional-come-back-emails/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/send-conditional-come-back-emails/page/2/?output_format=md)

 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15500657)
 * The plugin itself doesn’t have this feature.
 * However this can be accomplished by using a core filter hook:
 * [https://developer.wordpress.org/reference/hooks/pre_wp_mail/](https://developer.wordpress.org/reference/hooks/pre_wp_mail/)
 * Check the subject of the Come Back! email and short-circuit wp_mail if the user
   is not an editor.
 * I hope this helps!
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15500777)
 * Like this:
 *     ```
       /**
        * Filter the email based on Come Back! email and user role.
        * 
        * @param null|bool $return Short-circuit return value.
        * @param array     $atts Array of the wp_mail() arguments.
        *
        * @return null|bool Short-circuit return value..
        */
       function come_back_conditional_emails( $return, $atts ) {
   
       	// Return if it's not a come back! email.
       	if ( $atts['subject'] !== get_option( 'come_back_email_subject', esc_html__( 'Come Back!', 'come-back' ) ) ) {
       		return true;
       	}
   
       	$user = get_user_by( 'email', $atts['to'] );
   
       	$user_roles = ! empty( $user->roles ) ? $user->roles : array();
   
       	if ( ! in_array( 'editor', $user_roles ) ) {
       		return false;
       	}
   
       	// Anyway.
       	return true;
       }
       add_filter( 'pre_wp_mail', 'come_back_conditional_emails', PHP_INT_MAX, 2 );
       ```
   
 * To test go to Settings > Come Back! and send a sample email to the editor, make
   sure the email is sent. Send sample email to other users, make sure the email
   isn’t sent.
 *  Thread Starter [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * (@honoluluman)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15501348)
 * Hello [@sanzeeb3](https://wordpress.org/support/users/sanzeeb3/)
 * Thank you very much for the help.
    So in my case should i do ?
 *     ```
       // Return if it's not a come back! email.
       	if ( $atts['subject'] !== get_option( 'come_back_email_subject', esc_html__( 'The text of my emails subject' ) ) ) {
       		return true;
       	}
       ```
   
 * |
 * In case i would like to include 2 roles instead of 1 should i do?
 *     ```
       if ( ! in_array( 'editor', 'subscriber', $user_roles ) ) {
       		return false;
       	}
       ```
   
 * thank you for your time 🙂
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15501510)
 * In that case you can do something like:
 *     ```
       /**
        * Filter the email based on Come Back! email and user role.
        * 
        * @param null|bool $return Short-circuit return value.
        * @param array     $atts Array of the wp_mail() arguments.
        *
        * @return null|bool Short-circuit return value..
        */
       function come_back_conditional_emails( $return, $atts ) {
   
       	// Return if it's not a come back! email.
       	if ( $atts['subject'] !== get_option( 'come_back_email_subject', esc_html__( 'Come Back!', 'come-back' ) ) ) {
       		return true;
       	}
   
       	$user = get_user_by( 'email', $atts['to'] );
   
       	$user_roles = ! empty( $user->roles ) ? $user->roles : array();
   
       	foreach( $user_roles as $role ) {
   
       		if ( in_array( $role, [ 'subscriber', 'editor' ] ) ) {
       			return true;
       		}		
       	}
   
       	// Otherwise.
       	return false;
       }
       add_filter( 'pre_wp_mail', 'come_back_conditional_emails', PHP_INT_MAX, 2 );
       ```
   
 * with no other changes in the subject because get_option( ‘come_back_email_subject’)
   will automatically fetch the Come Back! subject you’ve set.
    -  This reply was modified 4 years, 1 month ago by [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/).
    -  This reply was modified 4 years, 1 month ago by [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/).
 *  Thread Starter [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * (@honoluluman)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15502803)
 * Hello [@sanzeeb3](https://wordpress.org/support/users/sanzeeb3/)
 * Thank you very much for your code.
    I have tested it and it seems that it doesn’t
   work. If i send a test email without the custom code, the email is send. If i
   enable the code, i don’t get any email.
 * Any ideas?
 * Thank you again for your time.
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15502818)
 * With the custom code, the emails are only sent to subscribers and editors. Did
   you try sending email to them?
 *  Thread Starter [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * (@honoluluman)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15502830)
 * yes, i have tried them. I get an admin notification that the email was send. 
   but i never receive it. If i try some other role, i get an admin notification
 * _Email not sent. Please check if your site can send emails._
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15502923)
 * It sounds like you’re facing the general email delivery issues.
 * The emails are sent from your site as expected. The delivery part is outside 
   of the context of the plugin.
 * To improve the deliverabilty, I recommend setting up SMTP plugins.
 * I hope this clarifies!
 *  Thread Starter [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * (@honoluluman)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15502947)
 * It is not a deliverabilty issue. I am on SMTP with deliverabilty 9/10 from [https://www.mail-tester.com/](https://www.mail-tester.com/).
   All other emails are working normal.
 * If i don’t use the code, i get the test email normal.
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15502970)
 * Thanks for the update. I’ll test and get back to you. I’m away from desk currently.
 * Have a good one! 🙂
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15503077)
 * [@honoluluman](https://wordpress.org/support/users/honoluluman/) – I realized
   the mistake in the code. Here’s the updated code.
 *     ```
       /**
        * Filter the email based on Come Back! email and user role.
        * 
        * @param null|bool $return Short-circuit return value.
        * @param array     $atts Array of the wp_mail() arguments.
        *
        * @return null|bool Short-circuit return value..
        */
       function come_back_conditional_emails( $return, $atts ) {
   
       	// Return if it's not a come back! email.
       	if ( $atts['subject'] !== get_option( 'come_back_email_subject', esc_html__( 'Come Back!', 'come-back' ) ) ) {
       		return null;
       	}
   
       	$user = get_user_by( 'email', $atts['to'] );
   
       	$user_roles = ! empty( $user->roles ) ? $user->roles : array();
   
       	foreach( $user_roles as $role ) {
   
       		if ( in_array( $role, [ 'subscriber', 'editor' ] ) ) {
       			return null;
       		}		
       	}
   
       	// Otherwise.
       	return false;
       }
       add_filter( 'pre_wp_mail', 'come_back_conditional_emails', PHP_INT_MAX, 2 );
       ```
   
 * I hope this works!
 *  Thread Starter [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * (@honoluluman)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15503879)
 * Hello [@sanzeeb3](https://wordpress.org/support/users/sanzeeb3/)
 * Thank you for the fast reply.
    Unfortunately this code also not working. It returns
   just a msg _Email not sent. Please check if your site can send emails._
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15505447)
 * [@honoluluman](https://wordpress.org/support/users/honoluluman/) – I’m out of
   ideas what could it be, but I’ll circle to this later.
 *  Thread Starter [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * (@honoluluman)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15522352)
 * Hello [@sanzeeb3](https://wordpress.org/support/users/sanzeeb3/) ,
 * Any ideas how it could work? 🙂
 *  Plugin Author [Sanjeev Aryal](https://wordpress.org/support/users/sanzeeb3/)
 * (@sanzeeb3)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/#post-15522836)
 * Hi [@honoluluman](https://wordpress.org/support/users/honoluluman/),
 * That snippet worked for me. I’m not sure what went wrong for you. Let’s debug
   by following these steps:
 * 1) Turn debug log: [https://www.wpbeginner.com/wp-tutorials/how-to-set-up-wordpress-error-logs-in-wp-config/](https://www.wpbeginner.com/wp-tutorials/how-to-set-up-wordpress-error-logs-in-wp-config/)
 * 2) Add the code snippet:
 *     ```
       /**
        * Filter the email based on Come Back! email and user role.
        * 
        * @param null|bool $return Short-circuit return value.
        * @param array     $atts Array of the wp_mail() arguments.
        *
        * @return null|bool Short-circuit return value..
        */
       function come_back_conditional_emails( $return, $atts ) {
   
       	// Return if it's not a come back! email.
       	if ( $atts['subject'] !== get_option( 'come_back_email_subject', esc_html__( 'Come Back!', 'come-back' ) ) ) {
       		return null;
       	}
   
       	$user = get_user_by( 'email', $atts['to'] );
   
       	error_log( print_r( $user, true ) );
   
       	$user_roles = ! empty( $user->roles ) ? $user->roles : array();
   
       	foreach( $user_roles as $role ) {
   
       		if ( in_array( $role, [ 'subscriber', 'editor' ] ) ) {
       			return null;
       		}		
       	}
   
       	// Anyway.
       	return false;
       }
       add_filter( 'pre_wp_mail', 'come_back_conditional_emails', PHP_INT_MAX, 2 );
       ```
   
 * 3) Send an email to the editor or subscriber from Come Back! > Settings > Send
   Sample Email.
 * 4) Check debug.log
 * 5) Share the logged information, here.
 * Thank you!

Viewing 15 replies - 1 through 15 (of 16 total)

1 [2](https://wordpress.org/support/topic/send-conditional-come-back-emails/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/send-conditional-come-back-emails/page/2/?output_format=md)

The topic ‘Send conditional come back emails’ is closed to new replies.

 * ![](https://ps.w.org/come-back/assets/icon-256x256.png?rev=2434888)
 * [Come Back!](https://wordpress.org/plugins/come-back/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/come-back/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/come-back/)
 * [Active Topics](https://wordpress.org/support/plugin/come-back/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/come-back/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/come-back/reviews/)

 * 16 replies
 * 2 participants
 * Last reply from: [honoluluman](https://wordpress.org/support/users/honoluluman/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/send-conditional-come-back-emails/page/2/#post-15523844)
 * Status: resolved