bbp_chunk_emails( array $args = array() )

Automatically splits bbPress emails with many Bcc recipients into chunks.


Description Description

This middleware is useful because topics and forums with many subscribers run into problems with Bcc limits, and many hosting companies & third-party services limit the size of a Bcc audience to prevent spamming.

The default "chunk" size is 40 users per iteration, and can be filtered if desired. A future version of bbPress will introduce a setting to more easily tune this.


Parameters Parameters

$args

(Optional) Original arguments passed to wp_mail().

Default value: array()


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/common/functions.php

function bbp_chunk_emails( $args = array() ) {

	// Get the maximum number of Bcc's per chunk
	$max_num = apply_filters( 'bbp_get_bcc_chunk_limit', 40, $args );

	// Look for "bcc: " in a case-insensitive way, and split into 2 sets
	$match       = '/^bcc: (\w+)/i';
	$old_headers = preg_grep( $match, $args['headers'], PREG_GREP_INVERT );
	$bcc_headers = preg_grep( $match, $args['headers'] );

	// Bail if less than $max_num recipients
	if ( empty( $bcc_headers ) || ( count( $bcc_headers ) < $max_num ) ) {
		return $args;
	}

	// Reindex the headers arrays
	$old_headers = array_values( $old_headers );
	$bcc_headers = array_values( $bcc_headers );

	// Break the Bcc emails into chunks
	foreach ( array_chunk( $bcc_headers, $max_num ) as $i => $chunk ) {

		// Skip the first chunk (it will get used in the original wp_mail() call)
		if ( 0 === $i ) {
			$first_chunk = $chunk;
			continue;
		}

		// Send out the chunk
		$chunk_headers = array_merge( $old_headers, $chunk );

		// Recursion alert, but should be OK!
		wp_mail(
			$args['to'],
			$args['subject'],
			$args['message'],
			$chunk_headers,
			$args['attachments']
		);
	}

	// Set headers to old headers + the $first_chunk of Bcc's
	$args['headers'] = array_merge( $old_headers, $first_chunk );

	// Return the reduced args, with the first chunk of Bcc's
	return $args;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.