【问题标题】:Send data from Realtime Database using Firebase Functions to email使用 Firebase 函数将实时数据库中的数据发送到电子邮件
【发布时间】:2019-07-01 13:54:54
【问题描述】:

如何通过电子邮件将某些数据发送到 Firebase Functions?假设有一个授权用户,我们将他的一些数据(例如出生日期)放入实时数据库。如何使用 Firebase 函数在 HTTP 请求后将此字段发送到电子邮件?

【问题讨论】:

  • 您要导出所有实时数据库并发送到电子邮件吗?
  • @NickUnuchek,不,我想发送一个特定的字段(在这个用户的分支中)
  • 就目前而言,您的问题有点过于宽泛。它读起来像“这是我的用例,我该怎么做?”对于 Stack Overflow 来说,这不是一个好的格式,我们最好处理更具体和更集中的问题。这里涉及三种复杂的技术:触发云函数、读取数据库、发送电子邮件。您正在为其中的哪一个而苦苦挣扎?您已经尝试过哪些技术?什么没有奏效,或者给出的结果与您的预期不同?
  • @FrankvanPuffelen,事实上我是一名 Android 开发人员和 Node.js。 js真的不知道。。想弄明白,但需要一个方向来理解。有人可以做类似的事情并告诉我吗?

标签: android node.js firebase-realtime-database google-cloud-functions


【解决方案1】:

通过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;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2015-04-23
    • 2011-05-19
    • 2019-03-14
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多