我试图发送电子邮件使用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)
我该怎么补救呢?
1条答案
按热度按时间iqxoj9l91#
执行以下操作无法正常工作:
你混合了Callable Cloud Functions和HTTPS ones(它们以Node.js表达式
req
和res
作为参数)。req
和res
将没有任何值,因此dest
将为null。我建议你研究一下Callable Cloud Functions文档。
下面的代码应该可以做到这一点(未测试):
请注意,我们使用
const dest = data.email
,因为在用于调用CF的createMessage
对象中没有dest
元素。您也可以研究以下样本:https://github.com/firebase/functions-samples/blob/master/quickstarts/email-users/functions/index.js