Skip to content

Instantly share code, notes, and snippets.

@alvinslee
Last active January 28, 2022 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvinslee/4a927d71d765d06ec049b11cbe4ed206 to your computer and use it in GitHub Desktop.
Save alvinslee/4a927d71d765d06ec049b11cbe4ed206 to your computer and use it in GitHub Desktop.
Email Sending Example Using SendGrid Node.js SDK
// Make sure you have added @sendgrid/mail to your Node.js project.
//
// Example Usage:
// API_KEY=SG.123456789 CUSTOM_URL=https://www.example.com?campaign_id=my-campaign RECIPIENT_EMAIL=johndoe@gmailx.com node index.js
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.API_KEY)
const text = `Please click this link for my email campaign: ${process.env.CUSTOM_URL}`
const html = `Please click <a href="${process.env.CUSTOM_URL}"}this link</a> for my email campaign.`
const msg = {
to: process.env.RECIPIENT_EMAIL,
from: 'example@example.com', // CHANGE THIS TO USE YOUR SENDGRID VERIFIED SENDER
subject: 'SendGrid Test Email with campaign link',
text,
html
}
sgMail
.send(msg)
.then((response) => {
console.log(response[0].statusCode)
console.log(response[0].headers)
})
.catch((error) => {
console.error(error)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment