1

I am trying to send a simple mail through my website to my own other account. But it does not work. I am using node mailer and have followed the documentation precisely.

I have tried checking for any kind of syntax error or external factors.

//Send Mail
router.post('/volunteer', ensureAuthenticated, async(req, res) => {

    // const { name, email, city } = req.body;
    //Create the transporter
    require('dotenv').config();
    const nodemailer = require('nodemailer');

    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: process.env.EMAIL,
            pass: process.env.PASSWORD
        }
    });

    //set up mailOptions
    let mailOptions = {
        from: 'readsocially001@gmail.com',
        to: 'vaibhav.ag.001@gmail.com',
        subject: 'Volunteer',
        text: 'HEY'
    };

    transporter.sendMail(mailOptions, function (err, data) {
        if (err) {
            console.log("some error");
        } else {
            console.log("SENT");
        }

    });
    res.redirect('/volunteer');
});

It just goes into the if(err) statement and prints some error. The actual error is:

Error: Missing credentials for "PLAIN"
    at SMTPConnection._formatError (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:781:19)
    at SMTPConnection.login (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:452:38)
    at connection.connect (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-transport/index.js:271:32)
    at SMTPConnection.once (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:209:17)
    at Object.onceWrapper (events.js:286:20)
    at SMTPConnection.emit (events.js:198:13)
    at SMTPConnection._actionEHLO (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:1309:14)
    at SMTPConnection._processResponse (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:940:20)
    at SMTPConnection._onData (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:746:14)
    at TLSSocket.SMTPConnection._onSocketData (/Users/vaibhav2001/Documents/test/ReadSocially/node_modules/nodemailer/lib/smtp-connection/index.js:189:46)
    at TLSSocket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10)
    at TLSWrap.onStreamRead [as onread] (internal/stream_base_commons.js:94:17) code: 'EAUTH', command: 'API' }

2 Answers2

0

According to the official doc, you can't use plain credentials: https://nodemailer.com/usage/using-gmail/ To use nodemailer with Gmail you need to create an App and use the oauth like this:

    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        type: 'OAuth2',
        user: 'user@example.com',
        accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
    }
});

More examples: https://nodemailer.com/smtp/oauth2/#examples

Hope it helps

Jose Mato
  • 2,709
  • 1
  • 17
  • 18
0

You can try this.

var nodemailer = require('nodemailer');
var xoauth2 = require('xoauth2');

// listen for token updates (if refreshToken is set)
// you probably want to store these to a db
generator.on('token', function(token){
    console.log('New token for %s: %s', token.user, token.accessToken);
});

// login
var transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        xoauth2: xoauth2.createXOAuth2Generator({
            user: '{username}',
            clientId: '{Client ID}',
            clientSecret: '{Client Secret}',
            refreshToken: '{refresh-token}',
            accessToken: '{cached access token}'
        })
    }
});
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43