Created
January 10, 2024 00:26
-
-
Save tedyyu/e2540ae38e6e73c870b112fe97a4fa3b to your computer and use it in GitHub Desktop.
sendgrid/mail typescript example
This file contains hidden or 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
import sgMail from '@sendgrid/mail' | |
async function sendMail(email: string, content: string, subject: string): Promise<void> { | |
// using Twilio SendGrid's v3 Node.js Library | |
// https://github.com/sendgrid/sendgrid-nodejs | |
sgMail.setApiKey('<sendGridApiKey>'); | |
const msg = { | |
to: email, // Change to your recipient | |
from: '[email protected]', // Change to your verified sender | |
subject: subject, | |
//text: content, | |
html: content, | |
}; | |
sgMail | |
.send(msg) | |
.then(() => { | |
//console.log(`Email ${content} sent to ${email} successfully.`); | |
}) | |
.catch((error) => { | |
console.error(`Email ${content} failed to be sent to ${email}. Error: ${error}`); | |
}); | |
} | |
export default sendMail; | |
//test | |
//await sendGrid('验证码: <strong>123456</strong>', '<[email protected]>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment