Change email address for sending

Hello everyone,

I know the default behavior of sending email in Keycloak will always trigger with email attribute from User which makes sence. On the other hand, i have business case where the email field is used for different purposes and we would like to send email to another field value like custom attribute for email sending

I read the code of org.keycloak.email.freemarker.FreeMarkerEmailTemplateProvider#send(java.lang.String, java.util.List<java.lang.Object>, java.lang.String, java.util.Map<java.lang.String,java.lang.Object>) and the method does not allow to pass in address.

I looked at protected method org.keycloak.email.freemarker.FreeMarkerEmailTemplateProvider#send(java.util.Map<java.lang.String,java.lang.String>, java.lang.String, java.lang.String, java.lang.String, java.lang.String) and I can see you can pass in address as the last attribute.

I have options either i have to clone UserModel and set email attribute directly because setting email attribute to the same userModel will trigger update email attribute of User which we don’t want.

Inherit org.keycloak.email.freemarker.FreeMarkerEmailTemplateProvider which I do not know how to register the implement in Keycloak.

Thanks a lot for any hints,
Tuan Do

You can just extend the default FreeMarkerEmailTemplateProvider and corresponding ...Factory classes and return a higher order value in the factory class. This way, the Keycloak will use your implementation.

Or you implement your custom provider/factory and configure it as the default to be used, like described here: Configuring providers - Keycloak

BTW: mentioning methods with only the types of the arguments, not the names, makes it very hard to read and finding the proper method you are referring to. Also, repeated usage of full package names bloats the text. Consider to mention it e.g. like send(Map<String, String> config, String subject, String textBody, String htmlBody, String address) which is easier to read and understand.

1 Like

Perfect exactly thanks, that’s what i’m implementing right now. By enable this new email provide during starting of Keycloak. Thanks a lot for quick support

1 Like