How to send the email notification to the entire user group instead of single user. | Community
Skip to main content
Adobe Employee
April 7, 2022
Solved

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

  • April 7, 2022
  • 2 replies
  • 2263 views

Hi,

 

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by JeevanRaj

Hi @keerthana_h_n 

 

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.

2 replies

JeevanRaj
Community Advisor
JeevanRajCommunity AdvisorAccepted solution
Community Advisor
April 7, 2022

Hi @keerthana_h_n 

 

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.

Adobe Employee
April 7, 2022

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?

 

JeevanRaj
Community Advisor
Community Advisor
April 7, 2022

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();
}
DIPEN_SEN_
Level 3
April 7, 2022

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.  
September 6, 2024

Hi DIPEN 


can you please elaborate on the first case i.e. create DL for groups?

 

Thanks