Skip to content

Instantly share code, notes, and snippets.

@tedyyu
Created January 10, 2024 00:26
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 tedyyu/e2540ae38e6e73c870b112fe97a4fa3b to your computer and use it in GitHub Desktop.
Save tedyyu/e2540ae38e6e73c870b112fe97a4fa3b to your computer and use it in GitHub Desktop.
sendgrid/mail typescript example
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: 'sender@yourmail.com', // 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>', '<target@mail.com>');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment