Keycloak - 如何在使用 Keycloak 的 Admin REST API 发送电子邮件时 select 电子邮件验证模板?
Keycloak - How to select the email verification template when sending an email using Keycloak's Admin REST API?
我正在使用 Keycloak 的 Admin REST API 向用户发送验证电子邮件。具体来说,使用端点:
admin/realms/{realmName}/users/{userId}/execute-actions-email?redirect_uri={redirectURI}&client_id={clientId}
邮件发送成功,但是keycloak选择的邮件模板是"Update Your Account"模板而不是"Verify email"模板。
有没有办法让 Keycloak 使用正确的模板?
更新:
也尝试使用 send-verify-email
端点..存在同样的问题。
我正在使用 3.4.0 版。如果使用 execute-actions 端点,则不能直接选择模板。但是你可以使用 freemarker 来 select 不同的内容。这是 <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
select 您的内容。
我扩展了 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
我正在使用 Keycloak 的 Admin REST API 向用户发送验证电子邮件。具体来说,使用端点:
admin/realms/{realmName}/users/{userId}/execute-actions-email?redirect_uri={redirectURI}&client_id={clientId}
邮件发送成功,但是keycloak选择的邮件模板是"Update Your Account"模板而不是"Verify email"模板。
有没有办法让 Keycloak 使用正确的模板?
更新:
也尝试使用 send-verify-email
端点..存在同样的问题。
我正在使用 3.4.0 版。如果使用 execute-actions 端点,则不能直接选择模板。但是你可以使用 freemarker 来 select 不同的内容。这是 <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
select 您的内容。
我扩展了 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