@pdftron/ses-email-builder
v0.1.1
Published
A utility SDK for generating and sending emails via Amazon SES
Readme
AWS SES Email template builder
An isomorphic utility SDK to build and send emails via SES.
Installation
yarn add @pdftron/ses-email-builderfetch is also required to be available as a global. If using node, you can use the isomorphic-fetch package.
Usage
The default export of this package is a factory function that returns a Class you can use throughout your app to send emails. The factory function allows you to set global styles that will be applied to all emails, and the returned class lets you set more specific details per email.
Example usage:
const factory = require('@pdftron/ses-email-builder');
const EmailBuilder = factory({
...defaults // see documentation below for options
})
const builder = new EmailBuilder();
builder
.setSource('PDFTron <[email protected]>')
.setTo('[email protected]')
.setSubject('PDFTron - Contact Sales Form Confirmation')
.setHeader({ text: "PDFTron Systems" })
.addText({ text: "Thank you for filling out our form! We will get back to you soon" })
.addButtons({
buttons: [
{
text: "See more info",
to: "https://pdftron.com/webviewer"
},
{
text: "Documentation",
to: "https://pdftron.com/documentation"
}
]
})
.send(); // send the email!API
Any options that include a style should come in the form of a style object, similar to React
factory(defaults: Object) => EmailBuilderClass
Defaults object can contain:
appNameThe name of your application. The text in the header is defaulted to thisendpointThe endpoint of your AWS SES endpoint. Requests will be sent here. Required.defaultEmailSourceThe default source of all emailsdefaultHeaderImageA link to an image that will be included in all headersdefaultHeaderTextStyleThe default style for the text in the headerdefaultHeaderStyleThe default style for the header divdefaultSubHeaderStyleThe default style for the subheader dicdefaultSubHeaderTextStyleThe default style for the subheader textdefaultFontThe default font to use for all textdefaultFontColorThe default font color to use for all textdefaultHeaderImageStyleThe default style for the image in the headerdefaultBodyStyleThe default style for the overall wrapper divdefaultContentStyleThe default style for the div wrapping the main email content (not including the headers or footer)defaultLabelStyleThe default style for info labelsdefaultValueStyleThe default style for info label valuesdefaultLinkStyleThe default style for any linksdefaultFooterStyleThe default style for the footer divdefaultFooterTextThe default text to be displayed in the footerdefaultFooterTextStyleThe default style for the text in the footerdefaultButtonStyleThe default style for all buttons
Returns a reference to a EmailBuilder class
new EmailBuilderClass() => builder
Methods
Any methods that accept styles will override the defaults set in the factory.
builder.setDefaultFont(font: string) => thisSets the default font for this specific emailbuilder.setDefaultFontColor(color: string) => thisSets the default font color for this specific emailbuilder.setContentStyle(style: StyleObject) => thisSets the style for the content of this emailbuilder.setBodyStyle(style) => thisSets the style for the entire wrapper divbuilder.setSource(source: string) => thisSets the source/sender of this emailbuilder.setTo(to: string|string[]) => thisSets who the email is sent tobuilder.setSubject(subject: string) => thisSets the subject of the emailbuilder.setTextStyle(tags: string|string[], style: StyleObject) => thisSets the style for specific text tags.
Example:
// sets all the p's and h5's to red
builder.setTextStyle(['p', 'h5'], { color: 'red' })builder.setHeader(options: Object) => thisSets the header of the emailoptions.textThe text to be in the headeroptions.imageSourceThe source of the image for the headeroptions.styleStyle for the header divoptions.imgStyleStyle for the image in the headeroptions.textStyleStyle for the text in the header
builder.setSubheader(options: Object) => thisSets the contents and style of the subheaderoptions.textThe text to appear in the subheaderoptions.styleThe style to apply to the subheader divoptions.textStyleThe style to apply to the text in the subheader
builder.addText(options: Object) => thisAdds text to the body of the emailoptions.textThe text to addoptions.tagThe type of text. For example, 'p' or 'h3'options.textStyleThe style of the text
builder.addCustom(html: string) => thisAdds a custom HTML string to the bodybuilder.addLineBreak() => thisAdds a line break to the bodybuilder.addInfoItem(options: Object) => thisAdds a nicely diaplayed key value pair to the email bodyoptions.labelThe label of the dataoptions.valueThe value of the dataoptions.labelTagThe tag to use for the labeloptions.valueTagThe tag to use for the valueoptions.labelStyleStyle for the labeloptions.valueStyleStyle for the value
builder.addButtons(options: Object) => thisAdds a row of buttons to the email bodyoptions.buttonsAn array of buttons to add. Each button can have atext,to,andstyleproperty.options.wrapperStyleStyle to apply to the whole wrapper div
builder.addJSON(options: Object) => thisRenders a bunch of info label corrosponding to a JSON object. Loops over theaddInfoItemAPI.options.dataA non-nested object containing key value pairs to render.options.labelTagThe tag to use for the labelsoptions.valueTagThe tag to use for the valuesoptions.labelStyleStyle for the labelsoptions.valueStyleStyle for the values
builder.setFooter(options: Object) => thisSets the footer of the emailoptions.textThe text to render in the footeroptions.styleThe style to apply to the footer divoptions.textStyleStyle to apply to the footer text
builder.getHTML() => stringReturns the HTML contents of the email. Used mostly for debugging.builder.get() => ObjectReturns the entire SES data structure of the email. Used mostly for debugging.builder.send() => Promise<Response>Sends the email
Statics
EmailBuilderClass.link(options: Object) => string Creates an inline link element that can inserted into other text
options.textThe text of the linkoptions.toWhere the link points tooptions.styleStyle for the link
Example
builder.addText({
text: `View documentation ${EmailBuilder.link({
text: 'here',
to: 'http://pdftron.com/documentation',
})}`
})