One of my Firebase functions is named sendMail
and it is responsible for sending emails. I am facing an issue while trying to pass the email address of the recipient along with another parameter to this function. Within my Vue application, I invoke the function in the following manner:
sendEmail(){
console.log(this.email)
let sendMail = firebase.functions().httpsCallable('sendMail');
sendMail(
{
"email": this.email,
"superu": this.superu
}
).then(
result => {
console.log(result)
}
)
}
The contents of my index.js
function script are as follows:
const functions = require('firebase-functions');
const admin = require("firebase-admin")
const nodemailer = require('nodemailer');
admin.initializeApp()
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '*****@****.com',
pass: '***********'
}
});
exports.sendMail = functions.https.onRequest((req, res) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
console.log(req.body['data']);
const mailOptions = {
from: `•••••••••@gmail.com`,
to: req.body['data'].email,
subject: 'contact form message',
html: `<h2 style="color: teal">Order Confirmation</h2>
<a href="https://track-acquintances.firebaseapp.com/signup/${req.body.superu}">
<b> Register </b>"<br>
</a>`
};
return transporter.sendMail(mailOptions, (error, data) => {
if (error) {
return res.status(200).json({data: error.message});
}
data = JSON.stringify(data)
return res.status(200).json({data: data});
});
});
I have been unable to access the passed email data which is causing the function to fail. Upon logging req.body['data']
in the functions logs, it displays
{ email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f48c8c8cb48c8cda8c8c8cda8c">[email protected]</a>', superu: true }
. Despite attempting both req.body['data'].email
and req.body['data']['email']
, neither seem to work. Additionally, my browser's console outputs {data: "No recipients defined"}
. I would greatly appreciate any assistance. Thank you.