Email Templates API

You can use the following methods from the EmailTemplates interface:

  • EmailInfo generateEmail(EmailTemplate emailTemplate, Collection<ReportWithParams> params) creates EmailInfo from a template that may contain the same reports with different parameter values.

  • EmailInfo generateEmail(EmailTemplate emailTemplate, Map<String, Object> params) creates EmailInfo by parameters map for all included reports.

  • void checkParameterTypeChanged(ReportInputParameter inputParameter, ParameterValue parameterValue) checks that the report input parameter did not change its parameter type.

The EmailTemplate entity contains the subject, body, and attachments. It also has from, to, cc, bcc addresses.

The ReportWithParams is a wrapper class that represents a report and a map of parameters for that report.

The ParameterValue is a class that provides a string representation of the parameter with alias and type.

The ReportInputParameter is a class of Jmix Reports add-on.

The EmailInfo is a class of Jmix Email Sending Add-on.

Email Templates Builder

Email templates API contains a builder that can create and fill the EmailTemplate entity.

EmailTemplateBuilderImpl is an implementation of EmailTemplateBuilder that provides intermediate methods for setting and adding email template properties. It also contains terminal methods that can build EmailTemplate, generate, or send EmailInfo.

A copy of the specified EmailTemplate is created in the corresponding setter in the EmailTemplateBuilderImpl class. Every intermediate method fills the created copy.

public void setEmailTemplate(EmailTemplate emailTemplate) {
    this.emailTemplate = cloneTemplate(emailTemplate);
}

The build() method creates the copy from the copy inside the builder. It is necessary to save the state of the existed entity or builder.

Example of using the builder:

EmailTemplate newTemplate = emailTemplates.buildFromTemplate(CREATED_TEMPLATE_CODE)
        .setSubject("Test subject")
        .setTo("address@haulmont.com")
        .setBodyParameter("subscription", subscription)
        .setBodyParameter("customer", subscription.getCustomer())
        .build();