Risal Fajar Amiyardi
Risal Fajar Amiyardi

Reputation: 1021

Nodemailer queryA EREFUSED localhost when using Firebase Cloud Functions

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' }

Upvotes: 3

Views: 9262

Answers (4)

mfruizs
mfruizs

Reputation: 770

If you are using a nodemailer old-version, maybe you need to update to a new one.

{
    "code": "EDNS",
    "syscall": "queryA",
    "hostname": "smtp.gmail.com",
    "command": "CONN"
}

Use dependency manager that you need ( npm, yarn, etc )

npm update nodemailer

Here, you can read more info about this error nodemailer issue

Upvotes: 0

Mocktar Issa
Mocktar Issa

Reputation: 191

If the accepted answer doesn't work for you, you can also try to check your DNS configuration and ensure a nslookup on the hostname works well.

Run:

nslookup 'hostname'

If this results in error go to your network parameters and change your current DNS to 8.8.8.8 and try again to if the error persists. .Or run directly:

nslookup 'hostname' 8.8.8.8

Additionally if the error happens only in your Cloud/ Docker container or VM ensure the nameserver configuration is correct.

Note: I had this error because work wifi's primary DNS wasn't resolving 'smtp.office3.com' but the secondary did and it seems nodemailer has no process in place to resolve the hostname using secondary DNS or entry in the OS host. You can read more about how to fix is error in the Nodemailer faq here.

Upvotes: 0

ziye
ziye

Reputation: 11

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.

Upvotes: 1

Risal Fajar Amiyardi
Risal Fajar Amiyardi

Reputation: 1021

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'
    }
});

Upvotes: 6

Related Questions