在Firebase实时数据库中创建新密钥时如何获取电子邮件 [英] How to get an email when new key is created in Firebase real time database

查看:93
本文介绍了在Firebase实时数据库中创建新密钥时如何获取电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我几乎到处都看过,而且显然有Firebase Cloud Functions,只要在数据库中创建新密钥,就可以用来向我发送电子邮件.但是,我无法从头开始以及如何做.有人可以帮我吗.

Well, I have looked almost everywhere, and apparently there is Firebase Cloud Functions which can be used to send me an email whenever a new key is created in my database. However, I cannot wrap my head around where to start and how to do it. Can someone help me please.

推荐答案

您确实可以在每次在数据库中创建新节点(即新密钥)时使用Cloud Function发送电子邮件.

You can indeed use a Cloud Function to send an email each time a new node (i.e. a new key) is created in the database.

此处以及在正式的Cloud Functions示例之一,展示了如何发送电子邮件,

Have a look at the documentation for Realtime Database triggers here and at one of the official Cloud Functions sample which shows how to send an email, here.

在该示例中,发送电子邮件是为了响应用户帐户的创建和删除,但是将示例代码与Realtime Database触发器集成起来确实并不困难.

In the sample, emails are sent in response to the creation and deletion of user accounts, but it is really not difficult to integrate the sample code with a Realtime Database trigger.

例如,您可以根据样本进行以下操作:

For example you could do something like the following, adapted from the sample:

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For Gmail, enable these:
// 1. https://www.google.com/settings/security/lesssecureapps
// 2. https://accounts.google.com/DisplayUnlockCaptcha
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: gmailEmail,
    pass: gmailPassword,
  },
});

// Your company name to include in the emails
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = 'xxxxxx';

exports.sendWelcomeEmail = functions.database.ref('/thePathYouWant/{pushId}')
    .onCreate((snapshot, context) => {

       const createdData = snapshot.val(); // data that was created

       const email = createdData.email; // The email of the user. We make the assumption that it is written at the new database node
       const displayName = createdData.displayName; // The display name of the user.

       return sendWelcomeEmail(email, displayName);
});

// Sends a welcome email to the given user.
async function sendWelcomeEmail(email, displayName) {
  const mailOptions = {
    from: `${APP_NAME} <noreply@firebase.com>`,
    to: email,
  };

  // The user subscribed to the newsletter.
  mailOptions.subject = `Welcome to ${APP_NAME}!`;
  mailOptions.text = `Hey ${displayName || ''}! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
  await mailTransport.sendMail(mailOptions);
  return console.log('New welcome email sent to:', email);
}

如果您完全不熟悉Cloud Functions for Firebase,那么最好遵循教程此处.

If you are totally new to Cloud Functions for Firebase, it is probably a good idea to follow the tutorial here and to watch the official video series, here.

这篇关于在Firebase实时数据库中创建新密钥时如何获取电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆