SENDGRID_API_KEY
. You won’t be able to get that same key from the SendGrid dashboard again for security reasons.npm init
to start a new Node.js project. Install the Sendgrid helper library with npm install @sendgrid/mail
. Create a file called index.js
and open it in your editor of choice.to
field, and then call sendMultiple
with a single message object.index.js
and replace the emails in the to
array with your email addresses.const sgMail = require('@sendgrid/mail'); sgMail.setApiKey(process.env.SENDGRID_API_KEY); const msg = { to: ['[email protected]', '[email protected]'], // replace these with your email addresses from: 'Sadie Miller <[email protected]>', subject: '🍩 Donuts, at the big donut 🍩', text: 'Fresh donuts are out of the oven. Get them while they’re hot!', html: '<p>Fresh donuts are out of the oven. Get them while they’re <em>hot!</em></p>', }; sgMail.sendMultiple(msg).then(() => { console.log('emails sent successfully!'); }).catch(error => { console.log(error); });
node index.js
from the terminal. You should see console output letting you know the script ran successfully, and watch the emails arrive in your inbox shortly.sgMail.sendMultiple
with sgMail.send
. That said, be careful and don’t expose your customers’ email addresses unless you have a good use case for doing so.index.js
with the following:const sgMail = require('@sendgrid/mail'); sgMail.setApiKey(process.env.SENDGRID_API_KEY); const msg = { personalizations: [ { to: '[email protected]', // replace this with your email address subject: 'Did somebody say BACON DONUTS?? 🥓🥓🥓', }, { to: '[email protected]', // replace this with your email address } ], from: 'Sadie Miller <[email protected]>', subject: '🍩 Donuts, at the big donut 🍩', text: 'Fresh donuts are out of the oven. Get them while they’re hot!', html: '<p>Fresh donuts are out of the oven. Get them while they’re <em>hot!</em></p>', }; sgMail.send(msg).then(() => { console.log('emails sent successfully!'); }).catch(error => { console.log(error); });
node index.js
on the command line to try it out.subject
- the email subject line.headers
- any headers you want to send with the email.substitutions
- key/value pairs representing strings that you would like to substitute into an email subject line or body.custom_args
- any custom arguments you would like to include in your email, that will override substitutions.send_at
- specifies a particular time you’d like the email to be sent, in Unix timestamp format..send
method also accepts an array of email message objects. Unlike using an array of addresses in the to
field, the code below will not cc recipients.const sgMail = require('@sendgrid/mail'); sgMail.setApiKey(process.env.SENDGRID_API_KEY); const messages = [ { to: '[email protected]', // replace this with your email address from: 'Sadie Miller <[email protected]>', subject: 'We miss you 😭', text: 'Get 10% off with coupon code NOMNOMNOM', html: '<p>Get 10% off with coupon code <b>NOMNOMNOM</b></p>', }, { to: '[email protected]', // replace this with your email address from: 'Lars Barriga <[email protected]>', subject: 'NEW! Ube rolls 😻', text: 'In addition to donuts, we are now selling ube rolls.', html: '<p>In addition to donuts, we are now selling ube rolls.</p>', }, ]; sgMail.send(messages).then(() => { console.log('emails sent successfully!'); }).catch(error => { console.log(error); });