0
votes

In my liferay application I am adding application users by using Expando programatically and not using liferay default User signup. Now I want to send credentials to user to his email address using liferay default templates available from Control Panel - Portal Setting - Email Templates.

How can I trigger email using this liferay inbuilt template? Any hint is appreciated.

1

1 Answers

0
votes

Liferay uses template files(.tmpl) files to manage Email templates. As per the Liferay Source,for User creation,given snippet is used to send out mail to users.

String body = PrefsPropsUtil.getContent(
            user.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY);

SubscriptionSender subscriptionSender = new SubscriptionSender();

    subscriptionSender.setBody(body);
    subscriptionSender.setCompanyId(user.getCompanyId());
    subscriptionSender.setContextAttributes(
        "[$USER_ID$]", user.getUserId(), "[$USER_PASSWORD$]", password,
        "[$USER_SCREENNAME$]", user.getScreenName());
    subscriptionSender.setFrom(fromAddress, fromName);
    subscriptionSender.setHtmlFormat(true);
    subscriptionSender.setMailId(
        "user", user.getUserId(), System.currentTimeMillis(),
        PwdGenerator.getPassword());
    subscriptionSender.setServiceContext(serviceContext);
    subscriptionSender.setSubject(subject);
    subscriptionSender.setUserId(user.getUserId());

    subscriptionSender.addRuntimeSubscribers(toAddress, toName);

    subscriptionSender.flushNotificationsAsync();

This is part of service impl class UserLocalServiceImpl. Here "PropsKeys.ADMIN_EMAIL_USER_ADDED_BODY" is the path of the template for body content(default used by liferay).You can populate your custom data in the provided template.

Edit: After you perform your custom logic,you can directly call

UserLocalServiceUtil.sendPassword(
            long companyId, String emailAddress, String fromName,
            String fromAddress, String subject, String body,
            ServiceContext serviceContext)

method directly from custom class,which will take care of using liferay template as well as password management.