0

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.

Lalit Jawale
  • 1,216
  • 9
  • 19

1 Answers1

0

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.

Shivam Aggarwal
  • 795
  • 1
  • 11
  • 30
  • Thanks for your reply, but I need to send user's password also, from where can I get user's password? – Lalit Jawale Aug 02 '16 at 12:24
  • Try using user.getPassword() for that – Shivam Aggarwal Aug 03 '16 at 06:46
  • user.getPassword() gives me encrypted password which cannot be decrypted. So that's not useful. – Lalit Jawale Aug 04 '16 at 13:09
  • @LalitJawale Updated my answer! – Shivam Aggarwal Aug 05 '16 at 08:28
  • Thanks for you time, but still it is using liferay's "Reset Password Template" which does not have password parameter. – Lalit Jawale Aug 05 '16 at 10:35
  • If you are able to get the password,you can simply replace body key with ADMIN_EMAIL_USER_ADDED_BODY and make sure to populate the correct parameters as per the template – Shivam Aggarwal Aug 05 '16 at 11:35
  • I am getting password but it is in encrypted format, So can you please tell me how can I decrypt it to send it as a parameter. – Lalit Jawale Aug 07 '16 at 05:49
  • @LalitJawale as per the Edited answer,could you try directly using sendPassword method for email(containing password) after you perform your custom logic to add expando field – Shivam Aggarwal Aug 08 '16 at 08:42
  • Sorry but my intention behind this scenario is to send user credentials to user at any time and not as soon as the user is added. So later I will not have that user's password with me to send it to email. Biggest hurdle is to have user password in PLAIN TEXT to send it to user. – Lalit Jawale Aug 08 '16 at 12:31
  • As per to the best of my knowledge,due to security reasons,to not allow even developers have access to the user password,user password is not available as plain text.Even we had faced this hurdle some time back,but I guess with a clear business case,sending password should not be an option.Rather,the user should use reset/Forgot password view. – Shivam Aggarwal Aug 09 '16 at 05:30