Blogs

Blogs

New feature for Liferay 7: Localize Email Notifications

Hi everyone!

For quite some time, one of our community's most requested features has been the possibility to localize all email notifications sent from your portal. Finally, the wait is over. Email localization for notifications has officially landed on master and will be publicly available in Liferay Portal 7.0.

Additionally, as part of the effort of localizing the notifications, a new liferay-ui:email-notification-setting taglib has been created that should help everyone localize their emails easily by using:

<liferay-ui:email-notification-settings
  emailBody='<%= LocalizationUtil.getLocalizationXmlFromPreferences(
      companyPortletPreferences, renderRequest, "adminEmailVerificationBody", "settings", 
      ContentUtil.get(PropsValues.ADMIN_EMAIL_VERIFICATION_BODY)) %>'
  emailParam="adminEmailVerification"
  emailSubject='<%= LocalizationUtil.getLocalizationXmlFromPreferences(
      companyPortletPreferences, renderRequest, "adminEmailVerificationSubject", "settings", 
      ContentUtil.get(PropsValues.ADMIN_EMAIL_VERIFICATION_SUBJECT)) %>'fieldPrefix="settings"
  showEmailEnabled="<%= false %>"
/>

What do you think? Is there any other improvements you'd like to see around this?

Hope you enjoy...

Good to Have this feature.

Just to share with you, for some of the our applications, we have used a "Web Content" as the email template and access the content with it's name programmatically inside the code, replace the tokens with run time values and fire the email. This way we can change the contents anytime on the fly and also send customised emails to users based on their geography and language.

Here is the API to pick up the most recent article (Web Content) with it's name.

public static String getEmailTemplate(String articleId) {

long companyId = CompanyThreadLocal.getCompanyId();

String emailTemplate = StringPool.BLANK;
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
JournalArticle.class, PortalClassLoaderUtil.getClassLoader());
dynamicQuery.add(RestrictionsFactoryUtil.eq("companyId", companyId));
dynamicQuery.add(RestrictionsFactoryUtil.eq("articleId", articleId));
dynamicQuery.addOrder(OrderFactoryUtil.desc("version"));

try {
@SuppressWarnings("unchecked")
List<JournalArticle> articles = JournalArticleLocalServiceUtil.dynamicQuery(dynamicQuery);

for (JournalArticle journalArticle: articles) {
try {
emailTemplate = JournalArticleLocalServiceUtil.getArticleContent(
journalArticle, null, null, "en_US", null);
break;
} catch (PortalException e) {
e.printStackTrace();
}
}
} catch (SystemException e) {
e.printStackTrace();
}

return emailTemplate;
}

================

After programmatically obtaining the Email template, replace the values during runtime as shown here…

public static void sendInvitation(long invitationId, String inviterEmail, String inviterName, String inviteeEmail, String inviteeName) {

MailMessage mailMessage = new MailMessage();
mailMessage.setFrom(getFrom());
mailMessage.setTo(getTo(inviteeEmail, inviteeName));

InternetAddress[] replyTo = new InternetAddress[]{getTo(inviterEmail, inviterName)};
mailMessage.setReplyTo(replyTo);

StringBuilder sb = new StringBuilder();
sb.append("Inviation from ").append(inviterName).append("to join hasnain");
mailMessage.setSubject(sb.toString());

String mailBody = getEmailTemplate("PORTAL_INVITATION");

String[] tokens = {"[$INVITER_NAME$]", "[$INVITATION_ID$]"};
String[] replacements = {inviterName, String.valueOf(invitationId)};

mailBody = StringUtil.replace(mailBody, tokens, replacements);

mailMessage.setBody(mailBody);

MailServiceUtil.sendEmail(mailMessage);
}
Hi Ahmed,

Thanks for sharing! Your idea looks quite interesting. We've been considering adding that kind of support through ADT... would that solve your use case as well?

If you'd like to see this implemented in core, you could open a Feature Request in https://www.liferay.com/community/ideas. The most voted ones have high chances of getting picked up for next versions emoticon
Hi Chema, does it means that all *.tmpl messages of portal.properties can be localized ?
Hey Denis!

Yes, that's the idea. It should apply to all *.tmpl files in portal properties regarding email notifications. Furthermore, you should be able to do the same for any other templatized content you wished to localize for sending emails.
Great! so will be this feature available in plugins and Social Office too ?
Hey Denis,

Yes, now that the framework allows for it, we should have all email notifications in plugins (SO, Knowledge Base, Calendar...) using the new infrastructure by the time we release the next version.

In fact, we're about to start working on integrating it for Knowledge Base (https://issues.liferay.com/browse/LPS-50889). Feel free to submit contributions for any of the others! ;)
This is what the feature which i have searched and requested for it.at the time i have implemented with the help web content localization.

for details.
https://www.liferay.com/community/forums/-/message_boards/message/12795025


Any ways ...happy to see this feature..emoticon

Hello it is very urgent thank you for answering me I have a problem integrating the text editor into the knowledge bases Thanks for your help