You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make email delivery a supported extension point
#55816
When #52361 was declined, the reason was that Keycloak can't maintain proprietary mechanisms for sending email, and that this is what extensions and SPIs are for. I agree with that, and I'd like to propose the small change that would make it true in practice. No vendor code would go into core.
Where an email sender extension stands today
The SPI is internal.EmailSenderSpi#isInternal() returns true, so every custom sender starts with KC-SERVICES0047 … is implementing the internal SPI emailSender. This SPI is internal and may change without notice. An operator takes that as "unsupported", and an author has no contract to build against.
Composition is locked inside the SMTP sender.multipart/alternative, the header order, the RFC 2047 subject, Reply-To defaulting to From, IDN conversion and address validation are private methods of DefaultEmailSenderProvider. A sender for an HTTP API has to rewrite them, and nothing guarantees that a recipient gets the same message when a realm changes transport.
The contract is undocumented. For example, send can now run on the EMAIL executor in a transaction of its own, outside the request. The interface's only javadoc says validate checks "configuration for the SMTP sender".
Why it is worth it
The demand is real, but scattered.How can I configure Keycloak to send emails using AWS SES with IAM roles, instead of using IAM users, to follow best practices? #32010, which asks how to send through SES with IAM roles instead of IAM users, has 14 upvotes and has been unanswered since August 2024. At least five SES senders exist outside Keycloak. The one that is still maintained has 19 tags in five years, and most of them only follow Keycloak releases. The rest are archived, forks, or bundled inside other products. Senders for other services (Zoho, generic HTTP senders) share the same problems.
The cost to Keycloak is small. The interface has changed incompatibly three times in ten years: 2017, 2022 and 2025. Since then it has only gained constants. Making it public mostly means that future additions come as default methods, as send(…, UserModel, …) already became in 2022.
Proposal
Two independent changes, each useful without the other. Both are implemented and tested on my fork against current main:
A public emailSender SPI (branch). It flips isInternal and documents the contract on the interface: what config is, that send either delivers or throws, that it may run outside the request, that validate is static, and that configuration keys other than the two masked ones are visible to realm administrators. It also adds a chapter to the Server Developer Guide and an integration test with a custom sender.
Composition separated from transport (branch). What DefaultEmailSenderProvider does to build the message moves into one class, EmailMessage. The SMTP sender keeps only SMTP: session properties, authenticators, SMTPUTF8 and Transport. The SMTP output does not change. A test written and run against the code before the refactor pins the message byte for byte on the wire, plus the SMTP envelope, and it passes unchanged afterwards. EmailMessage can also write the message with CRLF line endings, which HTTP APIs that take a raw message expect and which MimeMessage#writeTo does not produce for 7bit parts.
With both, a sender for a vendor's API comes down to authentication plus one HTTP call. It is maintained outside core, and it sends the message Keycloak composes.
The first consumer already exists: the SES sender from #52361, now a standalone extension for 26.8.2 (lorexoff/keycloak-aws-ses-email-sender). Two of us maintain it, with two years of support declared. It would move to the public SPI and to EmailMessage as soon as they exist.
A question, not part of the proposal
The realm's email settings are one map shaped around SMTP, and testSMTPConnection knows SMTP secrets by name. If a sender could declare its own configuration properties, as other components do, non-SMTP senders would be configurable from the console and testable with "Test connection". That is a larger change that touches the admin UI, so I would only start it if you think it belongs in core.
Would you accept these two changes? And is there anything you would want done differently?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
When #52361 was declined, the reason was that Keycloak can't maintain proprietary mechanisms for sending email, and that this is what extensions and SPIs are for. I agree with that, and I'd like to propose the small change that would make it true in practice. No vendor code would go into core.
Where an email sender extension stands today
EmailSenderSpi#isInternal()returnstrue, so every custom sender starts withKC-SERVICES0047 … is implementing the internal SPI emailSender. This SPI is internal and may change without notice. An operator takes that as "unsupported", and an author has no contract to build against.multipart/alternative, the header order, the RFC 2047 subject,Reply-Todefaulting toFrom, IDN conversion and address validation are private methods ofDefaultEmailSenderProvider. A sender for an HTTP API has to rewrite them, and nothing guarantees that a recipient gets the same message when a realm changes transport.sendcan now run on theEMAILexecutor in a transaction of its own, outside the request. The interface's only javadoc saysvalidatechecks "configuration for the SMTP sender".Why it is worth it
send(…, UserModel, …)already became in 2022.Proposal
Two independent changes, each useful without the other. Both are implemented and tested on my fork against current
main:emailSenderSPI (branch). It flipsisInternaland documents the contract on the interface: whatconfigis, thatsendeither delivers or throws, that it may run outside the request, thatvalidateis static, and that configuration keys other than the two masked ones are visible to realm administrators. It also adds a chapter to the Server Developer Guide and an integration test with a custom sender.DefaultEmailSenderProviderdoes to build the message moves into one class,EmailMessage. The SMTP sender keeps only SMTP: session properties, authenticators, SMTPUTF8 andTransport. The SMTP output does not change. A test written and run against the code before the refactor pins the message byte for byte on the wire, plus the SMTP envelope, and it passes unchanged afterwards.EmailMessagecan also write the message with CRLF line endings, which HTTP APIs that take a raw message expect and whichMimeMessage#writeTodoes not produce for 7bit parts.With both, a sender for a vendor's API comes down to authentication plus one HTTP call. It is maintained outside core, and it sends the message Keycloak composes.
The first consumer already exists: the SES sender from #52361, now a standalone extension for 26.8.2 (lorexoff/keycloak-aws-ses-email-sender). Two of us maintain it, with two years of support declared. It would move to the public SPI and to
EmailMessageas soon as they exist.A question, not part of the proposal
The realm's email settings are one map shaped around SMTP, and
testSMTPConnectionknows SMTP secrets by name. If a sender could declare its own configuration properties, as other components do, non-SMTP senders would be configurable from the console and testable with "Test connection". That is a larger change that touches the admin UI, so I would only start it if you think it belongs in core.Would you accept these two changes? And is there anything you would want done differently?
All reactions