Plugin to split subscription emails
-
For my WordPress website I use third party SMTP server with a limit on number of recipients. Is there any plugin that I can use to split messages to limit maximum emails in BCC?
For example, server limit is 50, so if I have 60 subscribers to a forum or topic, I want server to send two emails. One with 50 and the other one with 10 emails.
-
Hi @tananaev ,
It took me some time to look around, but maybe https://wordpress.org/plugins/asyncronous-bbpress-subscriptions/ can help you ?
Thanks for suggestion, but I have actually already implemented it myself. Basically I took original bbpress methods and modified them slightly to split messages by 50 subscribers.
You created/used filters ? Or you just rewrote the functions ?
If you want to share your work, please do.Pascal.
I added functions to “functions.php” in my theme:
I’m not a PHP developer and I’m not a WordPress expert, so I’m sure it’s far from perfect, but here is my code:
function custom_bbp_notify_topic_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0 ) { // Bail if subscriptions are turned off if ( !bbp_is_subscriptions_active() ) { return false; } /** Validation ************************************************************/ $reply_id = bbp_get_reply_id( $reply_id ); $topic_id = bbp_get_topic_id( $topic_id ); $forum_id = bbp_get_forum_id( $forum_id ); /** Topic *****************************************************************/ // Bail if topic is not published if ( !bbp_is_topic_published( $topic_id ) ) { return false; } /** Reply *****************************************************************/ // Bail if reply is not published if ( !bbp_is_reply_published( $reply_id ) ) { return false; } // Poster name $reply_author_name = bbp_get_reply_author_display_name( $reply_id ); /** Mail ******************************************************************/ // Remove filters from reply content and topic title to prevent content // from being encoded with HTML entities, wrapped in paragraph tags, etc... remove_all_filters( 'bbp_get_reply_content' ); remove_all_filters( 'bbp_get_topic_title' ); // Strip tags from text and setup mail data $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) ); $reply_url = bbp_get_reply_url( $reply_id ); $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); // For plugins to filter messages per reply/topic/user $message = sprintf( __( '%1$s wrote: %2$s Post Link: %3$s ----------- You are receiving this email because you subscribed to a forum topic. Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), $reply_author_name, $reply_content, $reply_url ); $message = apply_filters( 'bbp_subscription_mail_message', $message, $reply_id, $topic_id ); if ( empty( $message ) ) { return; } // For plugins to filter titles per reply/topic/user $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id ); if ( empty( $subject ) ) { return; } /** Users *****************************************************************/ // Get the noreply@ address $no_reply = bbp_get_do_not_reply_address(); // Setup "From" email address $from_email = apply_filters( 'bbp_subscription_from_email', $no_reply ); // Get topic subscribers and bail if empty $user_ids = bbp_get_topic_subscribers( $topic_id, true ); // Dedicated filter to manipulate user ID's to send emails to $user_ids = apply_filters( 'bbp_topic_subscription_user_ids', $user_ids ); if ( empty( $user_ids ) ) { return false; } $to_email = apply_filters( 'bbp_subscription_to_email', $no_reply ); do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids ); for ( $i = 0; $i <= count($user_ids); $i++ ) { if ( $i % (50 - 1) === 0 || $i === count($user_ids) ) { if ( $i !== 0 ) { $headers = apply_filters( 'bbp_subscription_mail_headers', $headers ); wp_mail( $to_email, $subject, $message, $headers ); } $headers = array( 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>' ); } if ( $i < count($user_ids) ) { // Don't send notifications to the person who made the post if ( !empty( $reply_author ) && (int) $user_ids[$i] === (int) $reply_author ) { continue; } // Get email address of subscribed user $headers[] = 'Bcc: ' . get_userdata( $user_ids[$i] )->user_email; } } do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids ); return true; } function custom_bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) { // Bail if subscriptions are turned off if ( !bbp_is_subscriptions_active() ) { return false; } /** Validation ************************************************************/ $topic_id = bbp_get_topic_id( $topic_id ); $forum_id = bbp_get_forum_id( $forum_id ); /** * Necessary for backwards compatibility * * @see https://bbpress.trac.wordpress.org/ticket/2620 */ $user_id = 0; /** Topic *****************************************************************/ // Bail if topic is not published if ( ! bbp_is_topic_published( $topic_id ) ) { return false; } // Poster name $topic_author_name = bbp_get_topic_author_display_name( $topic_id ); /** Mail ******************************************************************/ // Remove filters from reply content and topic title to prevent content // from being encoded with HTML entities, wrapped in paragraph tags, etc... remove_all_filters( 'bbp_get_topic_content' ); remove_all_filters( 'bbp_get_topic_title' ); // Strip tags from text and setup mail data $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) ); $topic_url = get_permalink( $topic_id ); $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); // For plugins to filter messages per reply/topic/user $message = sprintf( __( '%1$s wrote: %2$s Topic Link: %3$s ----------- You are receiving this email because you subscribed to a forum. Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), $topic_author_name, $topic_content, $topic_url ); $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id ); if ( empty( $message ) ) { return; } // For plugins to filter titles per reply/topic/user $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id ); if ( empty( $subject ) ) { return; } /** User ******************************************************************/ // Get the noreply@ address $no_reply = bbp_get_do_not_reply_address(); // Setup "From" email address $from_email = apply_filters( 'bbp_subscription_from_email', $no_reply ); // Get topic subscribers and bail if empty $user_ids = bbp_get_forum_subscribers( $forum_id, true ); // Dedicated filter to manipulate user ID's to send emails to $user_ids = apply_filters( 'bbp_forum_subscription_user_ids', $user_ids ); if ( empty( $user_ids ) ) { return false; } $to_email = apply_filters( 'bbp_subscription_to_email', $no_reply ); do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); for ( $i = 0; $i <= count($user_ids); $i++ ) { if ( $i % (50 - 1) === 0 || $i === count($user_ids) ) { if ( $i !== 0 ) { $headers = apply_filters( 'bbp_subscription_mail_headers', $headers ); wp_mail( $to_email, $subject, $message, $headers ); } $headers = array( 'From: ' . get_bloginfo( 'name' ) . ' <' . $from_email . '>' ); } if ( $i < count($user_ids) ) { // Don't send notifications to the person who made the post if ( !empty( $topic_author ) && (int) $user_ids[$i] === (int) $topic_author ) { continue; } // Get email address of subscribed user $headers[] = 'Bcc: ' . get_userdata( $user_ids[$i] )->user_email; } } do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); return true; } function custom_bbp_init () { remove_action ('bbp_new_reply', 'bbp_notify_topic_subscribers', 11); add_action ('bbp_new_reply', 'custom_bbp_notify_topic_subscribers', 11, 5); remove_action ('bbp_new_topic', 'bbp_notify_forum_subscribers', 11); add_action ('bbp_new_topic', 'custom_bbp_notify_forum_subscribers', 11, 4); } add_action ('bbp_init', 'custom_bbp_init', 99);
- You must be logged in to reply to this topic.