Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 573
Risal Fajar Amiyardi Avatar asked Jul 18 '26 13:07

Risal Fajar Amiyardi


1 Answers

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'
    }
});
like image 126
Risal Fajar Amiyardi Avatar answered Jul 21 '26 04:07

Risal Fajar Amiyardi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!