Sending Emails with attachments in SAP Hybris

Hybris Logo

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).

 

3.6 5 votes
Article Rating
Subscribe
Notify of
guest

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

13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
CHARAFI
CHARAFI
6 years ago

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

CHARAFI
CHARAFI
Reply to  Mouad EL Fakir
6 years ago

Hello Mouad,
I don’t know how to thank you. You make our life easier.
you don’t have an example about interceptors for example create our custom interceptors ?

CHARAFI
CHARAFI
Reply to  Mouad EL Fakir
6 years ago

And hou about to manipulate widget in the backoffice and adding custom widgets ?

CHARAFI
CHARAFI
Reply to  Mouad EL Fakir
6 years ago

Ok thank you very much

Prashanth
Reply to  Mouad EL Fakir
5 years ago

Hi Mouad pdf is not getting attached in email

FELIPE MARCELO DE JESUS LIMA
FELIPE MARCELO DE JESUS LIMA
5 years ago

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);

Mrunal Khatavkar
Mrunal Khatavkar
3 years ago

Hi ,

Thanks for sharing such a useful document. May I know how we can restrict jpg/jpeg attachments in the mail ?

Thanks in advance

Miles
Miles
1 year ago

Hi, I was adding attachment with file name having German chars, but when I received the mail, the file name is having somo other chars.. looks like decoding is not happening.

MSalian
MSalian
8 months ago

CC email address can also be added inside the generate method

13
0
Would love your thoughts, please comment.x
()
x