简体   繁体   English

使用Firebase功能将实时数据库中的数据发送到电子邮件

[英]Send data from Realtime Database using Firebase Functions to email

How to send certain data via email to Firebase Functions? 如何通过电子邮件将某些数据发送到Firebase Functions? Let's say there is an authorized user and some of his data (eg date of birth) we put in the Realtime Database. 假设有一个授权用户,我们将其一些数据(例如出生日期)放入了实时数据库。 How, using Firebase Functions, do you send this field to email after HTTP request? 如何使用Firebase Functions在HTTP请求后如何将此字段发送到电子邮件?

To send an email via JS, you need to connect nodemailer 要通过JS发送电子邮件,您需要连接nodemailer

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');

and create transport: 并创建运输:

let mailTransportAuth = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {
        user: 'email',  /
        pass: 'password'
    }
});

Then create a function that will monitor the changes (onUpdate) in Realtime database and send letters: 然后创建一个函数来监视实时数据库中的更改(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.

相关问题 如何使用电子邮件从Firebase实时数据库检索数据? - How to retrieve data using email from firebase realtime databse? 使用存储在 firebase 实时数据库中的电子邮件登录 - Login using email stored in firebase realtime database 无法使用FirebaseRecyclerViewAdapter从Firebase实时数据库检索数据 - Not able to retrive data from Firebase Realtime Database using FirebaseRecyclerViewAdapter 如何使用TypeScript为Firebase Realtime数据库编写的云函数从onCreate触发的节点之外的节点获取数据? - How to get the data from the node other than triggered by onCreate using cloud functions written in TypeScript for Firebase Realtime database? 如何使用 firebase 函数将从网站抓取的值插入到 firebase 实时数据库中? - How to insert value crawlled from a website to the firebase realtime database using firebase functions? 如何从Firebase实时数据库中选择数据 - How to select data from firebase realtime database 从Android中的Firebase实时数据库获取数据 - Get data from firebase realtime database in android 无法从Firebase实时数据库获取数据 - Unable to fetch data From Firebase Realtime Database 实时从数据库中检索数据的问题 Firebase - Issues retrieving data from database in Realtime Firebase 显示来自 Firebase 实时数据库的数据 - Display data from Firebase Realtime Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM