Send Email Using SendGrid

Tony Huang
DeepQ Research Engineering Blog
3 min readJun 1, 2018

What is SendGrid?

SendGrid is a cloud-based email delivery engine, enabling us to send email without holding our own email servers. SendGrid is for anyone who needs to send email.

Pricing

SendGrid Pricing

SendGrid Functions

Activity Feed

With the activity feed, we can easily know the status of the emails we sent, search certain email easliy with search and filter options, export the data into a CSV file.

Statistics

Tracking emails helps us to be a good sender. Statistics includes clicks, opens, the browsers the recipients use, and etc.

Transactional Templates

Creating email templates helps us to reuse the template to send emails to recipient so that we can save a lot of time.

Sender Authentication

We can use the following simple settings to have better deliverability.

Domain Authentication and IP Whitelabel show email providers that SendGrid has our permission to send emails on our behalf.

Link Branding allows all of the click-tracked links and opens tracked images in emails to be from our domain instead of from sendgrid.net.

For more information, please refer to SendGrid.

Integrate with SendGrid in Node.js

SendGrid provides client libraries in many languages. This is the preferred way to integrate with SendGrid. I will take Node.js for example.

With SendGrid, to send an Email becomes very easy.

import sgMail from ‘@sendgrid/mail’;const tony = process.env.htc;
const frank = process.env.frank;
const candy = process.env.candy;
const ej = process.env.ej;
const hiro = process.env.hiro;
const rex = process.env.rex;
const yingray = process.env.yingray;
const url = process.env.url;
const id = process.env.templateId;
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: frank,
from: tony,
subject: 'Greeting From Tony',
text: 'HIIIII~~~~~',
};
sgMail.send(msg);

If wanting to send a single email to multiple recipients, just modify to field to an array containing the recipients.

const msg = { 
to: [frank, candy, ej, hiro, rex, yingray],
from: tony,
subject: 'Greeting From Tony',
text: 'HIIIII~~~~~',
};

But what if we want the recipients don’t see each others email addresses? We can use sendMultiple instead of send.

const msg = { 
to: [frank, candy, ej, hiro, rex, yingray],
from: tony,
subject: 'Greeting From Tony',
text: 'HIIIII~~~~~',
};
sgMail.sendMultiple(msg);

We can also send multiple emails to multiple recipients by doing like this.

const msgs = [
{
to: [frank, candy, ej],
from: tony,
subject: 'Greeting From Tony 1',
text: 'HIIIII~~~~~1',
},
{
to: [hiro, rex, yingray],
from: tony,
subject: 'Greeting From Tony 2',
text: 'HIIIII~~~~~2',
}
];

We can specify the cc, bcc and replyTo fields for more control over whom we send the email to and where the recipients will reply to.

const msg = { 
to: frank,
from: tony,
cc: candy,
bcc: [ej, hiro],
replyTo: tony,
subject: 'Greeting From Tony',
text: 'HIIIII~~~~~',
};

send and sendMultiple return a promise object so that we can handle success and catch errors.

As mentioned above, we can use transactional template! It is also very simple that we just add templateId field. Here is my template body

<html>
<head>
<title></title>
</head>
<body>
<div><%body%>{{text}}</div>
<a style="background-color:#333333;border:1px solid #333333;border-color:#333333;border-radius:6px;border-width:1px;color:#ffffff;display:inline-block;font-family:arial,helvetica,sans-serif;font-size:16px;font-weight:normal;letter-spacing:0px;line-height:16px;padding:12px 18px 12px 18px;text-align:center;text-decoration:none" href="{{buttonURL}}" target="_blank">Verify</a>
</body>
</html>

As you can see in the above code snippet, there are {{text}} and {{buttonURL}}. We can substitute them when calling send.

const msg = { 
to: frank,
from: tony,
cc: candy,
bcc: [ej, hiro],
replyTo: tony,
subject: 'Greeting From Tony',
templateId: id,
substitutions:{
text: `Random Number: ${Math.random()}`,
buttonURL: url
},
substitutionWrappers = ['{{', '}}'],
};

For more information, examples, and use cases, please refer to SendGrid and sendgrid-nodejs on Github.

--

--