如何通过电子邮件向Firebase Functions发送某些数据?假设有一个授权用户,我们将其一些数据(例如出生日期)放入了实时数据库。如何使用Firebase Functions在HTTP请求后如何将此字段发送到电子邮件?
答案 0 :(得分:0)
要通过JS发送电子邮件,您需要连接nodemailer
const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
并创建运输方式:
let mailTransportAuth = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: 'email', /
pass: 'password'
}
});
然后创建一个函数,该函数将监视实时数据库中的更改(onUpdate)并发送字母:
exports.sendCodeOnEmail = functions.database.ref('/users/{userssId}/').onUpdate((snapshot, context) => {
var newCode = generateCode()
var key = Object.keys(snapshot.after.val())[0];
const c = snapshot.after.child(key).toJSON().toString();
const email = snapshot.after.child("email").exportVal();
const code = snapshot.after.child("code").exportVal();
snapshot.after.ref.update({'code': newCode});
const mailOptions = {
from: '"From me" <noreply@firebase.com>',
to: email,
text: "text"
};
mailOptions.subject = "Subject message";
console.log(snapshot);
try {
mailTransportAuth.sendMail(mailOptions);
console.log(`Email sent to:`, email);
} catch (error) {
console.error('There was an error while sending the email:', error);
}
console.log("Send code on email: " + email, " , code: " + code);
return null;
});