/**
 * Sends notification emails for new topics to subscribed forums
 *
 * Gets new post's ID and check if there are subscribed users to that forum, and
 * if there are, send notifications
 *
 * Note: in bbPress 2.6, we've moved away from 1 email per subscriber to 1 email
 * with everyone BCC'd. This may have negative repercussions for email services
 * that limit the number of addresses in a BCC field (often to around 500.) In
 * those cases, we recommend unhooking this function and creating your own
 * custom emailer script.
 *
 * @since 2.5.0 bbPress (r5156)
 *
 * @param int $topic_id ID of the newly made reply
 * @param int $forum_id ID of the forum for the topic
 * @param mixed $anonymous_data Array of anonymous user data
 * @param int $topic_author ID of the topic author ID
 *
 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
 * @uses bbp_get_topic_id() To validate the topic ID
 * @uses bbp_get_forum_id() To validate the forum ID
 * @uses bbp_is_topic_published() To make sure the topic is published
 * @uses bbp_get_forum_subscribers() To get the forum subscribers
 * @uses bbp_get_topic_author_display_name() To get the topic author's display name
 * @uses do_action() Calls 'bbp_pre_notify_forum_subscribers' with the topic id,
 *                    forum id and user id
 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_message' with the
 *                    message, topic id, forum id and user id
 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_title' with the
 *                    topic title, topic id, forum id and user id
 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_headers'
 * @uses get_userdata() To get the user data
 * @uses wp_mail() To send the mail
 * @uses do_action() Calls 'bbp_post_notify_forum_subscribers' with the topic,
 *                    id, forum id and user id
 * @return bool True on success, false on failure
 */
function 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);
    // Setup the From header
    $headers = array('From: ' . get_bloginfo('name') . ' <' . $from_email . '>');
    // 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;
    }
    // Loop through users
    foreach ((array) $user_ids as $user_id) {
        // Don't send notifications to the person who made the post
        if (!empty($topic_author) && (int) $user_id === (int) $topic_author) {
            continue;
        }
        // Get email address of subscribed user
        $headers[] = 'Bcc: ' . get_userdata($user_id)->user_email;
    }
    /** Send it ***************************************************************/
    // Custom headers
    $headers = apply_filters('bbp_subscription_mail_headers', $headers);
    $to_email = apply_filters('bbp_subscription_to_email', $no_reply);
    do_action('bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids);
    // Send notification email
    wp_mail($to_email, $subject, $message, $headers);
    do_action('bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids);
    return true;
}
 /**
  * @covers ::bbp_get_do_not_reply_address
  */
 public function test_bbp_get_do_not_reply_address()
 {
     $_SERVER['SERVER_NAME'] = 'example.org';
     $address = bbp_get_do_not_reply_address();
     $this->assertEquals('*****@*****.**', $address);
     $_SERVER['SERVER_NAME'] = 'www.example.org';
     $address = bbp_get_do_not_reply_address();
     $this->assertEquals('*****@*****.**', $address);
     $_SERVER['SERVER_NAME'] = 'subdomain.example.org';
     $address = bbp_get_do_not_reply_address();
     $this->assertEquals('*****@*****.**', $address);
     $_SERVER['SERVER_NAME'] = 'www.subdomain.example.org';
     $address = bbp_get_do_not_reply_address();
     $this->assertEquals('*****@*****.**', $address);
     // Reset server name.
     $_SERVER['SERVER_NAME'] = 'example.org';
 }
 private function get_headers()
 {
     // Get the noreply@sitename.com email address
     $no_reply = bbp_get_do_not_reply_address();
     $from_email = apply_filters('bbp_notify_admin_from_email', $no_reply);
     // Setup the From header
     $headers = array('From: ' . get_bloginfo('name') . ' <' . $from_email . '>');
     return $headers;
 }