Sending Emails with attachments in SAP Hybris

Hybris featured image

1. Overview

In the last article, we’ve explored how to send a basic Email in Hybris using Business process.

In this article, I will show you how to send an Email with attachments in Hybris.

Note, that this article completes the last one, I invite you to take a quick look on it before your start this one.

Unfortunately, sending Emails with attachments files in Hybris using Business Process is not supported by default, you need to customize and/or override a bunch of o classes to make it.

2. Implementation

1. First of all, create a one-to-many relation between BusinessProcessModel and EmailAttachmentModel.

Add this relation to your *-items.xml and run ant all command.

<!-- ...\hybris\bin\custom\training\trainingcore\resources\trainingcore-items.xml -->

<relation code="BusinessProcess2EmailAttachmentsRel" localized="false" autocreate="true" generate="true">
	<description>This relation links business process with the attachments.</description>
	
	<sourceElement type="BusinessProcess" qualifier="process" cardinality="one" />
	
	<targetElement type="EmailAttachment" qualifier="attachments" cardinality="many" collectiontype="list">
		<modifiers partof="true" />
	</targetElement>
</relation>

2. Override the DefaultEmailGenerationService to customize the default Email generation logic.

Create a Java class called AttachmentEmailGenerationService extends the DefaultEmailGenerationService, and override the generate() method.

package com.stackextend.training.core.email;

import ...;

public class AttachmentEmailGenerationService extends DefaultEmailGenerationService {

	private static final Logger LOG = Logger.getLogger(AttachmentEmailGenerationService.class);

	@Override
	public EmailMessageModel generate(final BusinessProcessModel businessProcessModel, final EmailPageModel emailPageModel) throws RuntimeException {

	    // call super.generate() to fill other fields
	    EmailMessageModel emailMessage = super.generate(businessProcessModel, emailPageModel);

        // retrieve attachments from business process and add them to the email message
        List<EmailAttachmentModel> attachments = businessProcessModel.getAttachments();
        emailMessage.setAttachments(attachments);

        // re-save the emailMessage (Thanks Mercelo)
        getModelService().saveAll(emailMessage);

        // return email message
        return emailMessage;
	}
}

3. Register the AttachmentEmailGenerationService as a bean in Spring, with the emailGenerationService as an alias.

<alias alias="emailGenerationService" name="attachmentEmailGenerationService"/>
<bean id="attachmentEmailGenerationService" 
	class="com.stackextend.training.core.email.AttachmentEmailGenerationService"
	parent="defaultEmailGenerationService">
</bean>

4. Finally, add the attachments to the Business Process and start it, to send the Email.

// Create a new instance of the process
HelloWorldProcessModel helloWorldProcessModel = (HelloWorldProcessModel) getBusinessProcessService()
	.createProcess("helloWorld-" + System.currentTimeMillis(), "helloWorldEmailProcess");

// Fill the process with the appropriate data
helloWorldProcessModel.setSite(...);
helloWorldProcessModel.setCustomer(...);
helloWorldProcessModel.setLanguage(...);
helloWorldProcessModel.setCurrency(...);
helloWorldProcessModel.setStore(...);

// Add attachment to the business process
// 1. Convert your File as InputStream
DataInputStream inputStream; // new FileInputStream(myFile)
String filename; // ex: myFile.csv
String mimeType; // ex: text/csv

// 2. Create an email attachment from InputStream
EmailAttachmentModel attachment = getEmailService().createEmailAttachment(inputStream, filename, mimeType);

// 3. create new attachments list and fill it with attachment
List<EmailAttachmentModel> attachments = new ArrayList<>();
attachments.add(attachment);

// 4. set add attachments list to the business process
helloWorldProcessModel.setAttachments(attachments);

// Save the process
getModelService().save(helloWorldProcessModel);

// Then start the process = send the Email
getBusinessProcessService().startProcess(helloWorldProcessModel);

The code snippet above requires modelServicebusinessProcessService and emailService to be injected (Autowired).

 

10
Leave a Reply

avatar
2 Comment threads
8 Thread replies
4 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
FELIPE MARCELO DE JESUS LIMAPrashanthMouad EL FakirCHARAFI Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
CHARAFI
Guest
CHARAFI

Hello Mouad, first i want to thank for sharing these tutorials to help beginners in sap hybris. I followed the tutorials but I get this error by running the code.
missing values for [catalogVersion] in model EmailAttachmentModel () to create a new EmailAttachment

FELIPE MARCELO DE JESUS LIMA
Guest
FELIPE MARCELO DE JESUS LIMA

hi Mouad, how are u ?

It worked, but I had to make a change.
in the class that inherits the DefaultEmailGenerationService.

it was necessary, save the instance of emaiLmessage

emailMessage.setAttachments(Collections.singletonList(contactUsProcessModel.getEmailAttachment())); getModelService().saveAll(emailMessage);