Expand my Community achievements bar.

Applications for the 2024-2025 Adobe Experience Manager Champion Program are open!
SOLVED

How to send the email notification to the entire user group instead of single user.

Avatar

Level 5

Hi,

 

I have a requirement to send the email notification to the group. How can that be done?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @keerthana_hn 

 

You could fetch the users in a group and iterate through them to get their email ID. Below is a sample code to fetch the user email IDs.


UserManager userManager=jrSession.getUserManager();
Group group = (Group) userManager.getAuthorizable("GroupId");
log.info("group >>> "+group);
final Iterator<Authorizable> memberslist = group.getMembers();
while (memberslist.hasNext()) {
final Authorizable user = memberslist.next();
if (user != null && !user.isGroup() && user.getProperty("./profile/email") != null) {

//EMAIL ADDRESS FOR MEMBERS
Value[] usrEmail = user.getProperty("./profile/email");

for(Value emailGroup : usrEmail) {
log.info("emailGroup line 152>>> "+emailGroup);
}
}
}

Once you have the list of email ids you can use org.apache.commons.mail.HtmlEmail.addTo() to add the list of ids. 

Let me know if you still face any issues.

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi @keerthana_hn 

 

You could fetch the users in a group and iterate through them to get their email ID. Below is a sample code to fetch the user email IDs.


UserManager userManager=jrSession.getUserManager();
Group group = (Group) userManager.getAuthorizable("GroupId");
log.info("group >>> "+group);
final Iterator<Authorizable> memberslist = group.getMembers();
while (memberslist.hasNext()) {
final Authorizable user = memberslist.next();
if (user != null && !user.isGroup() && user.getProperty("./profile/email") != null) {

//EMAIL ADDRESS FOR MEMBERS
Value[] usrEmail = user.getProperty("./profile/email");

for(Value emailGroup : usrEmail) {
log.info("emailGroup line 152>>> "+emailGroup);
}
}
}

Once you have the list of email ids you can use org.apache.commons.mail.HtmlEmail.addTo() to add the list of ids. 

Let me know if you still face any issues.

Avatar

Level 5

Hi @JeevanRaj ,

 

I have created a list of id's and trying to add it to the email.addTo() nut it's returning an exception. Is there any way create a list to add it to the email?

 

Avatar

Community Advisor

What is the exception you are getting?

 

I was able to set emails to the group with below sample code. Let me know if it helps.

 

try(ResourceResolver rr = request.getResourceResolver()) {
HtmlEmail email = new HtmlEmail();
email.addTo("receiver@example.com");
email.setSubject("test email subject");
email.setMsg("text email body");
email.setHtmlMsg("<html><head></head><body><p>html email body</p></body></html>");
email.setFrom("sender@example.com","TestUser");
List<String> emailIds = new ArrayList<>();




Session session = rr.adaptTo(Session.class);
UserManager userManager = AccessControlUtil.getUserManager(session);
Group group = (Group) userManager.getAuthorizable(<GROUP_NAME_HERE>);
final Iterator<Authorizable> memberslist = group.getMembers();
while (memberslist.hasNext()) {
final Authorizable user = memberslist.next();
if (user != null && !user.isGroup() && user.getProperty("./profile/email") != null) {

//EMAIL ADDRESS FOR MEMBERS
Value[] usrEmail = user.getProperty("./profile/email");
for(Value emailGroup : usrEmail) {
emailIds.add(emailGroup.getString());
}
}
}

String[] arr = emailIds.stream().toArray(String[] ::new);
email.addTo(arr);
// Declare a MessageGateway service
MessageGateway<Email> messageGateway;
messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
messageGateway.send(email);
} catch(Exception e) {
e.printStackTrace();
}

Avatar

Level 3

It can be done in 2 ways

  1. Create DL for groups and send email to DL.
  2. Use recursive method to get list of all users.(refer ACS AEM commons functionality). Either you can use ACS commons way of sending separate email to all users in a loop. Or you can create your own email service that takes multiple email addresses into TO address and send single mail. Both has its pros and cons. With single mail you make single call to SMTP but all mail ids will be visible in TO address. To hide it you have to use BCC instead of TO address. Again with multiple address in loop will not expose which all users are notified as single 1:1 email will be triggered.