[英]Send data from Realtime Database using Firebase Functions to email
如何通過電子郵件將某些數據發送到Firebase Functions? 假設有一個授權用戶,我們將其一些數據(例如出生日期)放入了實時數據庫。 如何使用Firebase Functions在HTTP請求后如何將此字段發送到電子郵件?
要通過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;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.