Skip to content

Instantly share code, notes, and snippets.

@adarsh-chakraborty
Created November 22, 2021 09:26
Show Gist options
  • Save adarsh-chakraborty/eb7ae135cc6db47af171b7a65927d9e4 to your computer and use it in GitHub Desktop.
Save adarsh-chakraborty/eb7ae135cc6db47af171b7a65927d9e4 to your computer and use it in GitHub Desktop.
Send e-mails through node js

Import the package and set up transporter.

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: process.env.MAILUSER,
        pass: process.env.MAILPASS
    }
});

Now, in your code. Set up options to send the mail to, and send the mail through transporter.

 let mailOptions = {
        from: `Donald Duck <Donaldduck@disney.com>`,
        to: 'mickeymouse@example.com',
        replyTo: `donaldduck@disney.com`,
        subject: `Quake Quake`,
        text: `Hello Micky, How you doing?`
    };

    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            console.log(error);
        } else {
            console.log('Email sent: ' + info.response);
        }
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment