sendgrid-templated
v1.0.0
Published
Sendgrid extension for easy use with html templates and data injection.
Downloads
2
Maintainers
Readme
sendgrid-templated
sendgrid-templated
extends @sendgrid/client
and @sendgrid/helpers
to allow for easy use with html templates and data injection.
Installation
Use npm i sendgrid-templated
to install the package via NPM.
Useage
import Email from "sendgrid-templated";
const email = new Email({
apiKey: "SENDGRID_API_KEY",
from: { email: "[email protected]", name: "Jane Doe" },
to: { email: "[email protected]", name: "John Doe" },
subject: "Welcome",
template: {
filePath: path.join(__dirname, "templates/welcome.html"),
data: { firstName: "John", message: "Thanks for joining!" }
}
});
email
.send()
.then(() => {
// Success!
})
.catch(err => {
// Handle err
});
Assuming templates/welcome.html
looks something like:
<div>
<h1>Welcome, {{firstName}}!</h1>
<div>{{message}}</div>
</div>
Config Options
The config object supplied to the contructor has the following options:
apiKey
: The API key you receive from sendgrid. Defaults toprocess.env.SENDGRID_API_KEY
from
: Object containing theemail
and [optional]name
for the email'sfrom
info. Defaults toprocess.env.SENDGRID_FROM_EMAIL
andprocess.env.SENDGRID_FROM_NAME
to
: Object containing theemail
and [optional]name
for the email'sto
info.subject
: The subject of the email.template
: [optional] Object containing info about the template to be used. SeeTemplate Config Options
below for more details.
Template Config Options
The template config object has the following options:
filePath
: The path to the html file to be used as the email template.data
: [optional] Object containing key/value pairs of injections to be made into the template.wrappers
: [optional] A tuple of opening and closing char(s) to denote a template variable. (e.g.["%", "%"]
would denote a template var of format%firstName%
)
Contributions
Contributions are welcome!
To continue development on sendgrid-templated
, clone this repo, then run npm i
to install all necessary dependencies.
You can run npm build
to build the project, and npm test
to run tests.