Filter uncanny-automator

automator_email_body

Filters the email body content before it is sent, allowing for dynamic customization.

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

Description

Fires before the email body content is finalized for an automation action. Developers can modify the email body, for example, to add dynamic content, adjust formatting, or insert specific information before it's sent. The `$this` parameter refers to the email action object.


Usage

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

Parameters

$this (mixed)
This parameter represents the email body content that will be filtered.
$this (mixed)
This parameter represents the sender's email address for the outgoing email.

Return Value

The filtered value.


Examples

add_filter( 'automator_email_body', 'my_custom_email_body_content', 10, 2 );

/**
 * Adds custom content to the email body.
 *
 * This function demonstrates how to modify the email body generated by the Automator plugin.
 * It appends a personalized greeting and a link to the user's account.
 *
 * @param string $body The original email body content.
 * @param object $automator_action The current Automator action object, which contains context like the user ID.
 * @return string The modified email body.
 */
function my_custom_email_body_content( $body, $automator_action ) {
    // Ensure we have a user ID to personalize the email
    $user_id = $automator_action->get_trigger_user_id(); // Assuming get_trigger_user_id() exists and returns the relevant user ID

    if ( $user_id ) {
        $user = get_user_by( 'id', $user_id );
        if ( $user ) {
            $username = $user->display_name;
            $account_url = home_url( '/my-account/' ); // Example: Replace with your actual account page URL

            $custom_content = sprintf(
                '<p>Hi %s,</p><p>Your recent action has been processed. You can review your details at your account page here: <a href="%s">%s</a></p>',
                esc_html( $username ),
                esc_url( $account_url ),
                esc_html( $account_url )
            );

            // Append custom content to the existing body
            $body = $custom_content . $body;
        }
    }

    // You might also want to add some standard closing remarks
    $closing_remarks = '<p>Thank you for using our service!</p>';
    $body .= $closing_remarks;

    return $body;
}

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:404

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