Sending email in SAP Commerce(Hybris)

Every store needs to send emails for order approval, registration, password reset and so on. Email sending is the one of the major and longest process in SAP Commerce(Hybris).

In this article i will try to explain how to:

  1. Send email;
  2. Send email with attachments;
  3. Send email to multiple addresses.

Sending email in SAP Commerce(Hybris)

  1. As a first step we create item type which extends StoreFrontCustomerProcess(or just StoreFrontProcess):

2. Then we create exampleEmailProcess.xml to define process actions:

3. We register the generateExampleEmail as a bean in Spring configuration file:

4. To add the process definition to the system we register it as ProcessDefinitionResource in Spring’s configuration file:

5. Then we create java class which extends CustomerEmailContext or AbstractEmailContext. The context object keeps the data that is required to generate an email(data passed to the template):

The init() method defines the model and its attributes that will be accessible from the email template and also sets the email address of the recipient.

6. The context object must be registered in Spring configuration file:

7. Now, we should create two vm templates: one for subject, one for body:

Subject content:

Body content:

8. Prepare impex for creating RendererTemplates for Subject and Body:

9. Add properties needed for smtp in our properties file:

10. Create Java class ExampleEvent which extends AbstractCommerceUserEvent:

11. Create ExampleEventListener which extends AbstractAccelaratorSiteEventListener< ExampleEvent >

After extending the AbstractAccelaratorSiteEventListener class we override onSiteEvent().

12. Register newly created EventListener as bean in Spring:

13. Create service implementation(sendExampleEmail()) for publishing the event which w e can call from our controller:

Sending email with attachments

Unfortunately, sending email with attachments in Hybris using Business Process is not supported by default, we need customizing to achieve this. Hybris OOTB EmailService which is responsible for sending email is supporting fields like attachments but they are not populated by EmailGenerationService into EmailMessageModel. Overriding generation service to fill all required data to EmailMessageModel help us solve the issue.

We define our custom attributes in BusinessProcess to store the values and access them later. Also, we have to override generate method of DefaultEmailGenerationService which responsible to generate EmailMessageModel.

  1. As we already have OOTB EmailAttachment itemType, we can use it for emailAttachments attribute. So, first we create a one-to-many(1..n) relation between BusinessProcessModel and EmailAttachmentModel in our *-item.xml.

2. Create a class which extends the DefaultEmailGenerationService and override the generate() method.

3. Register the CustomEmailGenerationService as a bean and point emailGenerationService alias to our class:

4. Update our ExampleEventListener to handle attachments:

Of course before that we have to add to our class ExampleEvent.java new property List<EmailAttachmentsModel> emailAttachments(point 10) and then update the initializeExampleEvent() method from point 13 to set attachments to event class. We use EmailService.createEmailAttachment() method to create attachment models.

Sending email to multiple addresses

Sending email to multiple addresses in Hybris using Business Process is also not supported by default, so again we need customizing to achieve this.

We again define our custom attributes in BusinessProcess to store the values and access them later. Also, we have to override generate method of DefaultEmailGenerationService the same way as we did above.

  1. We define attribute in BusinessProcessModel — we can use String collection for toAddresses, ccAddresses and bccAddresses:

Note that autocreate and generate are false as we want only to update this model.

2. And again we override generate() method of DefaultEmailGenerationService:

And then the steps for EventListener are similar as for email attachments. Check onSite() and initializeExampleEvent() from above examples.

Hope i did not miss any details. :)

--

--

--

Software Development Lead @Coca-Cola HBC

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

QuickCheck or Fuzzing? Which one to use?

Breaking Down Object Oriented Ruby

Deploying apps in Cloud Foundry

Exception Handling Isn’t Sexy

How I was able to list some internal information from PayPal #BugBounty

How to Install Akaunting on CentOS and Ubuntu

How Delta Lake and DLF Service Facilitate Real-time CDC Synchronization in a Data Lake

Why we love managed hosting with WP Engine

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nuray Fahri

Nuray Fahri

Software Development Lead @Coca-Cola HBC

More from Medium

Java features from version — 8 to 18

Liquibase

Functional Programming in Java

SINGLETON DESIGN PATTERN