Expand my Community achievements bar.

SOLVED

Trigger Mail service for admin login

Avatar

Level 4

Dear All,

 

I have configured Day CQ Mail service in my local like below.

 

subnaik_0-1735649487503.png

 

Now I have written one servlet to get email and I am successfully getting email like below.

 

subnaik_1-1735649963274.png

I have written below servlet.

 

************************** SendEmailAdminLoginServlet  *******************************

 

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ai.contentadmin.core.service.SendEmailService;

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Custom Servlet",
		"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/sendemail" })
public class SendEmailAdminLoginServlet extends SlingAllMethodsServlet {

	private static final long serialVersionUID = 1L;

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Reference
	private SendEmailService sendEmailService;

	@Override
	protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp) throws IOException {

		String emailTemplatePath = "/etc/workflow/notification/email/admin-login-email.txt";

		// Set email template dynamic variables
		Map<String, String> emailParams = new HashMap<String, String>();
		emailParams.put("subject", "ADMIn Login AEM Send Email");
		emailParams.put("message", "This is ADMIN Login Email!!!");

		// Sender Email Address
		emailParams.put("senderEmailAddress", "abc@gmail.com");

		// Sender Email Name
		emailParams.put("senderName", "Test User For Login");

		// Sender Email Name
		emailParams.put("recipientName", "Test User");

		List<String> recipients = new ArrayList<>();
		recipients.add("test@gmail.com");
		logger.info("Recipients list :{}", recipients);

		try {
			sendEmailService.sendEmail(emailTemplatePath, recipients, emailParams);
		} catch (LoginException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		logger.error(">>>>>>>>>>>   Email Sent <<<<<<<<<<<<< '{}'");

		resp.setStatus(SlingHttpServletResponse.SC_OK);
		resp.setContentType("application/json;charset=UTF-8");
		resp.getWriter().print("{\"response message\" : \" Email Successfully Sent !!!\"}");
	}
}

 

Now my requirement is that whenever some admin will login to the AEM environment then one email will be triggered with the date and time from the IP where we have logged in. Can you please help me here how can I achieve this ?

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi, 

It depends on how you are authenticating into AEM, you can intercept that process and then manually trigger that servlet that you created. You can check this article that explains one way to do it: https://medium.com/tech-learnings/adobe-experience-manager-reporting-on-users-last-login-date-e20350.... The tricky part is to know the IP from where this is authenticated, I don't know if that's something easy to accomplish other than use some CDN's feature, again this will depend on which type of CDN and information is being passed through your back end servlet, something like this: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-get-client-ip-addre....

 

Hope this helps



Esteban Bustamante

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi, 

It depends on how you are authenticating into AEM, you can intercept that process and then manually trigger that servlet that you created. You can check this article that explains one way to do it: https://medium.com/tech-learnings/adobe-experience-manager-reporting-on-users-last-login-date-e20350.... The tricky part is to know the IP from where this is authenticated, I don't know if that's something easy to accomplish other than use some CDN's feature, again this will depend on which type of CDN and information is being passed through your back end servlet, something like this: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-get-client-ip-addre....

 

Hope this helps



Esteban Bustamante

Avatar

Community Advisor

Yes. I agree with @EstebanBustamante. You can also look at making a process change to get the admins to use their individual user IDs so identifying IP address should not be required.

 

Thanks

Narendra

Avatar

Level 2

Hi,

 

You can either write an event listener for authentication events or a custom filter servlet to intercept login. Also, you can hook into an authentication flow by implementing AuthenticationInfoPostProcessor intreface and override process method to send an email.