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 :
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
<itemtype code="CustomerDetailsEmailProcess" extends="StoreFrontCustomerProcess" jaloclass="com.hybris.training.core.jalo.process.CustomerDetailsEmailProcess">
-
<attributes>
-
<attribute qualifier="firstName" type="java.lang.String">
-
<persistence type="property" />
-
</attribute>
-
<attribute qualifier="lastName" type="java.lang.String">
-
<persistence type="property" />
-
</attribute>
-
<attribute qualifier="emailId" type="java.lang.String">
-
<persistence type="property" />
-
</attribute>
-
</attributes>
-
</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.
​
-
<?xml version="1.0" encoding="utf-8"?>
-
<process xmlns="http://www.hybris.de/xsd/processdefinition" start="generateCustomerDetailsEmail" name="customerDetailsEmailProcess" processClass="com.hybris.core.model.process.CustomerDetailsEmailProcess" onError="error">
-
<action id="generateCustomerDetailsEmail" bean="generateCustomerDetailsEmail">
-
<transition name="OK" to="sendEmail"/>
-
<transition name="NOK" to="error"/>
-
</action>
-
<action id="sendEmail" bean="sendEmail">
-
<transition name="OK" to="removeSentEmail"/>
-
<transition name="NOK" to="failed"/>
-
</action>
-
<action id="removeSentEmail" bean="removeSentEmail">
-
<transition name="OK" to="success"/>
-
<transition name="NOK" to="error"/>
-
</action>
-
<end id="error" state="ERROR">Something went wrong.</end>
-
<end id="failed" state="FAILED">Could not send Customer Details email.</end>
-
<end id="success" state="SUCCEEDED">Sent Customer Details email.</end>
-
</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.
​
-
public class CustomerDetailsEmailContext extends AbstractEmailContext
-
{
-
private String firstName;
-
private String lastName;
-
private String emailId;
-
-
@Override
-
public void init(final StoreFrontCustomerProcessModel processModel, final EmailPageModel emailPageModel)
-
{
-
super.init(processModel, emailPageModel);
-
if (processModel instanceof CustomerDetailsEmailProcessModel)
-
{
-
put("firstName",((CustomerDetailsEmailProcess) processModel).getFirstName());
-
put("lastName",((CustomerDetailsEmailProcess) processModel).getLastName());
-
put("emailId",((CustomerDetailsEmailProcess) processModel).getEmailId());
-
}
-
}
-
public String getFirstName() {
-
return firstName;
-
}
-
public void setFirstName(String firstName) {
-
this.firstName = firstName;
-
}
-
public String getLastName() {
-
return lastName;
-
}
-
public void setLastName(String lastName) {
-
this.lastName = lastName;
-
}
-
public String getEmailId() {
-
return emailId;
-
}
-
public void setEmailId(String emailId) {
-
this.emailId = emailId;
-
}
-
}
​
​
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 customerDetailsEmailProcessModel= getBusinessProcessService().createProcess("CustomerEmailProcess"+System.currentTimeMillis(), "customerDetailsEmailProcess");
​
customerDetailsEmailProcessModel.setFirstName("fname");
customerDetailsEmailProcessModel.setLastName("lname");
customerDetailsEmailProcessModel.setEmailId("testemail@gmail.com");
getModelService().save(customerDetailsEmailProcessModel);
getBusinessProcessService().startProcess(customerDetailsEmailProcessModel);
​
​
​
​
​
​