send email in javascript

To send an email using JavaScript, you can use the Nodemailer library which is a module for Node.js that simplifies the process of sending emails. Here is a simple example of how to use Nodemailer to send an email using SMTP:

index.tsx
const nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
const transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // true for 465, false for other ports
    auth: {
        user: 'youremail@gmail.com', // generated ethereal user
        pass: 'yourpassword' // generated ethereal password
    }
});

// send mail with defined transport object
let info = await transporter.sendMail({
    from: '"Sender Name" <youremail@gmail.com>', // sender address
    to: 'receiver@example.com', // list of receivers
    subject: 'Hello', // Subject line
    text: 'Hello world?', // plain text body
    html: '<b>Hello world?</b>' // html body
});

console.log('Message sent: %s', info.messageId);
795 chars
24 lines

Note that you will need to replace the email and password with your own Gmail account, and you may need to enable "less secure apps" access in your Google account settings or set up 2-step verification in order for this to work.

Alternatively, you can also use the Gmail API to send emails with JavaScript, which requires setting up credentials and authentication. If you are using a client-side JavaScript application, you can use the Gmail RESTful API with OAuth 2.0 authorization.

gistlibby LogSnag