>我试图使用Firebase云功能从表单提交的数据发送电子邮件,但它根本没有发送。
我的函数代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
const cors = require('cors')({
origin: '*'
})
admin.initializeApp();
/**
* Here we're using Gmail to send
*/
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'password'
}
});
exports.sendEmail = functions.https.onCall((contact, context) => {
cors(req, res, () => {
const dest = req.body.dest
console.log(dest)
const mailOptions = {
from: `Contact AET ${contact.email}`,
to: dest,
subject: "AET Website Message ", // Subject line
html: `
<div
style="padding: 10px; font-family:Gadaj; border: 2px solid #eee; border-radius: 10px ">
<p style="font-size: 15px">You have a new message request</p>
<h2>Contact Details</h2>
<ul style="list-style-type: none">
<li>Name: ${contact.name}</li>
<li>Email: ${contact.email}</li>
</ul>
<h2>Message</h2>
<p style="font-size: 16px;">${contact.message}</p>
<img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
</div>
`
};
return transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return console.log(err)
}
return console.log(`Message sent to ${info.messageId}`)
})
})
})
然后我像这样在我的前端连接
它sendMessage() {
if (this.$refs.formEmail.validate()) {
this.loading = true;
const createMessage = {
name: this.name,
email: this.email,
message: this.message
};
const sendEmail = functions.httpsCallable("sendEmail");
sendEmail(createMessage)
.then(res => {
// eslint-disable-next-line no-console
console.log(res.data);
})
.catch(err => {
// eslint-disable-next-line no-console
console.log(err);
});
}
}
我把它作为错误返回:
Error: Error: INTERNAL
at new HttpsErrorImpl (index.cjs.js?001a:58)
at _errorForResponse (index.cjs.js?001a:153)
at Service.eval (index.cjs.js?001a:538)
at step (tslib.es6.js?9ab4:99)
at Object.eval [as next] (tslib.es6.js?9ab4:80)
at fulfilled (tslib.es6.js?9ab4:70)
我该如何解决这个问题?
执行以下操作不起作用:
exports.sendEmail = functions.https.onCall((contact, context) => {
cors(req, res, () => {...});
});
您正在混合可调用的云函数和HTTPS函数(它们采用节点.js表示req
和res
作为参数)。req
和res
将没有任何值,因此dest
将为 null。
我建议您学习可调用的云函数文档。
以下应该可以解决问题(未测试):
exports.sendEmail = functions.https.onCall(async (data, context) => {
try {
const dest = data.email
console.log(dest)
const mailOptions = {
from: `Contact AET ${contact.email}`,
to: dest,
subject: "AET Website Message ", // Subject line
html: `
<div
style="padding: 10px; font-family:Gadaj; border: 2px solid #eee; border-radius: 10px ">
<p style="font-size: 15px">You have a new message request</p>
<h2>Contact Details</h2>
<ul style="list-style-type: none">
<li>Name: ${contact.name}</li>
<li>Email: ${contact.email}</li>
</ul>
<h2>Message</h2>
<p style="font-size: 16px;">${contact.message}</p>
<img src="https://images.prod.meredith.com/product/fc8754735c8a9b4aebb786278e7265a5/1538025388228/l/rick-and-morty-pickle-rick-sticker" />
</div>
`
};
await transporter.sendMail(mailOptions);
return null;
} catch (error) {
// See the doc: https://firebase.google.com/docs/functions/callable#handle_errors
}
})
请注意,我们确实const dest = data.email
,因为用于调用 CF 的createMessage
对象中没有dest
元素。
您也可以研究此示例:https://github.com/firebase/functions-samples/blob/master/quickstarts/email-users/functions/index.js