Repository navigation
feat(providers): support HTTP API providers - #41
Conversation
- Add Transport_PHPMailer, a PHPMailer that hands the built email to a provider send callback instead of sending it over SMTP - Swap it in on the wp_mail filter when the configured provider has send and wp_mail() would use the default PHPMailer - Turn a WP_Error or a thrown error from send into a PHPMailer exception, with password field values redacted, so wp_mail_failed, the log and the alerts keep working - Accept providers with configure, send or both in Provider_Registry - Document the send contract in readme.md, readme.txt and AGENTS.md - Add tests for the transport, the swap and the registry validation Many mail services offer an HTTP API that works where SMTP ports are blocked. This lets a provider use it while wp_mail() still parses the headers, recipients and attachments. Fixes WordPress#1
|
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. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
PHPMailer in WordPress 6.3 keys Reply-To addresses by address, so the assertion failed on the PHP 7.4 / WP 6.3 job.
|
Thanks @faisalahammad, this is a well-thought-out foundation. Letting To resolve
Smaller suggestions
Overall this looks good to me once points 1 and 2 are covered. Disclosure: I used Claude (Claude Code) to help review this PR. I read through its findings and am posting them as my own review. |
- Put the transport mailer in place when any provider has a send callback, so the provider is read on phpmailer_init. Other wp_mail callbacks, like a connection selector, can no longer change the result by running first or last. - Redact base64, URL, JSON and Basic auth forms of secrets, longest first. - Fail an email sent from inside a send callback, without changing the email in progress. - Do not create the SMTP instance for API providers. Addresses PR feedback. Refs WordPress#41
|
Thanks @davidperezgar for the careful review. I pushed 18f0800 (and merged trunk first in 48dede4). 1. Order with #32
Because of this, #32 is not merged, so I can not test both together yet. 2. Redaction Errors now redact the plain, base64, URL (raw and form) and JSON forms (with and without escaped slashes and unicode) of every password field. They also redact Basic auth tokens ( Smaller suggestions
412 tests pass and |
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>
… it is static WordPress 6.8 declares setLanguage() as an instance method and stores the strings per instance, so the static call threw an Error and broke every send through a transport. Since 6.9 the method and the strings are static. On 6.8 the transport now keeps the English PHPMailer errors.
* Add a Google (Gmail API) provider Sends through the Gmail API with OAuth 2.0, using the HTTP API provider support from #41. The site stores a client ID, client secret and refresh token (gmail.send scope); access tokens are cached per account in a transient and dropped on a 401. Bcc recipients are kept in the raw message. Transients are removed on uninstall. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep the WordPress PHPMailer when no connection uses the Google provider Built-in API providers only make the plugin swap in Transport_PHPMailer when a connection uses them, so SMTP-only sites are unchanged. Providers added by other plugins keep the previous behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Show the fields of the selected provider without saving The settings and connection forms render the fields of every provider. Those of the other providers are hidden and disabled, so they are not submitted, and assets/js/provider-fields.js swaps them when the provider select changes. Secrets of other providers are never printed, and a key shared by two providers gets a unique id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Providers can now send through an HTTP API instead of SMTP, with a new
sendcallback.wp_mail()still builds the email as usual (headers, recipients, attachments, From andphpmailer_init), and only the delivery goes to the provider. Errors become a normalwp_mail_failed, so the email log, alerts and test email work with API providers too. Only the contract ships here, no built-in API provider.Fixes #1
Changes
Provider_Registry
Before:
After:
Why: A provider needs
configure,sendor both. A send-only provider was dropped before.Transport_PHPMailer (new)
A
PHPMailersubclass. When a transport is set,send()runspreSend()(address checks and MIME build), then calls the provider:Anything but
truebecomes aPHPMailer\Exception, with values ofpasswordfields replaced by[redacted].Why:
wp_mail()only catches PHPMailer exceptions, so this keeps the core success and failure actions.pre_wp_mailwas not used because it skips header parsing and those actions.Mailer
use_transport()runs on thewp_mailfilter. When the active provider hassendand is configured, it puts aTransport_PHPMailerin the global$phpmailer, with the sameis_email()validator and translations core sets. It does this only when the global is empty or the defaultPHPMailer/WP_PHPMailer, so a plugin that replaced PHPMailer is left alone and the email is logged asother.Docs
readme.mdhas a new "HTTP API providers" section with an example.readme.txt(FAQ and changelog) andAGENTS.mdare updated.Testing
Test provider used (mu-plugin):
Test 1: Provider shows up
Result: the API key field shows instead of the SMTP fields. Before this change, Test API was ignored and the page fell back to Generic SMTP.
Test 2: Success
abcand send a test email.Result: "Test email sent", and the log shows the email as Sent with provider Test API.
Test 3: Failure
failand send a test email.Result: "Rejected: invalid key [redacted]" in the Test tab and in the log. The key is not shown.
Test 4: Generic SMTP
Result: works as before, with the connection log.
Automated: 399 PHPUnit tests pass, including end-to-end
wp_mail()tests for success, failure, Cc, Reply-To and attachments.composer lintis clean.Screenshots
Test email sent through the API provider
Failure, with the key redacted
Email log