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?