3
votes

I'm trying to send email to admin account whenever there's a new Order created in Firebase Database. I'm using Cloud Functions and Nodemailer for this, but I got below error:

Error: queryA EREFUSED localhost at QueryReqWrap.onresolve [as oncomplete] (dns.js:213:19)

And this is the code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
admin.initializeApp();

/**
* Here we're using Gmail to send 
*/
let transporter = nodemailer.createTransport({
    service: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: '[email protected]',
        pass: 'mypass'
    }
});

exports.sendMail = functions.database.ref('/Order/{orderId}').onCreate((snapshot, context) =>{
    const dest = '[email protected]';

    const mailOptions = {
        from: 'Risal Fajar <[email protected]>',
        to: dest,
        subject: 'I\'M A PICKLE!!!',
        html: `<p style="font-size: 16px;">Pickle Riiiiiiiiiiiiiiiick!!</p>
            <br />
            <img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
        `
    };

    // returning result
    return transporter.sendMail(mailOptions).then(() => {
        console.log('Mail sent to ', dest);
    });
});

I don't know why the error showed localhost even though I've entered smtp.gmail.com

EDIT: I've added transport.verify() and this is the result

{ Error: queryA EREFUSED localhost at QueryReqWrap.onresolve [as oncomplete] (dns.js:213:19) errno: 'EREFUSED', code: 'EDNS', syscall: 'queryA', hostname: 'localhost', command: 'CONN' }

2

2 Answers

3
votes

FIXED IT

I fixed it by changing the transporter configuration (change service name) to:

let transporter = nodemailer.createTransport({
    service: 'gmail',
    port: 465,
    secure: true,
    auth: {
        user: '[email protected]',
        pass: 'mypass'
    }
});
1
votes

You can see the document: https://nodemailer.com/usage/using-gmail/

The gmail has another problem, but about this problem, same as I, I got this Error:

Error: { Error: queryA EREFUSED smtp.163.com at QueryReqWrap.onresolve [as oncomplete] (dns.js:199:19)

But when I try to run the same code at my mobile using termux, it works, maybe the free smtp services was so bad.