我正在使用Keycloak的Admin REST API向用户发送验证电子邮件。具体来说,使用端点:
admin/realms/{realmName}/users/{userId}/execute-actions-email?redirect_uri={redirectURI}&client_id={clientId}
电子邮件已成功发送,但keycloak选择的电子邮件模板是“更新您的帐户”模板,而不是“验证电子邮件”模板。
有没有办法让Keycloak使用正确的模板?
更新:
也试用了send-verify-email
端点..存在同样的问题。
答案 0 :(得分:3)
我扩展了Barnys的答案,并最终得到了这个模板。 不知道为什么他们不称其为“ bug”而是限制https://issues.redhat.com/browse/KEYCLOAK-5034
用于HTML
<#outputformat "plainText">
<#assign requiredActionsText><#if requiredActions??><#list requiredActions><#items as reqActionItem>${msg("requiredAction.${reqActionItem}")}<#sep>, </#sep></#items></#list></#if></#assign>
<#assign requiredActionsValues><#if requiredActions??><#list requiredActions><#items as reqActionItem>${reqActionItem}<#sep>, </#sep></#items></#list></#if></#assign>
</#outputformat>
<html>
<body>
<#if requiredActionsValues == "VERIFY_EMAIL">
${kcSanitize(msg("emailVerificationBodyHtml",link, linkExpiration, realmName, linkExpirationFormatter(linkExpiration)))?no_esc}
<#elseif requiredActionsValues == "UPDATE_PASSWORD">
${kcSanitize(msg("passwordResetBodyHtml",link, linkExpiration, realmName, linkExpirationFormatter(linkExpiration)))?no_esc}
<#else>
${kcSanitize(msg("executeActionsBodyHtml",link, linkExpiration, realmName, requiredActionsText, linkExpirationFormatter(linkExpiration)))?no_esc}
</#if>
</body>
</html>
用于纯文本
<#ftl output_format="plainText">
<#assign requiredActionsText><#if requiredActions??><#list requiredActions><#items as reqActionItem>${msg("requiredAction.${reqActionItem}")}<#sep>, </#sep></#items></#list><#else></#if></#assign>
<#assign requiredActionsValues><#if requiredActions??><#list requiredActions><#items as reqActionItem>${reqActionItem}<#sep>, </#sep></#items></#list></#if></#assign>
<#if requiredActionsValues == "VERIFY_EMAIL">
${msg("emailVerificationBody",link, linkExpiration, realmName, linkExpirationFormatter(linkExpiration))}
<#elseif requiredActionsValues == "UPDATE_PASSWORD">
${msg("passwordResetBody",link, linkExpiration, realmName, linkExpirationFormatter(linkExpiration))}
<#else>
${msg("executeActionsBody",link, linkExpiration, realmName, requiredActionsText, linkExpirationFormatter(linkExpiration))}
</#if>
不要忘记将模板更改放入集群的所有实例并重新启动每个集群实例
sudo systemctl restart keycloak.service
答案 1 :(得分:-1)
我正在使用3.4.0版。如果使用execute-actions端点,则无法直接选择模板。但您可以使用freemarker选择不同的内容。以下是<MY-THEME>/email/html/executeActions.ftl
<#outputformat "plainText">
<#assign requiredActionsValues><#if requiredActions??><#list requiredActions><#items as reqActionItem>${reqActionItem}}<#sep>, </#sep></#items></#list></#if></#assign>
</#outputformat>
<!DOCTYPE html>
<html>
<body>
Actions: '${requiredActionsValues}'<br>
Email: ${user.email}<br>
Username: ${user.username}<br>
</body>
</html>
使用变量requiredActionsValues
根据操作选择内容。