A Developers Blog: AEM Component To Send An Email Tutorial

This blog has moved to GitHub, and you should be redirected shortly...

Friday, March 6, 2015

AEM Component To Send An Email Tutorial

The purpose of this tutorial is create a component for AEM that will send an email and then display a confirmation message. This is accomplished by using the ACS AEM Commons Email API. The ACS AEM Commons package adds a lot of features to the out of the box AEM implementation. For the purpose of this tutorial we will be creating the component in crxde lite. This tutorial does not go into full detail, but does provide all of the code that was used in the creation of this component for learning purposes.

This tutorial uses the following website: ACS AEM Commons

Lets get started -

1. The first thing you need to do is download and install ACS AEM Commons package into your instance of AEM. There are a few ways to do this. For this project we will be installing it as a package using the package manager in crxde lite. You can download the package from github here: ACS Commons Github

2. The second thing you need to do is have an email account that you want to use with this service. For the purpose of this tutorial, we will be using a Gmail account. If you do not already have one, you can set one up quickly here: Gmail

For the Gmail account you might have to update your settings to allow AEM to send emails with this account. Go to the following site: https://www.google.com/settings/security/lesssecureapps. Once you do this, turn on access for less secure apps. If this is turned off, there is a possibility that the emails will not send, and you will receive an email that Gmail blocked access to your account.

3. After you upload and install the package you will be able to use the ACS AEM Commons features. In order to send emails through AEM you need to configure the Day CQ Mail Service component. There are a few ways to do this. For this project we will be configuring the OSGI through crxde lite.

4. Go to your project folder under apps and create a "config" folder if you do not have one. Inside this folder is where we will put your OSGI configuration.

5. Right click on your config folder and create a node. An example is posted below:


6. Add the following properties to the node you just created:


7. You will want to replace the "youremail@gmail.com" with your Gmail email address, and "smtp.password" with your password to your Gmail account.

8. The next step is to create an email template that will be sent with your email service. Email templates are kept under "etc/notification/email". Navigate here and create a new file called "en.html". This file can be a text file or an html file. The main difference is that you can use html code in the template if you choose to make it an html file.

9. Inside the file you just created put in the following code:

The code that is like this "${example}", will be updated dynamically by the component that we will create in the next few steps. You can replace the code with normal text, however it will be the same in every email that gets sent out.

10. Now it is time to create a component that will be able to use this email service and email template. Inside your component folder create a new component. An example is posted below:


11. On the component you just created add a dialog to it so it will show up in the sidekick.

12. Open the jsp file that was created for your component and put in the following code:


13. In the jsp file you will want to update the recipient address with your email address so you can test out the email service.

14. Now you just need a template to add this component to. If you already have a page that you can add components to, you can skip these next few steps and go to step 19. 

15. In your project folder go to your "components" folder, right click on it and choose "Create Component". An example is posted below:


You can keep clicking next until you have the option to create the component.

16. Open up the "empty.jsp" file that was created and replace all the code inside with the following code:



17. In your project folder, create a "templates" folder if one does not already exist. Right click on this folder and choose "Create Template". An example is posted below:


You will want to update the "Resource Type:" field with the path to the "empty" component you just created. Click next and in the "Allowed Paths" option, add "/content(/.*)?". Then keep clicking next until you have an option to create a template. 

18. Next go to the websites console and choose the option to create a page. Make sure to choose the "Empty Template" from the template options.

19. Now, open the web page and add the email component to the page. You can do this by switching over to the Classic UI, and then selecting design mode from the sidekick. Next, click on edit the design of your paragraph system, and check the box next to the "Simple Email Component". Then switch back to edit mode, and add the component to your page.

If everything works correctly you should see a message on your page that the email was sent successfully.  Every time this page is visited, or refreshed, the code will be called and will send out a new email.

Now this is a very simple component that you can build upon. One example of building upon this component is that you could add in a html form where a user would put in their their name and email address, and then take that information and send that user an email.

I hope you enjoyed this tutorial. If you have any questions or comments please feel free to post them below.

Note: On the ACS AEM Commons website there is a tutorial on how to use their email API, however it did not work right out the box for me. That is why I created this tutorial on how I was able to get this feature to work.

You can download a package of the component on GitHub from here:

AEM-Email-Example.

You will have to update the OSGI settings and the email template with you email information.

Labels: , ,

3 Comments:

At March 14, 2016 at 5:41 AM , Blogger Unknown said...

Hi,
I added the component as placed on the guide on my localhost: 4502 AEM, but when you load the page I get the error "Email sent failed", I am using gmail.com to send and the same mail To receive .. .
Might you help me? Why not send the e-mail correctly?
Thank you very much

 
At May 14, 2016 at 10:21 PM , Blogger Scott Westover said...

Did you happen to update your email settings to allow for access for less secure apps?

 
At July 25, 2016 at 10:45 AM , Anonymous Anonymous said...

Hi Scott,
Very nice and informative blogs in AEM. I am just luving it while reading. Thanks!

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home