Filter uncanny-automator

automator_email_subject

Filters the email subject before it's sent by the Automator plugin, allowing for dynamic customization.

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

Description

Filters the email subject line before it's sent. Developers can modify the subject text using this hook to customize email notifications triggered by automations. Allows dynamic subject line generation based on recipe data.


Usage

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

Parameters

$this (mixed)
This parameter represents the current instance of the email action object.
$this (mixed)
This parameter holds the email subject line that will be filtered.

Return Value

The filtered value.


Examples

/**
 * Modify the email subject to include a specific prefix for completed recipes.
 *
 * This function checks if the current action is part of a "completed" recipe
 * and adds a "[Completed]" prefix to the email subject if it is.
 *
 * @param string $subject The original email subject.
 * @param object $automator_object The automator object (likely an instance of the class where this filter is applied).
 * @return string The modified email subject.
 */
add_filter( 'automator_email_subject', function( $subject, $automator_object ) {
    // Assuming $automator_object has a method to check if it's a completed recipe action.
    // Replace 'is_completed_recipe_action' with the actual method name if it differs.
    if ( method_exists( $automator_object, 'is_completed_recipe_action' ) && $automator_object->is_completed_recipe_action() ) {
        $subject = '[Completed] ' . $subject;
    }
    return $subject;
}, 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:403

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