繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM