top of page

Overview

​

Email processes in Hybris typically involve sending transactional emails to customers based on specific events or triggers within the e-commerce platform. These emails can include order confirmations, shipping notifications, password resets, and more. Below, I'll describe the general process of setting up an email process in Hybris, along with an example.

​

Email process components :

  1.  Email Template Creation: Start by creating an email template for the type of email you want to send. This template will define the email's structure, content, and placeholders for dynamic information like customer names, order details, etc. Email templates can be created using Hybris' back-office or CMS Cockpit.

  2. Event Trigger: Identify the event or trigger that will initiate the email process. For example, when a customer places an order, this event triggers an order confirmation email.

  3. Listeners and Observers: Configure listeners and observers to capture the event. In Hybris, listeners and observers are components that monitor specific events and take action when they occur.

  4. Business Logic: Implement the business logic to determine when and under what conditions the email should be sent. For example, you may want to send an order confirmation email only when the payment is successful.

  5. Email Sending Service: Utilize Hybris' email sending service or a custom service to send emails. You will reference the email template created earlier and populate it with dynamic data.

  6. Dynamic Data Population: Populate the email template with dynamic data based on the specific event. For instance, if you're sending an order confirmation email, you'll include order details like products purchased, shipping address, and the customer's name.

  7. Sending the Email: Use the configured email sending service to send the email to the customer's email address. Ensure that the email contains all relevant information and is formatted correctly.

  8. Logging and Error Handling: Implement logging and error handling mechanisms to track email sending status and manage any issues that may arise during the process.

 

 

Email Process Creation Steps

​

We have divided the email generation process in following steps.  

​

 

 

 

 

 

 

 

 

 

 

 

 

 

Step 1: We will first decide the content we are going to send in the email. In this example we will send first name, last name and email id of the customer as part of email body. 

​

Step 2: Create a custom process type which will extend the StoreFrontCustomerProcess. Define the attribute which we have to send as part of email.

 

  1. <itemtype code="CustomerDetailsEmailProcess" extends="StoreFrontCustomerProcess" jaloclass="com.hybris.training.core.jalo.process.CustomerDetailsEmailProcess">

  2. <attributes>

  3. <attribute qualifier="firstName" type="java.lang.String">

  4. <persistence type="property" />

  5. </attribute>

  6. <attribute qualifier="lastName" type="java.lang.String">

  7. <persistence type="property" />

  8. </attribute>

  9. <attribute qualifier="emailId" type="java.lang.String">

  10. <persistence type="property" />

  11. </attribute>

  12. </attributes>

  13. </itemtype>

​

​

Step 3: Define the email business process xml with name customerDetailsEmailProcess.xml. This file will contain the all the details related to the difference steps involved in this email process. 

​

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <process xmlns="http://www.hybris.de/xsd/processdefinition" start="generateCustomerDetailsEmail" name="customerDetailsEmailProcess" processClass="com.hybris.core.model.process.CustomerDetailsEmailProcess" onError="error">

  3. <action id="generateCustomerDetailsEmail" bean="generateCustomerDetailsEmail">

  4. <transition name="OK" to="sendEmail"/>

  5. <transition name="NOK" to="error"/>

  6. </action>

  7. <action id="sendEmail" bean="sendEmail">

  8. <transition name="OK" to="removeSentEmail"/>

  9. <transition name="NOK" to="failed"/>

  10. </action>

  11. <action id="removeSentEmail" bean="removeSentEmail">

  12. <transition name="OK" to="success"/>

  13. <transition name="NOK" to="error"/>

  14. </action>

  15. <end id="error" state="ERROR">Something went wrong.</end>

  16. <end id="failed" state="FAILED">Could not send Customer Details email.</end>

  17. <end id="success" state="SUCCEEDED">Sent Customer Details  email.</end>

  18. </process>

​

​

Step 4: Register generateCustomerDetailsEmail as bean in xml file and map the frontendTemplateName value as below. 

​

<bean id="generateCustomerDetailsEmail" parent="abstractGenerateEmailAction">

<property name="frontendTemplateName" value="CustomerDetailsEmailTemplate"/>

</bean>

​

​

Step 5: Register the process bean of ProcessDefinitionResource, hence it will recognized by Hybris Process Engine. 

​

<bean id="customerDetailsEmailProcessDefinitionResource" class="de.hybris.platform.processengine.definition.ProcessDefinitionResource" >

<property name="resource" value="classpath:/traingingcore/processes/customerDetailsEmailProcess.xml"/>

</bean>

​

​

Step 6: Create a email context class for the email by extending the AbstractEmailContext. This email context class will hold all the data population logic for the attributes which will be rendered in the email vm file.

​

  1. public class CustomerDetailsEmailContext extends AbstractEmailContext

  2. {

  3. private String firstName;

  4. private String lastName;

  5. private String emailId;

  6.  

  7. @Override

  8. public void init(final StoreFrontCustomerProcessModel processModel, final EmailPageModel emailPageModel)

  9. {

  10. super.init(processModel, emailPageModel);

  11. if (processModel instanceof CustomerDetailsEmailProcessModel)

  12. {

  13. put("firstName",((CustomerDetailsEmailProcess) processModel).getFirstName());

  14. put("lastName",((CustomerDetailsEmailProcess) processModel).getLastName());

  15. put("emailId",((CustomerDetailsEmailProcess) processModel).getEmailId());

  16. }

  17. }

  18. public String getFirstName()  {

  19. return firstName;

  20. }

  21. public void setFirstName(String firstName) {

  22. this.firstName = firstName;

  23. }

  24. public String getLastName()  {

  25. return lastName;

  26. }

  27. public void setLastName(String lastName) {

  28. this.lastName = lastName;

  29. }

  30. public String getEmailId()  {

  31. return emailId;

  32. }

  33. public void setEmailId(String emailId) {

  34. this.emailId = emailId;

  35. }

  36. }

​

​

Step 7: Register the CustomerDetailsEmailContext in the spring.xml file. 

​

<bean id="customerDetailsEmailContext" class="com.hybris.training.facades.process.email.context.CustomerDetailsEmailContext" parent="abstractEmailContext" scope="prototype" >

</bean>

​

 

Step 8: Define the Email Template file for email. Create 2 Velocity templates, one for the email Subject and the other for the Body.

 

<%-- .../trainingcore/import/emails/email-customerDetails-subject.vm --%>

${ctx.messages.emailSubject}

​

<%-- .../trainingcore/import/emails/email-customerDetails-body.vm --%>

<html>

<head>

</head>

<body bgcolor="#ffffff">

<h1>Hello   ${ctx.firstName}  ${ctx.lastName}</h1>

<h1>Email Id   ${ctx.emailId}</h1>

<p>The above details contains customer name and email id</p>

</body>

</html>

​

​

Step 9: We will define the impex files related to email.

​

# Create Template Renderers
INSERT_UPDATE RendererTemplate    ;code[unique=true] ;contextClass ;templateScript[lang=en,translator=de.hybris.platform.commerceservices.impex.impl.FileLoaderValueTranslator];rendererType(code)[default='velocity']
                       ;email-customerDetails-body       ;$emailPackageName.CustomerDetailsEmailContext    ;$emailResource/email-customerDetails-body.vm
                       ;email-customerDetails-subject    ;$emailPackageName.CustomerDetailsEmailContext    ;$emailResource/email-customerDetails-subject.vm

 

# Create Email page Template
INSERT_UPDATE EmailPageTemplate    ;$contentCV[unique=true];uid[unique=true]        ;active    ;frontendTemplateName    ;subject(code)                            ;htmlTemplate(code)        ;restrictedPageTypes(code)
                               ;                        ;CustomerDetailsEmailTemplate;true    ;CustomerDetailsEmail    ;email-customerDetails-subject                ;email-customerDetails-body    ;EmailPage

 

# Create Email Page
INSERT_UPDATE EmailPage    ;$contentCV[unique=true];uid[unique=true]    ;masterTemplate(uid,$contentCV);approvalStatus(code)[default='approved']
                       ;                        ;CustomerDetailsEmail    ;CustomerDetailsEmailTemplate;

​

​

 

Step 10: SMTP server mail set up by configurations in local.properties. 

​

emailservice.send.enabled = true

mail.from = xxx@gmail.com

mail.replyto = xxx@gmail.com

mail.smtp.server = smtp.gmail.com

mail.smtp.port = 

mail.smtp.user = xxx@gmail.com

mail.smtp.password = password

​

 

 

Step 11: Sending a CustomerDetailsEmail, starting a new instance of the Customer Details Email Process.

​

CustomerDetailsEmailProcessModel customerDetailsEmailProcessModelgetBusinessProcessService().createProcess("CustomerEmailProcess"+System.currentTimeMillis(), "customerDetailsEmailProcess");

​

customerDetailsEmailProcessModel.setFirstName("fname"); 

customerDetailsEmailProcessModel.setLastName("lname"); 

customerDetailsEmailProcessModel.setEmailId("testemail@gmail.com"); 

getModelService().save(customerDetailsEmailProcessModel); 

getBusinessProcessService().startProcess(customerDetailsEmailProcessModel);

​

​

​

​

​

 

​
 

hybris email
bottom of page