const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'xxxxxx@gmail.com',
pass: 'xxxxxxxx'
} ,
tls: {
rejectUnauthorized: false
},
});
exports.sendMail=functions.https.onCall((req,res)=>{
cors(req,res,()=>{
const email=JSON.parse(req.email)
const mailOptions = {
from: 'xxxxxxxxx@gmail.com',
to: email,
subject: 'Invitation to register your profile in xxxxxxx solutions',
text: `http://xxxxxx/xxxxxxxx`
};
return transporter.sendMail(mailOptions, function(error, info){
if (error) {
return res.send(error);
}
return res.send("Email sent")
});
})
})
答案 0 :(得分:1)
最可能是因为您:
res.send("Email sent")
返回。因此,您应该更改为HTTP Cloud Function,如下所示:
exports.sendMail=functions.https.onCall((req,res)=>{
cors(req,res,()=>{
const email=JSON.parse(req.email)
const mailOptions = {
from: 'xxxxxxxxx@gmail.com',
to: email,
subject: 'Invitation to register your profile in xxxxxxx solutions',
text: `http://xxxxxx/xxxxxxxx`
};
transporter.sendMail(mailOptions)
.then(() => {
res.send("Email sent");
})
.catch(error => {
console.log(error);
res.status(500).send(error);
});
})
})
或如下修改您的Callable Cloud Function:
exports.addMessage = functions.https.onCall((data, context) => {
const email = data.email;
const mailOptions = {
from: 'xxxxxxxxx@gmail.com',
to: email,
subject: 'Invitation to register your profile in xxxxxxx solutions',
text: `http://xxxxxx/xxxxxxxx`
};
return transporter.sendMail(mailOptions)
.then(() => {
return {result: "Email sent"};
})
.catch(error => {
console.log(error);
throw new functions.https.HttpsError('interna', error.message);
});
});
另外,请参见here,如何从客户端调用Callable CF。