I am developing a function to send email to customers.
exports.sendRestockingNotification = functions.https.onCall((data) => {
try {
console.log("Sending confirmation email to the customer...");
sendEmailRestocking(data.numero, data.nombre, data.emails);
} catch (error) {
console.error("Unexpected error: ", error);
}
});
function sendEmailRestocking (numero, nombre, emails) {
console.log("Sending Email...");
return transport.sendMail({
from: "XXXXX <xxxxxxxxxxxxx@gmail.com>",
to: emails,
subject: "El artículo " + nombre + " (" + numero + ") vuelve a estar disponible.",
html:
`
<div class="container rounded" style="padding: 10px 30px;">
<div class="container rounded" style="background-color: #0bbe83;">
<h1 style="color: white; padding: 5px;">TejidosPulido</h1>
</div>
<div class="row" style="padding: 10px 50px;">
<h5 style="color: #0bbe83;">Hola, </h5>
<p>
El artículo ${nombre} (${numero}) vuelve a estar disponible. ¡Date prisa antes de que se termine!
</p>
</div>
<br>
<p style="padding: 10px 30px;">
Un saludo,
<br>
Tu equipo TejidosPulido
</p>
</div>
`,
});
}
But I am always getting the same in the Firebase Functions console and any mail is sent and the debug message console.log("Sending a confirmation email to the customer...");
is not showed neither:
sendRestockingNotification -> Function execution started
sendRestockingNotification -> Function execution took 14 ms, finished with status code: 204
Have a look at the doc for Callable Cloud Functions. You need to return data that can be JSON encoded.
So you need to wait for the asynchronous sendEmailRestocking()
function to terminate before sending a response. For example with the following adaptations:
exports.sendRestockingNotification = functions.https.onCall(async (data) => { // See async
try {
console.log("Sending confirmation email to the customer...");
await sendEmailRestocking(data.numero, data.nombre, data.emails);
return {result: "mail send"}; // For example
} catch (error) {
// See https://firebase.google.com/docs/functions/callable#handle_errors
console.error("Unexpected error: ", error);
}
});