Email Customization Hybris

Sending Email through Hybris Contents Introduction:....................................................................

Views 71 Downloads 3 File size 343KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

Sending Email through Hybris

Contents Introduction:................................................................................................................................................1 Email flow:...................................................................................................................................................2 .................................................................................................................................................................... 2 Steps to send a simple email:......................................................................................................................2 Create an email process model in core-items.xml:...................................................................2 Create process xml and associate it with process model:............................................................................3 Register process related beans in core-spring.xml...................................................................3 Create an event and listener Java classes....................................................................................................4 Create an email context Java class and register in facades-spring.xml:....................................4 Create an email body and email subject vm files:.......................................................................................5 Create email page and associate it with email body and subject vm files:..................................................5 Steps to sending email in local host:............................................................................................................6 Triggering email through HMC:....................................................................................................................7 Steps to add cc mail:....................................................................................................................................9 Tips to avoid error:......................................................................................................................................9

Introduction: This document will help you to understand entire email functionality in Hybris. This will cover the following topics:    

Email flow Steps to send a simple email Steps to create email page and templates Steps to sending email in local host

  

Triggering email through HMC Steps to add cc mail Hints to avoid error

Email flow: Trigger Email Event

Capture the event in Listener and start the email process

Email process will generate, send and remove the email

Steps to senda simple email: To send an email need to follow the below steps:          

Create an email process modelin core-items.xml Create process xml and associate it with process model Register process related beans in core-spring.xml Create an event and listener Java classes Register email process, listener as a bean in core-spring.xml Create an email context Java class and register in facades-spring.xml Create an email body and email subject vm files Create email page and associate it with email body and subject vm files Run the created impex files Note: Sample codes are attached for contact us form submission email in each step. Follow the steps from beginning

Create an email process model in core-items.xml: Email Process model like DTO(Data Transfer Object). This process model attributes will be accessed from email context Java class and will be used in email body vm and email subject vm files. Hence details which are needs to be shown in the email should be present as attributes in this process model.We can extend any existing process model like StoreFrontProcessModel etc. For example, 



For contact us form submission, we need to send email to helpdesk with customer details and user given form fields values. Hence we created 2 new models o 1.CSTicketModel which holds the all the required attributes o 2.CsTicketProcessModel which holds the CSTicketModel as an attribute After adding your model in the items.xml, do an ant all and refresh your eclipse project folders.

hsscore_items_xml.txt

Create process xml and associate it with process model: To create an email process in Hybris need to create process.xml file. Process xml file contains following attributes    

start – need to mention the action id which needs to be started as first name – Process name which will be used to start the process in the listener or any required place processClass– mention your created process model class with full package name action tags – Mention series of actions which need to be executed by this process

Action tags contains following attributes:   

id – This is used to call this action by a process or other action bean – Mention the bean id which has the logic for this corresponding action transition tag – This is used to identify the next action which needs to be called based on the results

Sample contact us form submission process xml attached below

contactUsEmailProcess.xml.txt

Register process related beans in core-spring.xml After creating process.xml file, need to mention this process as a bean in the core-spring.xml. Email process required 3 action beans. 1. generateContactUSFormEmail – this bean has logic generate email contents 2. sendEmail – this bean has logic to sending email 3. removeSentEmail – this bean has logic to remove sent email Make sure these beans present with correct name in your core-spring.xml Sample code is attached below

hss_core_spring.xml.txt

Create an event and listener Java classes We can trigger the process by directly or using event driven pattern. Here trigger process by event driven pattern is explained. Event class is a DTO which should contain all the attributes which are present in the process model. Follow below steps to create event and listener class:     

In required email triggering place, trigger this event by setting required values which are need to be set to process model. In event listener class catch this event and create a process model using the process name which we have mentioned in the process.xml. Get all the values from the event and set it to the process model. Start the process using business service in the listener method. Make sure this listener is registered in the core-spring.xml

Refer sample event and Listener classes attached below

ContactUsEventListener.java

ContactUsEvent.java

hss_core_spring.xml.txt

Create an email context Java class and register in facadesspring.xml: 

 



We have to create two vm files for email body and email subject contents. .vm file is kind of .jsp file where html code design will be there for email contents. Vm file has their own syntax for if, else, for loops etc. Refer velocity template syntax in online. We will see .vm creation in the later sections. For each vm file we need context Java class to pass the Java objects to the .vm files from Java class so that we can display email contents based on our business logic in the .vm file. Email context class kind of controller class where you can put your Java object as a key value pair. Once email process is triggered, process will pass the created process model (which has required email contents) to the email context class. We can take value from process model and can send required values as a key value pair from email context java class to vm files. Association between email context class and corresponding vm file will be mentioned in the email page template creation steps.

To pass the Java object from email context Java class to .vmfile , use put (hybris OOTB method ) in the email context Java class as below: put(keyString, yourJavaObject)

We can also access our Java object using below syntax without using “put” method if we mentioned getter setter methods for our Java object in the email context Java also.

Syntax to access Java object from .vm file: ${ctx.yourJavaObject.requiredAttribute} Refer attached code to create context class. Make sure register your email context class in the .facades.xml.

hssfacades-spring.xml.txt

ContactUsFormEmailContext.java

Create an email body and email subject vm files: Create an email-body and email-subject vm files as attached and place it in the corresponding folder

email-contactUsFormBody.vm.txt

email-contactUsFormSubject.vm.txt

Create email page and associate it with email body and subject vm files: Creating email page steps are similar to normal cms page creation process. Create body vm file and subject vm file and put it in the corresponding folder. In the email page template we have to associate corresponding email-body vm file, email-subject file and their corresponding Java classes. Please refer the attached email-content.impex file.

email-contactUsFormSubject.vm.txt

email-contactUsFormBody.vm.txt

email-content.impex.txt

Steps to sending email in local host: 

For DEV/UAT/PROD we are using Rackspace server or any other server. That should have their own smtp server itself. Hence no need to mention ip address for it. Just mention as below that will handle by itself mail.smtp.server=localhost mail.smtp.port=25



Then we need to run the email content impex. Before run the email-content.impex, just change the from email attribute value as your/anyother valid born email id in the below impex as below INSERT_UPDATE EmailPage; $contentCV[unique=true];uid[unique=true];name;masterTemplate(uid, $contentCV);defaultPage;approvalStatus(code) [default='approved'];fromEmail[lang=en];fromName[lang=en] #Contact Us Form EmailPage ;;ContactUsFormEmail;Contact Us Form Email; ContactUsFormEmailTemplate; true;; [email protected]; [email protected] o o

  

Reason to change the from email value is that we are using BORN smtp server in our local. Hence we can’t set other domain email id as from email id. In DEV/UAT/PROD it should be your client helpdesk id.

Once all the above steps are done. Do ant clean all, start the server and update your system. Run the email conten.impex with online mode. Then trigger the mail as below in your required place. Make sure to inject basestoreservice, basesiteservice, CommonI18NService, eventservice in your Java class. final ContactUsEvent event = new ContactUsEvent(); event.setBaseStore(getBaseStoreService().getCurrentBaseStore()); event.setSite(getBaseSiteService().getCurrentBaseSite()); event.setLanguage(getCommonI18NService().getCurrentLanguage()); event.setCsTicketModel(csTicketModel); getEventService().publishEvent(event);



If process is triggered, mail will be sent to corresponding mail id. If mail is triggered properly, in the console there is an error saying that “could not sent email to the following server”  

This might be due to wrong smtp server details and port number in local.properties Might be firewall issue. SMTP port access might be blocked for your IP address. Please check with your admin support team.

Triggering email through HMC:      

Once email is triggered successfully, check your spam box and inbox. Once email is triggered successfully and If you want to change .vm file content, that can be done through HMC itself To Change the email body content: Go to WCMS folder in HMC Click Page template and change it to email template from dropdown Search your email page template id

      

In the result open your email page template online In that go to email tab, their you can find the email body vm file and subject vm file and you can edit their content from there. After changing the content no need to trigger the mail from website again. We can trigger it from using HMC. To trigger email process: Go to System in HMC Click the Business process Search your business process which you already triggered using process name as below

Click your process and click the repair process tab. Then select the your process to trigger email as below



Then click start. Your mail will be sent

Steps to add cc mail: To add cc mails in your email follow the below steps: 

Create a class which extend SendEmailAction(OOTB class) class and override executeAction() method as attached below code.

HSSSendEmailAction.java



Change the sendEmail bean class as yours in the core-spring.xml as below



Tips to avoid error:     

First define and create your process model and then proceed further steps Make sure process names are correctly used in the required place Make sure process beans, email context beans and all related beans are registered in the corresponding spring xml Make sure smtp server details are correctly configured Make sure from email id should be our born domain email id in the email page model if you are testing email in your local environment.

Thanks…………