我想要接收变量为"mail"message"name"的电子邮件。。。。比如"你好{name},你好吗?你的电子邮件是{mail},这是你的{message}来自联系表格的副本"。。。。。问题是我只是收到了{邮件},而不是{消息}{名称}
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const nodemailer = require('nodemailer');
const gmailEmail = functions.config().gmail.login;
const gmailPassword = functions.config().gmail.pass;
admin.initializeApp();
var goMail = function (message) {
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword
}
});
const mailOptions = {
from: gmailEmail,
to: 'la***@gmail.com',
subject: 'Hello ✔',
text: '!' + message,
html: '!' + message
};
const getDeliveryStatus = function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
};
transporter.sendMail(mailOptions, getDeliveryStatus);
};
exports.onDataAdded = functions.database.ref('/emails/{sessionId}').onCreate(function (snap, context) {
const createdData = snap.val();
var text = createdData.mail;
goMail(text);
});
这就是我的firebase实时数据库的外观
emails
-**sesionID*
mail: "randommail@gmail.com"
message:"test"
name:"testname"
我试着从代码中添加/编辑这个,但这没有帮助:-/
var goMail = function (message, secondmessage)
const mailOptions = {
from: gmailEmail,
to: 'la***@gmail.com',
subject: 'Hello ✔',
text: '!' + message + secondmessage,
html: '!' + message + secondmessage
};
var text = createdData.mail;
var secondtext = createdData.name
goMail(text, secondtext);
谢谢大家的回答,祝你今天愉快!
您只收到{{mail}},因为该值已在您的云函数上设置为常数值,而且在代码调用goMail函数时,您将发送2个参数,而不是预期的1个参数。尝试将您的goMail功能更改为(例如(:
var goMail = function (name, message) {
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword
}
});
const mailOptions = {
from: gmailEmail,
to: 'la***@gmail.com',
subject: 'Hello ✔',
text: '!' + name + message,
html: '!' + name + message
};
const getDeliveryStatus = function (error, info) {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
};
transporter.sendMail(mailOptions, getDeliveryStatus);
};
让我知道它是否有效。