Class MailTemplate
- java.lang.Object
-
- com.day.cq.commons.mail.MailTemplate
-
public class MailTemplate extends java.lang.ObjectTheMessageTemplateclass provides email text templating functionality. Templates arent:filenodes in the repository representing a text file.The text file contains the complete email. Email headers are defined as the first lines of the file in the format
Header-Name: Header-Value, one header per line. Headers supporting multiple values can thus have several header lines. The supported headers are the standard email headers.After the last header line put an empty line and start the email body afterwards.
Within all of the text file, replacement variables can be used in the form of
${variable}, e.g.${payload.title}. The available variables are defined by the variable resolver given in thegetEmail(org.apache.commons.lang.text.StrLookup, Class)method.The
getEmailmethod returns the chosen (typeargument) email implementation, as long as the type extendsEmailand has a publically accessible default constructor. Out of the box the three email implementations provided by the Apache Commons Email library can be used:SimpleEmail,HtmlEmailandMultiPartEmail.
-
-
Constructor Summary
Constructors Constructor Description MailTemplate(java.io.InputStream inputStream, java.lang.String encoding)Constructs a newMailTemplatewith the template text given as theinputStreamand the encoding (may be null).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static MailTemplatecreate(java.lang.String path, javax.jcr.Session session)Convenience method to create a newMailTemplatebased on thepathidentifying the location of the email template text in the repository.<T extends Email>
TgetEmail(java.util.Map<java.lang.String,java.lang.String> variables, java.lang.Class<T> type)Create anEmailbased on the template text and replacing variables in the template text.<T extends Email>
TgetEmail(StrLookup lookup, java.lang.Class<T> type)Deprecated.UsegetEmail(java.util.Map, Class)instead.
-
-
-
Constructor Detail
-
MailTemplate
public MailTemplate(java.io.InputStream inputStream, java.lang.String encoding) throws java.io.IOExceptionConstructs a newMailTemplatewith the template text given as theinputStreamand the encoding (may be null).- Parameters:
inputStream- The template text.encoding- The encoding of the input stream (may be null). If empty, UTF-8 will be used.- Throws:
java.io.IOException- If an error occurs handling the input stream.
-
-
Method Detail
-
getEmail
public <T extends Email> T getEmail(java.util.Map<java.lang.String,java.lang.String> variables, java.lang.Class<T> type) throws java.io.IOException, MessagingException, EmailException
Create anEmailbased on the template text and replacing variables in the template text. Emails are constructed and returned with the giventype. If thetypeisHtmlEmail(or a subclass), and the template content appears to be HTML, it is used as the HTML part of the email message. The text part of the email message is constructed from doing a basic HTML to plain text conversion.- Type Parameters:
T- The email type.- Parameters:
variables- The variables to use for variable substitution ornullif no variables are needed.type- The class defining the email type.- Returns:
- An email based on the template text and the variables.
- Throws:
java.io.IOException- If an error occurs handling the text template.MessagingException- If an error occurs during building the email message.EmailException- If an error occurs during building the email.- Since:
- 5.8
-
getEmail
@Deprecated public <T extends Email> T getEmail(StrLookup lookup, java.lang.Class<T> type) throws java.io.IOException, MessagingException, EmailException
Deprecated.UsegetEmail(java.util.Map, Class)instead.Create anEmailbased on the template text and replacing variables in the template text using the givenlookupimplementation. Emails are constructed and returned with the giventype. If thetypeisHtmlEmail(or a subclass), and the template content appears to be HTML, it is used as the HTML part of the email message. The text part of the email message is constructed from doing a basic HTML to plain text conversion.- Type Parameters:
T- The email type.- Parameters:
lookup- TheStrLookupimplementation to use for variable lookup.type- The class defining the email type.- Returns:
- An email based on the template text and the variable resolver.
- Throws:
java.io.IOException- If an error occurs handling the text template.MessagingException- If an error occurs during building the email message.EmailException- If an error occurs during building the email.java.lang.IllegalArgumentException- Iflookupisnull.
-
create
public static MailTemplate create(java.lang.String path, javax.jcr.Session session)
Convenience method to create a newMailTemplatebased on thepathidentifying the location of the email template text in the repository.- Parameters:
path- The location of the email template text in the repository. Must point to an nt:file node.session- The session used for accessing the repository.- Returns:
- A mail template or
nullif there was an error creating the template.
-
-