This repository contains materials to accompany the Twilio SendGrid session at SIGNAL Superclass 2021.
Among the communication channels available today, email is non-negotiable. Email is reliable, works globally, and offers flexibility that can't be met by other communications avenues. That may be why 83% of customers prefer to receive business communications via email.
In this SIGNAL Superclass session, you'll learn how to implement the Twilio SendGrid Mail Send API to programmatically deliver email at scale. You'll also see how the Twilio SendGrid dynamic templating system makes it possible to personalize your messages for each customer, and you can expect some deliverability pro-tips along the way.
The following documentation and resources will help you build upon what you learn in this session.
- How to send an email with Dynamic Transactional Templates
- The Design and Code Editor
- Using Handlebars
This outline provides abbreviated steps and code snippets to help you follow along with the workshop. You can use this outline and the app in the 5K9 directory to replicate the workshop on your own.
- Sign up for a SendGrid account.
- Create and store an API key.
- Domain authentication.
- Navigate to the 5K9 directory of this repository.
cd 5K9
- Install the project dependencies.
npm install
- Start the application in developer mode.
npm run dev
- File:
controllers/sendEmail.js
module.exports = function sendEmail(toAddress, templateData) {
const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const email = {
to: toAddress,
from: "[email protected]", // Change to your verified sending address
templateId: "your-template-id", // Change to your template ID
dynamicTemplateData: templateData,
};
sgMail
.send(email)
.then(() => {
console.log("Email sent");
})
.catch((error) => {
console.error(error);
});
};
- File:
routes/signup.js
const express = require("express");
const router = express.Router();
const sendEmail = require("../controllers/sendEmail");
router.get("/", function (req, res, next) {
res.render("signup");
});
router.post("/confirmation", function (req, res, next) {
sendEmail(req.body.email, req.body);
res.render("signup-confirmation");
});
module.exports = router;
- template insert snippet
- template resources test data
{
"first_name": "Miles",
"dog_name": "Dash",
"resources": {
"loose_leash_guide": "Guide to Loose Leash Walking",
"night_checklist": "Night Running Checklist",
"harness_picks": "2021 Harness Picks"
}
}
- template resources list snippet