Skip to content

Developer overview

Stacko Mail replaces only the transport of wp_mail(). Plugins keep calling wp_mail() as usual, and core’s mail filters and actions keep working.

  • Requires WordPress 6.5+ and PHP 8.1+.
  • PSR-4 namespace Stacko\Mail, in includes/.
  • The admin screens are a Vue 3 app (source in src/, built to build/) that talks to the plugin’s REST API.

Stacko Mail hooks the core pre_wp_mail filter at priority 99.

  1. If another plugin already short-circuited wp_mail() (returned a non-null value earlier), Stacko Mail does nothing.
  2. If the primary connection is WordPress default or no connection is set up, Stacko Mail returns null and core sends the email with PHP mail() as usual. The email is still logged through core’s wp_mail_succeeded and wp_mail_failed actions. The exception is when the stacko_mail_take_over_default filter returns true (email control does this while pausing or redirecting). Stacko Mail then handles the email itself.
  3. Otherwise, Stacko Mail builds a Stacko\Mail\Mail\Message from the wp_mail() arguments. It applies core’s wp_mail_from, wp_mail_from_name, wp_mail_content_type and wp_mail_charset filters, the same way core does.
  4. The message passes through the stacko_mail_message filter. Returning null blocks the email (error code blocked).
  5. The message is sent through the primary connection. If that fails, it is sent through the backup connection, unless the failure was caused by the message itself (invalid_address, no_recipients, attachment_failed, blocked).
  6. stacko_mail_after_send fires with every attempt. The logger, alerts and stats listen here.
  7. Core’s wp_mail_succeeded or wp_mail_failed fires, so other plugins see the result. On failure, the WP_Error data includes a stacko_mail_error_code key with the error code.
  8. wp_mail() returns true or false.
Core hook Behaviour
wp_mail filter Runs as usual (core applies it before pre_wp_mail).
wp_mail_from, wp_mail_from_name, wp_mail_content_type, wp_mail_charset Applied when the message is built.
phpmailer_init Fired for the Other SMTP mailer and for WordPress default used as a backup. Not fired for API mailers (Brevo, SendGrid, Mailgun, Postmark), which don’t use PHPMailer.
wp_mail_succeeded, wp_mail_failed Fired once per email with the final result.

Stacko Mail’s own From settings use wp_mail_from and wp_mail_from_name at priority 999, and the Return-Path option uses phpmailer_init at priority 999.

If another SMTP plugin also hooks pre_wp_mail at an earlier priority and short-circuits it, that plugin sends the email and Stacko Mail steps aside. Deactivate the other plugin after importing its settings.

Each way of sending is a mailer implementing Stacko\Mail\Mailers\MailerInterface:

ID Label Transport
brevo Brevo HTTPS API (api.brevo.com)
sendgrid SendGrid HTTPS API (api.sendgrid.com or api.eu.sendgrid.com)
mailgun Mailgun HTTPS API (api.mailgun.net or api.eu.mailgun.net)
postmark Postmark HTTPS API (api.postmarkapp.com)
smtp Other SMTP PHPMailer over SMTP
default WordPress default PHP mail()

A mailer declares its settings fields with schema(). The admin form is generated from that schema, so a new mailer needs no frontend code. Add mailers with the stacko_mail_mailers filter.

The log’s “Sent by” value comes from a backtrace of the wp_mail() call. It is the plugin or theme folder slug (for example woocommerce), core for WordPress itself, or empty when unknown. Stacko Mail’s own emails use stacko-mail-test (test emails) and stacko-mail-alert (alert emails). It’s available as $message->source.

Every admin screen, REST route and AJAX action requires the manage_options capability (manage_network_options on multisite), plus a WordPress nonce. Change the capability with the stacko_mail_capability filter.

Where What
{prefix}stacko_mail_logs One row per email.
{prefix}stacko_mail_events Timeline events per email (attempts, fallback, resends).
{prefix}stacko_mail_stats_daily Daily counts by status and source.
stacko_mail_settings option General settings.
stacko_mail_connections option Primary and backup connections. Secret fields are encrypted.
stacko_mail_alerts option Alert channels and rules. Webhook URLs are encrypted.

On multisite, these are per site.

Passwords, API keys and webhook URLs are encrypted with libsodium (sodium_crypto_secretbox). The key comes from STACKO_MAIL_ENCRYPTION_KEY if it’s defined, otherwise from the AUTH_KEY and SECURE_AUTH_SALT constants. There is no plaintext fallback. Secrets are never sent to the browser, exports, logs, transcripts or diagnostic reports.

Cron hook Schedule Purpose
stacko_mail_retention Daily Deletes log rows older than the retention period.
stacko_mail_send_alert Single events Sends one alert in the background.

Both are internal. They are removed on uninstall when the user chose to delete data.

All HTTP requests use wp_safe_remote_*. Webhook URLs for alerts must be HTTPS, and Slack, Discord and Teams URLs must match that service’s domain.

The human-readable source of the admin app is included in src/. To rebuild build/:

Terminal window
npm ci && npm run build