Last active
January 28, 2022 21:15
-
-
Save alvinslee/4a927d71d765d06ec049b11cbe4ed206 to your computer and use it in GitHub Desktop.
Email Sending Example Using SendGrid Node.js SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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