SendGrid. Отправка email-ов через пакет @sendgrid/mail

Теперь мы можем отправлять мейлы с Node.js кода. Для этого установим пакет @sendgrid/mail.

Для того, чтобы протестировать отправку email'у, пропишем следующий код, заменив <email_отправитель>, <email_адресат> и <API_токен>, на соответственно созданные нами email-отправитель, email-адресат и создан API-токен:

const sgMail = require("@sendgrid/mail");

sgMail.setApiKey(<API_токен>);
const msg = {
  to: <email_адресат>,
  from: <email_отправитель>,
  subject: "Sending with Twilio SendGrid is Fun",
  text: "and easy to do anywhere, even with Node.js",
  html: "<strong>and easy to do anywhere, even with Node.js</strong>",
};

async function main() {
  const [response] = await sgMail.send(msg);

  console.log(response);
}

main();

Last updated