Search code examples
javaemailquarkusmailerquarkus-qute

Addition of new line in Quarkus Qute Template


In my Quarkus project, I am sending an qute template in an email using mail object.

The data is dynamically added to qute template from my code before sending the mail. The sample template is

<html>
<head>
</head>

<body class="body">
<br>
    Hi <b>Receiver</b>,
    <br><br>
        {body}
    <br><br>
</body>
</html>

The email body is added from code using this

@Inject
@Location("sampleMail")
MailTemplate mailObject;

public void sendMail() {

  String emailBody = "First line <br>" +
  "Second line \n" +
  "Third line \\n" + System.lineSeparator() +
  "Fourth line";

  mailObject.to(recipient)
    .subject("Default subject")
    .data("body", emailBody)
    .send().subscribe().with(
      success -> logger.info("Message sent"),
      fail -> logger.error("Exception while sending mail", fail));
  }
}

Even after trying <br>, \n, \\n, System.lineSeparator() in body string, the new line is not rendered in qute template in the html that is being sent in the mail. All lines are present in one single line, new line is not created. Have checked the quarkus guides but there was no mention about this.

Any solution or suggestions for solving this issue ?


Solution

  • I believe your problem is related to your values being treated as text by Qute when rendering your template.

    The <br> should work if you replace your {body} by {body.raw}, like this :

    <html>
    <head>
    </head>
    
    <body class="body">
    <br>
        Hi <b>Receiver</b>,
        <br><br>
            {body.raw}
        <br><br>
    </body>
    </html>
    

    And of course :

    String emailBody = "First line <br>" +
      "Second line <br>" +
      "Third line <br>" +
      "Fourth line";