0

I need to send email through an HTTP Request to Firebase Cloud Function. I'm using Sparkpost as provider, but when I call the Function with Postman, I receive the following error:

{
    "message": {
        "errno": "EAI_AGAIN",
        "code": "EAI_AGAIN",
        "syscall": "getaddrinfo",
        "hostname": "api.sparkpost.com",
        "host": "api.sparkpost.com",
        "port": "443"
    }
}

If I try to call the same function served in local with firebase serve --only functions command, and then test it with Postman, I don't receive any error.

My code is the following:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const SparkPost = require('sparkpost');

// Initialize App
admin.initializeApp();

exports.sendSurveyRecap = functions.https.onRequest((req, res) => {
  try {
    if (req.method !== 'POST') {
      return res.status(405).json({
        message: `Method ${req.method} is not allowed`,
      });
    }

    const client = new SparkPost('<MY API KEY>');

    return client.transmissions.send({
      content: {
        from: '<MY EMAIL DOMAIN>',
        subject: 'Hello from node-sparkpost',
        html: '<p>Hello world</p>',
      },
      recipients: [
        { address: 'foo@example.com' },
      ],
    }).then(() => res.status(200).json({
      message: 'Message sent successful',
    })).catch(error => res.status(500).json({
      message: error,
    }));
  } catch (error) {
    return res.status(500).json({
      message: error.message,
    });
  }
});

So why I receive that error when I call the Cloud Function with the "deployed" url, instead with the "served" url I don't receive any error?

Mintendo
  • 557
  • 1
  • 8
  • 24
  • Which payment plan is your project on? How frequently did this function execute recently before the error? – Doug Stevenson Feb 16 '19 at 08:46
  • @DougStevenson Is the free version plan, and I call the function only one time – Mintendo Feb 16 '19 at 08:48
  • On the free plan, you can't make outgoing socket requests to destinations that aren't fully controlled by google. I guess this is just another way that the function can fail in that case. – Doug Stevenson Feb 16 '19 at 08:50
  • @DougStevenson thanks a lot :). This explain why with Gmail I don't receive any error. – Mintendo Feb 16 '19 at 09:04

0 Answers0