Filter
uncanny-automator
automator_email_attachments
Filters the email attachments before they are sent.
add_filter( 'automator_email_attachments', $callback, 10, 2 );
Description
Filters the email attachments for automated emails. Developers can use this hook to add, remove, or modify attachments before an email is sent. This filter is applied just before the email content is finalized.
Usage
add_filter( 'automator_email_attachments', 'your_function_name', 10, 2 );
Parameters
-
$this(mixed) - This parameter is a reference to the current instance of the action object.
-
$this(mixed) - This parameter contains the email recipient's address.
Return Value
The filtered value.
Examples
add_filter( 'automator_email_attachments', 'my_custom_email_attachments', 10, 2 );
/**
* Adds custom attachments to an Automator email.
*
* This function allows you to conditionally add custom files as attachments
* to emails sent by the Automator plugin. For example, you might want to
* attach a user-specific report or a PDF generated based on the trigger data.
*
* @param array $attachments The existing array of attachments.
* @param object $recipe_action The current recipe action object, which can be used to access trigger data or other contextual information.
* @return array The modified array of attachments, including any custom ones added.
*/
function my_custom_email_attachments( $attachments, $recipe_action ) {
// Example: Conditionally add a specific file based on a user meta value.
// Assuming $recipe_action has a method to get the user ID or related data.
// Replace 'get_user_id_from_recipe_action' with the actual method if it exists,
// or retrieve it from the trigger data if accessible.
$user_id = $recipe_action->get_user_id_from_recipe_action(); // Placeholder, adjust as needed
if ( $user_id && get_user_meta( $user_id, 'attach_special_report', true ) === 'yes' ) {
// Define the path to your custom attachment.
// This should be a valid file path on your server.
$custom_attachment_path = WP_CONTENT_DIR . '/uploads/custom-reports/special-report-' . $user_id . '.pdf';
// Check if the file exists before adding it.
if ( file_exists( $custom_attachment_path ) ) {
$attachments[] = $custom_attachment_path;
}
}
// Example: Add a generic file if a certain condition is met.
// This could be based on the recipe or trigger itself.
// For demonstration, let's say we always attach a company brochure.
$company_brochure_path = get_template_directory() . '/assets/company-brochure.pdf';
if ( file_exists( $company_brochure_path ) ) {
$attachments[] = $company_brochure_path;
}
return $attachments;
}
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:405
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;
}