Filter uncanny-automator

automator_email_to

Filters the recipient email address before an email is sent.

add_filter( 'automator_email_to', $callback, 10, 2 );

Description

Fires before an email action sends to dynamically modify the recipient list. Developers can use this hook to change the "To" field of emails sent by the Automator. Accepts the current email recipient(s) and the recipe action object.


Usage

add_filter( 'automator_email_to', 'your_function_name', 10, 2 );

Parameters

$this (mixed)
This parameter represents the `$this` object within the `send_email` method, providing access to the email action's properties and methods.
$this (mixed)
This parameter represents the email address of the recipient.

Return Value

The filtered value.


Examples

/**
 * Example of modifying the recipient email address for an automator email.
 *
 * This filter allows you to dynamically change the 'To' address of an email
 * sent by the Automator plugin, perhaps based on user roles, post types,
 * or other conditions within your WordPress site.
 *
 * @param string|array $recipient The current recipient email address or an array of addresses.
 * @param object       $action    The current Automator action object, which can provide context.
 * @return string|array The modified recipient email address or array of addresses.
 */
add_filter( 'automator_email_to', function( $recipient, $action ) {

    // Example: If the recipient is a specific admin email, change it to a different admin.
    // This assumes you have a way to identify specific automator actions or contexts.
    // For demonstration, let's say we want to change the recipient for an action
    // that has a specific metadata key set.
    if ( $action && method_exists( $action, 'get_recipe_id' ) && $action->get_recipe_id() === 123 ) { // Replace 123 with a real recipe ID if known
        // Check if the current recipient is the default admin email (example)
        if ( is_string( $recipient ) && $recipient === get_option( 'admin_email' ) ) {
            // Change the recipient to a specific secondary admin email
            return '[email protected]';
        }
    }

    // Example: If the recipient is an array, add another address to it.
    if ( is_array( $recipient ) ) {
        $recipient[] = '[email protected]';
        return array_unique( $recipient ); // Ensure no duplicates
    }

    // If no specific conditions are met, return the original recipient.
    return $recipient;

}, 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/core/lib/recipe-parts/actions/trait-action-helpers-email.php:402

public function send_email() {

		$header_raw = array(
			'from'      => $this->get_from(),
			'from_name' => $this->get_from_name(),
			'cc'        => $this->get_cc(),
			'bcc'       => $this->get_bcc(),
			'reply_to'  => $this->get_reply_to(),
			'content'   => $this->get_content_type(),
			'charset'   => $this->get_charset(),
		);

		// Process the attachments.
		$attachments = $this->get_attachments();

		$local_attachments = array();

		foreach ( $attachments as $attachment_url ) {
			if ( ! empty( $attachment_url ) ) {

				$attachment          = $this->process_attachment( $attachment_url );
				$local_attachments[] = $attachment;

				if ( is_wp_error( $attachment ) ) {
					$this->set_error_message( $attachment->get_error_message() );
					return false;
				}
			}
		}

		$headers     = apply_filters( 'automator_email_headers', Automator()->helpers->email->headers( $header_raw ), $this );
		$to          = apply_filters( 'automator_email_to', $this->get_to(), $this );
		$subject     = apply_filters( 'automator_email_subject', stripslashes( $this->get_subject() ), $this );
		$body        = apply_filters( 'automator_email_body', stripslashes( $this->get_body() ), $this );
		$attachments = apply_filters( 'automator_email_attachments', $this->get_attachments(), $this );
		$pass        = array(
			'to'         => $to,
			'subject'    => $subject,
			'body'       => $body,
			'headers'    => $headers,
			'attachment' => $local_attachments,
			'is_html'    => $this->is_is_html(),
		);

		if ( true === apply_filters( 'automator_send_email', true, $pass, $this->get_actions_data(), $this ) ) {
			$mailed = Automator()->helpers->email->send( $pass );
		} else {
			$error = Automator()->error;
			$error->add_error( 'wp_mail', esc_attr__( 'Email action is disabled by `automator_send_email` filter.', 'uncanny-automator' ), $pass );
			$mailed = $error;
		}

		if ( is_automator_error( $mailed ) ) {
			$errors = $mailed->get_messages( 'wp_mail' ) + $mailed->get_messages( 'wp_mail_to' );
			if ( $errors ) {
				foreach ( $errors as $error ) {
					$this->set_error_message( $error );
				}
			}

			return false;
		}

		return $mailed;
	}


Scroll to Top