Documentation Home

Email and SMS Templates

As of 3.3.0-GA of this module there is now domain and services around email and sms templates. These additions were made
in order to provide the ability to manage templates for email and sms notifications within the admin.

Configuration

In order to utilize the email and sms templates, there is some configuration and reorganization that needs to be done.

  1. Configure your email MessageCreator blMessageCreator. Typically, we use BroadleafThymeleaf3MessageCreator

    @Autowired
    @Bean
    public MessageCreator blMessageCreator(@Qualifier("blEmailTemplateEngine") TemplateEngine emailTemplateEngine) {
        return new BroadleafThymeleaf3MessageCreator(emailTemplateEngine, (JavaMailSender) blMailSender());
    }
    

    Note: Our example requires adding a dependency on broadleaf-thymeleaf3-presentation

  2. Move any of your existing email templates to classpath:/emailTemplates/* and rename to be all lower-case and underscore-delimited, e.g. order_confirmation_email.html.

  3. Configure your SMSMessageCreator blSMSMessageCreator; by default, this a noop, so you need to configure this to send SMS notifications using your SMS service.

Adding Templates

Adding a new email template (or sms template) is relatively straightforward. One thing I recommend is starting out with your html template
on the classpath and then moving it to the database later. Let's walk through adding an example order confirmation email template.

  1. Add your html email template file to classpath:/emailTemplates with the name order_confirmation_email.html
  2. Insert this data into your database:

    INSERT INTO BLC_EMAIL_TEMPLATE (EMAIL_TEMPLATE_ID, NAME, TYPE, ACTIVE_START_DATE, EMAIL_FROM, SUBJECT_TEMPLATE) VALUES (-22000, 'Order Confirmation Email', 'ORDER_CONFIRMATION', CURRENT_TIMESTAMP, 'noreply@mycompany.com', 'Your order ##{order.orderNumber} has been received!');
    

    Note: if you take the NAME column, make it lowercase, and replace spaces with underscores, that should result in the template name, order_confirmation_email.

  3. Go to /admin/email-templates/-22000 to view the entity form for your new template. You might notice that the template on the classpath is loaded into the template tab for you automatically. This makes it easy for a CMS user to modify existing classpath templates and save them in the database.

Setting Global Defaults

There are often times where you want to use a default from address or subject for a type of email. These defaults can be
defined in a couple different ways.

  1. Define a system property with the following pattern: emailTemplates.order_confirmation.from=noreply@mycompany.com. See com.broadleafcommerce.advancedcms.notification.service.EmailTemplateNotificationServiceImpl.getEmailTemplateProperty.
  2. Define an EmailInfo bean with the corresponding emailType:

    @Bean
    public EmailInfo blOrderConfirmationEmailInfo() {
        EmailInfo emailInfo = new EmailInfo();
        emailInfo.setFromAddress("noreply@mycompany.com");
        emailInfo.setEmailType(NotificationEventType.ORDER_CONFIRMATION.getType());
        return emailInfo;
    }
    

Content Targeting

Email and SMS templates are content targetable entities. This means that you can create a content targeter to swap out which
email template is used based on a set of rules.

When you create an email template there is a question asking if your template is "Only available for content targeting?".
If you answer yes, then this template will only ever be used if it is swapped out for another template. If you answer no,
then this template will always be used for that notification type as long as it is active, and it is not content targetable.

A good example of this is an order confirmation email. You have one order confirmation email template that is for anonymous users;
this email template should be marked as NOT content targetable. You then have another order confirmation email template that
is for registered users; this email template should be marked as content targetable. You can then go to the Content Targeters
admin section, and add a new targeter to swap the anonymous template for the registered template when the customer is a registered customer.

Note: Be careful when using content targeters to swap email templates. Not all the rules will apply in the context of
each email type. Rules based on product, or category, or even request attributes probably will not work as expected.

Limitations

There are some limitations to this new domain and functionality.

  1. Content targeting will not work properly in a non-request context. Currently the rule engine used by the content targeters is heavily dependent on the existence of a request; if there is no request, such as in a scheduled job, then content targeting will not work correctly.