BP_Email::set_to( string|array|int|WP_User $to_address, string $name = '', string $operation = 'replace' )

Set the email’s “to” address and name.

Description

IMPORTANT NOTE: the assumption with all emails sent by (and belonging to) BuddyPress itself is that there will only be a single $to_address. This is to simplify token and templating logic (for example, if multiple recipients, the "unsubscribe" link in the emails will all only link to the first recipient).

To set a single address, the first parameter is the address and the second the name. You can also pass a user ID or a WP_User object.

To set multiple addresses, for each array item, the key is the email address and the value is the name.

Parameters

$to_address

(Required) Either an email address, user ID, WP_User object, or an array containing any combination of the above.

$name

(Optional) If $to_address is a string, this is the recipient's name.

Default value: ''

$operation

(Optional) If "replace", $to_address replaces current setting (default). If "add", $to_address is added to the current setting.

Default value: 'replace'

Return

(BP_Email)

Source

File: bp-core/classes/class-bp-email.php

	public function set_to( $to_address, $name = '', $operation = 'replace' ) {
		$to = ( $operation !== 'replace' ) ? $this->to : array();

		if ( is_array( $to_address ) ) {
			foreach ( $to_address as $address ) {
				$to[] = new BP_Email_Recipient( $address );
			}

		} else {
			$to[] = new BP_Email_Recipient( $to_address, $name );
		}

		/**
		 * Filters the new value of the email's "to" property.
		 *
		 * @since BuddyPress 2.5.0
		 *
		 * @param BP_Email_Recipient[] "To" recipients.
		 * @param string $to_address "To" address.
		 * @param string $name "To" name.
		 * @param string $operation If "replace", $to_address replaced previous recipients. If "add",
		 *                          $to_address was added to the array of recipients.
		 * @param BP_Email $this Current instance of the email type class.
		 */
		$this->to = apply_filters( 'bp_email_set_to', $to, $to_address, $name, $operation, $this );

		return $this;
	}

Changelog

Changelog
Version Description
BuddyPress 2.5.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.