Skip to content

Hooks

Stacko Mail has six filters and three actions. All of them are prefixed stacko_mail_.

Hook Type Use it to
stacko_mail_message Filter Change or block an email before it is sent
stacko_mail_take_over_default Filter Make Stacko Mail handle emails while the mailer is WordPress default
stacko_mail_mailers Filter Add, replace or remove mailers
stacko_mail_log_status Filter Change the status written to the log
stacko_mail_capability Filter Change who can use Stacko Mail
stacko_mail_error_docs_url Filter Add “Learn more” links to error explanations
stacko_mail_after_send Action React to every send attempt
stacko_mail_logged Action React to every logged email
stacko_mail_connection_saved Action React to a connection being saved in the admin

Core’s wp_mail_* filters, phpmailer_init, wp_mail_succeeded and wp_mail_failed keep working too. See Compatibility with core hooks.

All classes are in the Stacko\Mail\Mail namespace and are immutable (readonly properties).

Message

Property Type Notes
$to, $cc, $bcc, $reply_to Address[]
$from Address After wp_mail_from / wp_mail_from_name.
$subject string
$body string
$content_type string e.g. text/html
$charset string
$headers array<string,string> Custom headers only (not From, Cc, Bcc, Reply-To, Content-Type).
$attachments array<int|string,string> File paths; string keys are attachment file names.
$embeds array<int|string,string> Inline images (WordPress 6.9+); keys are Content-IDs.
$source string Plugin or theme slug that called wp_mail(), core, or ''.
$atts array<string,mixed> The original wp_mail() arguments.

Methods: is_html(): bool, all_recipients(): Address[], with_from( Address $from ): Message, with_recipients( Address[] $to ): Message (drops Cc and Bcc).

Address: $email (string), $name (string), domain(): string. Casts to Name <email>, or just the email when there is no name.

Connection: $slot ('primary' or 'backup'), $mailer (mailer ID), $settings (array<string,mixed>).

SendResult: $ok (bool), $mailer (string), $duration_ms (int), $message_id (?string), $error_code (?string, see Error codes), $error_message (?string, secrets redacted), $transcript (string[], redacted protocol log), $temporary (bool, true for errors worth retrying).


Filters a message before Stacko Mail sends it. Return a Message to send it (changed or not), or null to block it. A blocked email is logged as Blocked with error code blocked, and wp_mail() returns false.

apply_filters( 'stacko_mail_message', Message|null $message );
Parameter Type Description
$message Message|null The message, or null if an earlier callback already blocked it.

Returns Message|null. Anything that is not a Message blocks the email.

When it fires: for every wp_mail() call that Stacko Mail handles, that is when the primary connection is not WordPress default, or when stacko_mail_take_over_default returns true. It does not fire for test emails or alert emails, which are sent directly through a connection.

Email control uses this filter at priority 5 to pause (return null) or redirect emails. Callbacks at the default priority 10 receive null while emails are paused, so always check the type.

use Stacko\Mail\Mail\Address;
use Stacko\Mail\Mail\Message;
add_filter( 'stacko_mail_message', function ( $message ) {
if ( ! $message instanceof Message ) {
return $message; // Already blocked.
}
// Send WooCommerce emails from a dedicated address.
if ( 'woocommerce' === $message->source ) {
return $message->with_from( new Address( 'shop@example.com', 'Example Shop' ) );
}
// Never send emails from a noisy plugin.
if ( 'noisy-plugin' === $message->source ) {
return null;
}
return $message;
} );

Whether Stacko Mail should handle an email itself even though the primary connection is WordPress default (or not set up). When true, the email goes through the full Stacko Mail pipeline, including stacko_mail_message, and is sent with PHP mail() by Stacko Mail instead of by core.

apply_filters( 'stacko_mail_take_over_default', bool $take_over );
Parameter Type Description
$take_over bool Default false.

When it fires: on each wp_mail() call while the primary connection is WordPress default or missing. Email control returns true while pausing or redirecting, so it works without a mail service.

// Let a custom stacko_mail_message filter run on a site without a mail service.
add_filter( 'stacko_mail_take_over_default', '__return_true' );

Filters the available mailers. Use it to add a mailer, replace a built-in one, or remove one.

apply_filters( 'stacko_mail_mailers', array $mailers );
Parameter Type Description
$mailers array<string, MailerInterface> Mailers keyed by ID: brevo, sendgrid, mailgun, postmark, smtp, default.

Returns the same shape. Entries that don’t implement Stacko\Mail\Mailers\MailerInterface are dropped. If the callback doesn’t return an array, the built-in mailers are used.

When it fires: once per request, the first time the mailer list is needed. The result is cached for the rest of the request.

A mailer implements Stacko\Mail\Mailers\MailerInterface:

Method Returns Purpose
id() string Stable machine ID. Also used in constant names.
label() string Name shown in the admin.
description() string One line for the provider picker.
schema() array Settings fields. The admin form is built from this.
send( Message $message, array $settings ) SendResult Sends one message.
test( array $settings ) Stacko\Mail\Mailers\ConnectionTest Step-by-step connection test, without sending.
dns_hints( array $settings ) array Optional spf_include (string) and dkim_selectors (string list) for the domain health check.

Each schema field is an array with key, type (text, email, password, number, select, switch or url) and label, plus optional help, required, default, options (for select), placeholder, advanced (hide under “Advanced”), secret (encrypt at rest, never send to the browser) and bindsSecret (the secret is tied to this field’s value; changing it drops the saved secret, and a secret from wp-config.php is only used if this field is defined there too).

Mailers are stateless: settings are passed on each call, so one instance serves both connections. send() should return SendResult::failed() rather than throw. An uncaught exception is turned into a failure with code mailer_exception.

use Stacko\Mail\Mail\Message;
use Stacko\Mail\Mail\SendResult;
use Stacko\Mail\Mailers\ConnectionTest;
use Stacko\Mail\Mailers\MailerInterface;
final class Acme_Mailer implements MailerInterface {
public function id(): string { return 'acme'; }
public function label(): string { return 'Acme Mail'; }
public function description(): string { return 'Sends through the Acme HTTP API.'; }
public function schema(): array {
return array(
array(
'key' => 'apiKey',
'type' => 'password',
'label' => 'API key',
'secret' => true,
'required' => true,
),
);
}
public function send( Message $message, array $settings ): SendResult {
$start = microtime( true );
$response = wp_safe_remote_post(
'https://api.acme.example/send',
array(
'headers' => array( 'Authorization' => 'Bearer ' . $settings['apiKey'] ),
'body' => wp_json_encode(
array(
'from' => (string) $message->from,
'to' => array_map( 'strval', $message->to ),
'subject' => $message->subject,
'html' => $message->body,
)
),
)
);
$ms = (int) round( ( microtime( true ) - $start ) * 1000 );
if ( is_wp_error( $response ) ) {
return SendResult::failed( $this->id(), $ms, 'connect_failed', $response->get_error_message(), true );
}
if ( 401 === wp_remote_retrieve_response_code( $response ) ) {
return SendResult::failed( $this->id(), $ms, 'auth_failed', 'The API key was rejected.' );
}
return SendResult::sent( $this->id(), $ms );
}
public function test( array $settings ): ConnectionTest {
$test = new ConnectionTest();
if ( empty( $settings['apiKey'] ) ) {
return $test->fail( 'key', 'API key', 'missing_api_key', 'Enter your API key.' );
}
return $test->done( 'key', 'API key', 'Present.' );
}
public function dns_hints( array $settings ): array {
return array( 'spf_include' => 'spf.acme.example' );
}
}
add_filter( 'stacko_mail_mailers', function ( array $mailers ): array {
$mailers['acme'] = new Acme_Mailer();
return $mailers;
} );

Use the error codes Stacko Mail already explains, so users get the plain-language fix. Unknown codes are shown with the generic unknown explanation.

Filters the status written to the email log for an email Stacko Mail sent.

apply_filters( 'stacko_mail_log_status', string $status, Message $message );
Parameter Type Description
$status string sent, failed or blocked.
$message Message The message as sent.

Returns string. The admin understands sent, failed, blocked, redirected and sending.

When it fires: when a send handled by Stacko Mail is logged (from stacko_mail_after_send), including test and alert emails. It does not fire for emails core sent itself with WordPress default. Email control uses it at priority 10 to turn sent into redirected.

add_filter( 'stacko_mail_log_status', function ( string $status, $message ): string {
// Treat emails held by our own gateway as blocked.
if ( 'sent' === $status && isset( $message->headers['X-Hold'] ) ) {
return 'blocked';
}
return $status;
}, 10, 2 );

Filters the capability needed to use Stacko Mail: the admin screens, every REST route, the failure notice and the admin bar indicator.

apply_filters( 'stacko_mail_capability', string $capability );
Parameter Type Description
$capability string manage_options, or manage_network_options on multisite.

When it fires: whenever Stacko Mail checks permissions.

add_filter( 'stacko_mail_capability', function (): string {
return 'manage_woocommerce';
} );

Filters the base URL for the “Learn more” link shown with error explanations. When it’s not empty, each explanation links to the base URL with the error code appended, for example https://example.com/email-errors/auth_failed.

apply_filters( 'stacko_mail_error_docs_url', string $base );
Parameter Type Description
$base string Default '' (no link). A trailing slash is added.

When it fires: each time an error is explained (log details, troubleshooter, alerts, WP-CLI).

add_filter( 'stacko_mail_error_docs_url', function (): string {
return 'https://intranet.example.com/email-errors';
} );

Fires after Stacko Mail tried to send an email, with every attempt in order. The last attempt decides the result. Stacko Mail’s own logger, alerts and stats listen here.

do_action( 'stacko_mail_after_send', Message $message, array $attempts );
Parameter Type Description
$message Message The message, after stacko_mail_message.
$attempts list<array{connection: Connection, result: SendResult}> One entry per attempt: the primary, then the backup if it was used. A blocked email has one failed attempt with code blocked.

When it fires:

  • For every wp_mail() call Stacko Mail handles, before core’s wp_mail_succeeded / wp_mail_failed.
  • For test emails sent through a configured mailer from Tools, the setup wizard and wp stacko-mail test ($message->source is stacko-mail-test).
  • For alert emails ($message->source is stacko-mail-alert).

It does not fire when core sends the email itself with WordPress default. Use wp_mail_succeeded / wp_mail_failed or stacko_mail_logged for those.

add_action( 'stacko_mail_after_send', function ( $message, array $attempts ) {
if ( count( $attempts ) > 1 ) {
$first = $attempts[0]['result'];
error_log( sprintf(
'Stacko Mail used the backup for "%s" (primary failed: %s)',
$message->subject,
$first->error_code
) );
}
}, 10, 2 );

Fires after an email was written to the log. Alerts and the admin failure notice listen here.

do_action( 'stacko_mail_logged', int $id, array $fields, Message $message );
Parameter Type Description
$id int Log entry ID.
$fields array<string,mixed> Status columns: status, connection (primary or backup), mailer, error_code, error_message; for emails Stacko Mail sent, also message_id and duration_ms.
$message Message The message.

When it fires: for every logged email, including those core sent with WordPress default. It does not fire when Keep an email log is off.

add_action( 'stacko_mail_logged', function ( int $id, array $fields, $message ) {
if ( 'failed' === $fields['status'] && 'woocommerce' === $message->source ) {
// Flag the order, notify a monitoring service, etc.
do_action( 'my_shop_email_failed', $id, $fields['error_code'] );
}
}, 10, 3 );

Fires after a connection was saved from the admin (Connections screen or setup wizard).

do_action( 'stacko_mail_connection_saved', string $slot, string $mailer );
Parameter Type Description
$slot string primary or backup.
$mailer string Mailer ID, e.g. smtp.

When it fires: after a successful save through the REST API. It does not fire for imports from other plugins or settings files, or when connections are swapped or removed.

add_action( 'stacko_mail_connection_saved', function ( string $slot, string $mailer ) {
if ( 'primary' === $slot ) {
delete_transient( 'my_plugin_mail_status' );
}
}, 10, 2 );