send an email in typescript

To send an email in TypeScript using Node.js, we can use a package called Nodemailer. Here's an example code snippet that demonstrates how to send an email:

index.ts
import nodemailer from "nodemailer";

// configure Nodemailer
const transporter = nodemailer.createTransport({
  host: "smtp.example.com",
  port: 587,
  secure: false, // true for 465, false for other ports
  auth: {
    user: "username",
    pass: "password"
  }
});

// define email options
const mailOptions = {
  from: "sender@example.com",
  to: "recipient@example.com",
  subject: "Test email",
  text: "This is a test email sent from Node.js using Nodemailer."
};

// send email
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.error(error);
  } else {
    console.log(`Email sent: ${info.response}`);
  }
});
649 chars
30 lines

This code creates a transporter object using the SMTP details for the email provider, defines an object with the email options, and sends the email using the sendMail method of the transporter. In the callback function, we can log any errors or a confirmation message that the email was sent.

gistlibby LogSnag