Filter
uncanny-automator
automator_send_email
Filters the email sending process within the Automator plugin, allowing modification of email data before it is sent.
add_filter( 'automator_send_email', $callback, 10, 3 );
Description
Allows developers to intercept and modify email sending logic within the Automator plugin. Before an email is sent, this filter hook provides access to the email's parameters, enabling modifications to the subject, body, headers, attachments, and HTML status. Use this hook to customize email content or conditionally prevent emails from being sent.
Usage
add_filter( 'automator_send_email', 'your_function_name', 10, 3 );
Parameters
-
$pass(mixed) - This parameter is a boolean value indicating whether the email should be sent.
-
$this(mixed) - This parameter likely holds a boolean value indicating whether the email should be sent.
-
$this(mixed) - This parameter likely contains the instance of the object that is currently executing the `send_email` method, providing access to its properties and methods.
Return Value
The filtered value.
Examples
/**
* Example of how to hook into the 'automator_send_email' filter to conditionally disable email sending.
*
* In this example, we'll prevent emails from being sent if a specific user role ('contributor')
* is associated with the current automation action.
*
* @param bool $send_email Whether to send the email. Default is true.
* @param array $pass The parameters array intended for wp_mail().
* @param array $actions_data The data associated with the automation actions.
* @param object $current_object The current object instance (likely an action or recipe object).
* @return bool Returns false to prevent the email from being sent, otherwise returns the original $send_email value.
*/
add_filter( 'automator_send_email', function ( $send_email, $pass, $actions_data, $current_object ) {
// Check if the current object has a method to get the associated user and if that user has a specific role.
// This is a hypothetical check, and the actual way to access user data might differ
// depending on the structure of $current_object.
if ( method_exists( $current_object, 'get_user' ) && ! empty( $current_object->get_user() ) ) {
$user = $current_object->get_user();
if ( in_array( 'contributor', (array) $user->roles ) ) {
// If the user has the 'contributor' role, prevent the email from being sent.
return false;
}
}
// Otherwise, allow the email to be sent by returning the original value.
return $send_email;
}, 10, 4 );
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:415
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;
}