Skip to content

Support multiple SMTP connections with per-email selection - #32

Merged
davidperezgar merged 8 commits into
trunkfrom
feature/multiple-connections
Nov 21, 2026
Merged

davidperezgar merged 8 commits into
trunkfrom
feature/multiple-connections

Conversation

@davidperezgar

@davidperezgar davidperezgar commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Fixes #30

Stacked on #31 (provider rate limits, issue #29), because each connection has its own sending limits. This PR targets that branch so the diff only shows the connections work. Merge #31 first, then retarget this PR to trunk.

What

A connection is a provider with its own settings, credentials, From address and sending limits.

  • Backward compatible. The settings saved under Tools > SMTP become the default connection (default). Nothing changes for a site that never adds another one, and existing options, secrets and log rows stay valid.
  • Admin. The Settings tab links to the default connection, the others and Add connection, and shows the form of the selected one (name, provider, provider fields, From email/name, sending limits). The ID used in code is generated from the name with sanitize_title() and never changes. The sending limits are hidden behind a Customize sending limits checkbox. Writes use admin-post handlers with a nonce and a manage_options check. Passwords are never printed back. Deleting a connection deletes its secrets.
  • Per-email selection, with the two mechanisms the issue sketches:
    • smtp_connection_for_email filter ($connection_id, $mail_data), which also routes emails sent by other plugins.
    • wp_smtp_mail( $connection, $to, $subject, $message, $headers, $attachments ).
  • Extensibility. wp_smtp_get_connections() returns id => label, provider, configured, rate_limits with no credentials. smtp_rate_limits now also receives the connection ID.
  • Logging. New connection_id column in the email log (schema version 2), a Connection column in the log table once there is more than one connection, and a connection selector in the Test tab.
  • Sender identity. The username and the From address are separate fields, so a connection can authenticate as one mailbox and show another From address.

Decisions worth a look

  • An unknown or incomplete connection falls back to the default connection instead of failing, so email is never dropped. The alternative is sending from the wrong mailbox silently; failing the email seemed worse.
  • Constants (SMTP_HOST, ...) only configure the default connection. Otherwise they would override every connection.
  • Secrets of connections are stored in the existing credentials option under connection:{id}:{field}. The colon can not be part of an ID, so deleting a can never touch the secrets of a_b.
  • Settings is scoped by an optional constructor argument, and the field rendering moved to Admin\Field_Renderer so the default and the extra connections share it.

Not included

Testing

  • composer lint is clean.
  • PHPUnit: 492 tests pass (new Connections_Tests and Connections_Tab_Tests, plus Mailer, Settings, Credentials, Logger, tracker, log table, Test email, Admin page and uninstall tests).
  • Checked by hand in a local site: add a connection, per-email selection through wp_smtp_mail() and the Connection column of the log. Real delivery through a second connection is not tested.
Open WordPress Playground Preview

davidperezgar and others added 2 commits November 16, 2026 21:08
Providers can declare optional sending limits (per second, minute, hour, day and concurrency, for messages and recipients) with the new rate_limits key. Administrators can override the message limits for the active provider under Sending limits, and the smtp_rate_limits filter can change the result. Other plugins read it with wp_smtp_get_rate_limits().

The plugin only reports the limits, it does not queue or throttle email.

Fixes #29

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A connection is a provider with its own settings, credentials, From address and sending limits. The settings saved under Tools > SMTP become the default connection, so existing sites keep working unchanged. Other connections are managed in a new Connections tab and stored in the smtp_connections option, with their secrets kept apart in the credentials storage.

An email is sent through the default connection unless another one is picked, with the smtp_connection_for_email filter or the new wp_smtp_mail() function. wp_smtp_get_connections() lists the connections for other plugins without exposing credentials. The email log records the connection that sent each email, and the Test tab can test any connection.

Builds on the provider rate limits of #31, since each connection has its own limits.

Fixes #30

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: davidperezgar <davidperez@git.wordpress.org>
Co-authored-by: ePascalC <casiepa@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

- Remove the Connections tab: the Settings tab now links to the default
  connection, the others and "Add connection", and shows the form of the
  selected one. Deleting a connection is a button of its form.
- Generate the ID of a new connection from its name with sanitize_title(),
  adding a number when it is reserved or taken. The ID never changes when
  the connection is renamed.
- Hide the sending limit fields behind a "Customize sending limits"
  checkbox, checked only when a limit is set. Unchecking it clears them.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
wp_kses_post() sorts the attributes of the link on WordPress trunk, so the
current link no longer matched `class="current" aria-current="page"`. Print
the link with esc_url() and esc_html() instead, which also keeps the markup
stable across WordPress versions.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@davidperezgar
davidperezgar added this pull request to stack #72 November 21, 2026 09:14
davidperezgar and others added 2 commits November 21, 2026 10:14
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…-connections

Conflicts resolved:
- uninstall.php: keep trunk's per-site closure and remove smtp_connections inside it.
- Log_Repository::insert(): keep trunk's upgrade and retry logic, add connection_id, and keep
  the entry without connection_id when the table still has the version 1 schema.
- Test_Email::redact(): take the password fields and the values from the same connection.
- Admin_Page: keep Field_Renderer and port the min and max attributes of number inputs to it.
- Tests: keep both sides, plus tests for the two fixes above.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Base automatically changed from feature/provider-rate-limits to trunk November 21, 2026 12:03
davidperezgar and others added 2 commits November 21, 2026 13:07
Trunk has the squash of #31, so the same hunks conflicted. Each conflicted file is the trunk
version with the changes of this branch on top, merged against the tip of #31 as base.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…fallback

- @SInCE: the 68 tags this branch adds say 1.3.0, since 1.2.0 is already released. The
  changelog entry moves from the 1.2.0 section to a new 1.3.0 one.
- smtp_connection_fallback action: fires when an email asks for an unknown or incomplete
  connection and is sent through the default one, with the ID asked for and the reason.
  Documented in readme.md and AGENTS.md, with tests for each case.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@davidperezgar
davidperezgar merged commit c2029c8 into trunk Nov 21, 2026
38 checks passed
@davidperezgar
davidperezgar deleted the feature/multiple-connections branch November 21, 2026 12:25
davidperezgar added a commit to faisalahammad/smtp that referenced this pull request Nov 21, 2026
Conflicts resolved:
- Mailer: keep the connection hooks and settings of WordPress#32 and add use_transport() next to them.
  The secrets redacted from the errors of the provider are found with the fields of the connection
  the email goes through, not the default one (new test, which fails without it).
- Provider_Registry, AGENTS.md, readme.txt: keep both the send and the rate_limits changes, and
  put the changelog entry in the 1.3.0 section.

PHPStan, which trunk now runs, flagged this branch. Fixed:
- Delivery_Tracker::remember() documents the null that Mailer passes when another plugin sends.
- Transport_PHPMailer filters the redacted values with a closure, since substr() can return false
  on PHP 7.
- Mailer uses WPINC five times, listed in the baseline like the other uses.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
faisalahammad added a commit to faisalahammad/smtp that referenced this pull request Nov 21, 2026
Since WordPress#32, the password field is rendered by Field_Renderer for every
connection, so the remove box also shows for extra connections. Check
that removing it there keeps the secret of the default connection.
faisalahammad added a commit to faisalahammad/smtp that referenced this pull request Nov 21, 2026
Since WordPress#32, the secrets of extra connections use keys like
connection:sponsors:password. Secret names only accept lowercase
letters, digits, hyphens and underscores, so wp_set_secret() refused
them and the password of an extra connection could not be saved.

- Credentials::get_secret_name() escapes the hyphen, then the colon
  (- to -0, : to -1), so two keys never share a name. Keys without
  either keep their name (smtp/password).
- delete_prefixed() also deletes the secrets stored with the API, so
  deleting a connection removes its password there too.
- Uninstall builds the names the same way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Support multiple SMTP connections with per-email selection

1 participant