Using Spring Mail with Mustache Templates

Using Mustache Templates to Send Email from a Spring Boot Application

In this post, I’m going to take us through an example Spring Boot application that sends a neatly formatted email using Mustache Templates. Mustache templates are a “logic-less” templating system.  Our example application is a simple Spring Boot application that collects some input from the user and uses it to send an email using a Mustache template.

Gradle Dependencies

The two dependencies relevant to our application are the Spring Boot starter for mail and the mustache template compiler.

implementation('org.springframework.boot:spring-boot-starter-mail')
implementation('com.github.spullara.mustache.java:compiler:0.9.5')

Our Template

Our template is in a file called email.mustache in the resources folder.  It displays the email subject and a list of tasks.  See the documentation for more about the syntax.

{{subject}}

Hello - Here is your daily task list.

{{#tasks}}
- {{.}}
{{/tasks}}

Properties for Email

To use email within Spring Boot, we need to specify a handful of properties.  This example is using gmail, so if you’re using a different provider your set up will be different.

If you’re using gmail, see these instructions to set up an App Password for your gmail account.

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=[your username]
spring.mail.password=[your email password]
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

The EmailDTO

Our DTO matches up with our input page of our application. The user can provide a subject, a to address and any number of tasks.

A quick note on security:  In a real world application, we’d need a lot more validation and probably some white-listing for our input.

public class EmailDto {
   @Email
   public String to;
   public String subject;
   public List tasks;

   public EmailDto() {
      tasks = new ArrayList();
      tasks.add("");
   }

   //Getters, setters and a toString

}

The Email Controller

The EmailController is where we send our email.  To send an email we need to Autowire the JavaMailSender.

@Controller
public class EmailController {
   @Autowired
   private JavaMailSender mailSender;

   @RequestMapping("/")
   public String displayEmailPage(EmailDto emailDto) {
      return "sendEmail";
   }

To use a Mustache template, we first compile it using the MustacheFactory.  Because SimpleMailMessage takes a string for the email text, we call execute on our mustache object with a StringWriter and our EmailDto.  Then we can just write that to a string and put it in our email object.

   @RequestMapping(value = "send", method = RequestMethod.POST)
   public String sendEmail(@Valid EmailDto emailDto) {

      MustacheFactory mf = new DefaultMustacheFactory();
      Mustache m = mf.compile("email.mustache");

      StringWriter writer = new StringWriter();
      String messageText = "";
      try {
         m.execute(writer, emailDto).flush();
         messageText = writer.toString();
      } catch (IOException e) {
         e.printStackTrace();
      }

      SimpleMailMessage message = new SimpleMailMessage();
      message.setTo(emailDto.getTo());
      message.setSubject(emailDto.getSubject());
      message.setText(messageText);

      mailSender.send(message);

      return "sendEmail";
   }

See it in Action

So let’s fill in the form.

email_mustache_user_input

Now, we should get an email that looks like this:

email_mustache_email

Conclusion

In this post, we went over how to use Spring’s built in email functionality via the Spring Boot starter with a Mustache template for formatting.  We saw how to layout the template, talked about where it should go and demonstrated how to use it as the body of an email.

The full example is over on GitHub.

Advertisements