'How to add Realm name in custom email template Subject

I have created a custom Theme for Keycloak email.

I know how to add the realm name in email body, but I didn't find how to add the realm name in the subject.

Is there any possibility to add this without the need of a custom Provider ?

Thank's !



Solution 1:[1]

Had the same question.

So did some research(went through code), and find out that as of writing this answer, the default Freemarker-based Email Implementation doesn't even supply the subject attributes(call them variables or arguments), and the final subject string formatting is done based on an empty list of attributes. https://github.com/keycloak/keycloak/blob/bfce612641a70e106b20b136431f0e4046b5c37f/services/src/main/java/org/keycloak/email/freemarker/FreeMarkerEmailTemplateProvider.java#L162

 @Override
    public void sendExecuteActions(String link, long expirationInMinutes) throws EmailException {
        Map<String, Object> attributes = new HashMap<>(this.attributes);
        attributes.put("user", new ProfileBean(user));
        addLinkInfoIntoAttributes(link, expirationInMinutes, attributes);

        attributes.put("realmName", getRealmName());

        send("executeActionsSubject", "executeActions.ftl", attributes);
    }

Which in return calls

    @Override
    public void send(String subjectFormatKey, String bodyTemplate, Map<String, Object> bodyAttributes) throws EmailException {
        send(subjectFormatKey, Collections.emptyList(), bodyTemplate, bodyAttributes);
    }

Finally, this function gets executed

 protected EmailTemplate processTemplate(String subjectKey, List<Object> subjectAttributes, String template, Map<String, Object> attributes) throws EmailException {
        try {
........
            String subject = new MessageFormat(rb.getProperty(subjectKey, subjectKey), locale).format(subjectAttributes.toArray());
........

Throughout the code trail for executeAction I nowhere found any logic which allows adding dynamic-values/variables in the subject line.

To solve this I am raising an issue. I will link it once done.

Update 1

Issue opened: https://github.com/keycloak/keycloak/issues/11883

Update 2

PR raised: https://github.com/keycloak/keycloak/pull/11885

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1