io.rocketbase.commons.email.EmailTemplateBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of email-template-builder Show documentation
Show all versions of email-template-builder Show documentation
let this service generate your custom html/text emails
The newest version!
package io.rocketbase.commons.email;
import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import io.rocketbase.commons.email.model.HtmlTextEmail;
import io.rocketbase.commons.email.template.TemplateLine;
import io.rocketbase.commons.email.template.TemplateLine.TemplateLineType;
import lombok.SneakyThrows;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public final class EmailTemplateBuilder {
private static final PebbleEngine ENGINE = new PebbleEngine.Builder().build();
public static EmailTemplateConfigBuilder builder() {
return new EmailTemplateConfigBuilder();
}
@SneakyThrows
static HtmlTextEmail build(String fontFamily, List templateLines, ImageLine logo, HeaderConfig headerConfig, CopyrightConfig copyrightConfig) throws PebbleException {
PebbleTemplate htmlTemplate = ENGINE.getTemplate("templates/email/base.html");
PebbleTemplate textTemplate = ENGINE.getTemplate("templates/email/base.txt");
Map template = new HashMap<>();
template.put("fontFamily", fontFamily);
template.put("logo", logo);
template.put("header", headerConfig);
template.put("lines", templateLines.stream()
.filter(l -> !l.getType().equals(TemplateLineType.FOOTER))
.collect(Collectors.toList()));
template.put("footers", templateLines.stream()
.filter(l -> l.getType().equals(TemplateLineType.FOOTER))
.collect(Collectors.toList()));
template.put("copyright", copyrightConfig);
Writer htmlWriter = new StringWriter();
htmlTemplate.evaluate(htmlWriter, template);
Writer textWrite = new StringWriter();
textTemplate.evaluate(textWrite, template);
return new HtmlTextEmail(htmlWriter.toString(), textWrite.toString());
}
public static class EmailTemplateConfigBuilder {
private String fontFamily = "'Helvetica Neue',Helvetica,Arial,sans-serif";
private List templateLines = new ArrayList<>();
private ImageLine logo;
private HeaderConfig headerConfig;
private CopyrightConfig copyrightConfig;
public EmailTemplateConfigBuilder fontFamiliy(String fontFamily) {
this.fontFamily = fontFamily;
return this;
}
public ImageLine logo(String src, String alt, int width, int height) {
logo = new ImageLine(this, src, alt, width, height);
return logo;
}
public HeaderConfig header(String title) {
headerConfig = new HeaderConfig(this, title);
return headerConfig;
}
/**
* @param
*/
public TableConfig addTable() {
TableConfig tableConfig = new TableConfig(this);
templateLines.add(tableConfig);
return tableConfig;
}
/**
* @param textOrHtml detects whether text or html
* sometime the detection is not working as expected. then you can use addPlainText or addHtml to overrule the detection
*/
public TextLine addText(String textOrHtml) {
TextLine line = new TextLine(TemplateLineType.TEXT, this, textOrHtml);
templateLines.add(line);
return line;
}
private TextLine addText(String textOrHtml, boolean asHtml) {
TextLine line = new TextLine(TemplateLineType.TEXT, this, textOrHtml, asHtml);
templateLines.add(line);
return line;
}
/**
* @param html HTML formatted text
*/
public TextLine addHtml(String html) {
return addText(html, true);
}
/**
* @param text text that shall be shown exactly as given - e.g. it will be encoded when it shall be shown as HTML
*/
public TextLine addPlainText(String text) {
return addText(text, false);
}
public ButtonLine addButton(String text, String url) {
ButtonLine line = new ButtonLine(this, text, url);
templateLines.add(line);
return line;
}
public ImageLine addImage(String src, String alt, int width, int height) {
ImageLine line = new ImageLine(this, src, alt, width, height);
templateLines.add(line);
return line;
}
public ImageLine addImageFullWidth(String src, String alt) {
ImageLine line = new ImageLine(this, src, alt);
templateLines.add(line);
return line;
}
/**
* @param textOrHtml detects whether text or html
* sometime the detection is not working as expected. then you can use addPlainText or addHtml to overrule the detection
*/
public TextLine addFooter(String textOrHtml) {
TextLine line = new TextLine(TemplateLineType.FOOTER, this, textOrHtml);
templateLines.add(line);
return line;
}
private TextLine addFooter(String textOrHtml, boolean asHtml) {
TextLine line = new TextLine(TemplateLineType.FOOTER, this, textOrHtml, asHtml);
templateLines.add(line);
return line;
}
/**
* @param html HTML formatted text
*/
public TextLine addHtmlFooter(String html) {
return addFooter(html, true);
}
/**
* @param text text that shall be shown exactly as given - e.g. it will be encoded when it shall be shown as HTML
*/
public TextLine addPlainTextFooter(String text) {
return addFooter(text, true);
}
public CopyrightConfig copyright(String name) {
copyrightConfig = new CopyrightConfig(this, name);
return copyrightConfig;
}
public HtmlTextEmail build() {
return EmailTemplateBuilder.build(fontFamily, templateLines, logo, headerConfig, copyrightConfig);
}
}
}