简体   繁体   English

如何设置firebase触发邮件和云功能

[英]How to setup firebase trigger-mail and cloud functions

I faced a lot of trouble setting up trigger mail extensions along with cloud functions.我在设置触发邮件扩展和云功能时遇到了很多麻烦。 Here I explain step-by-step how to get things done!在这里,我将逐步解释如何完成工作!

Lets get working.让我们开始工作吧。

Set up Firebase设置Firebase

  • Create a project if you haven't already here .如果您还没有在这里,请创建一个项目。

  • To use trigger-mail extension and cloud functions, you need to upgrade the project to BLAZE Plan .要使用trigger-mail扩展和云功能,您需要将项目升级到BLAZE Plan

  • Go on and do that now (check bottom left side of window). Go 并立即执行此操作(检查窗口的左下角)。

  • Go on and set-up firestore database and storage . Go 并设置firestore数据库和storage This is necessary for both extension and functions to work.这对于扩展和功能的工作都是必要的。

Firebase 控制台


Configuring Extensions配置扩展

  • Click on Extensions panel under Build .单击Build下的Extensions面板。

  • Find Trigger Mail extension and click on install.查找Trigger Mail扩展并单击安装。

触发邮件

Here's the main part:这是主要部分:
在此处输入图像描述

  • Click on next 2 times.单击下一步 2 次。

Grant all necessary permissions.授予所有必要的权限。

在此处输入图像描述

This is where you'll link your mail account from which you'll be sending mail在这里您将链接您将从中发送邮件的邮件帐户

You'll be greeted with such a screen ->您会看到这样的屏幕-> smtp配置

URI网址

If the mail I'm linking is xyz123@gmail.com , this will be your SMTPS format:如果我链接的邮件是xyz123@gmail.com ,这将是您的 SMTPS 格式:

smtps://xyz123@gmail.com@smtp.gmail.com:465 smtps://xyz123@gmail.com@smtp.gmail.com:465

Use this in the SMTPS connection URI field.SMTPS connection URI字段中使用它。

Password密码

This is a little hectic step.这是一个有点紧张的步骤。

  • Enable 2 factor Authorization in your Gmail here .此处的 Gmail 中启用 2 因素授权。

  • Now you would need to create an App Password现在您需要创建应用密码

应用密码

  • Click on Generate.单击生成。

  • You'll see such a screen ->你会看到这样的画面 ->

在此处输入图像描述

  • You have to enter this password in the SMTP password field and click Create secret .您必须在SMTP 密码字段中输入此密码,然后单击Create secret

NOTE: Do not enter spaces.注意:不要输入空格。

  • Wait for sometime for the process to finish.等待一段时间以完成该过程。

  • After it's done, Your screen will look like this ->完成后,您的屏幕将如下所示 ->

在此处输入图像描述

  • You could keep the same Gmail for Default Reply-To address as the original mail, or one of your choice.您可以保留与原始邮件相同的默认回复地址 Gmail,或者您的选择之一。

  • Let Email documents collection be the same.Email 个文档集合相同。

  • Click on Install Extension.单击安装扩展。

在此处输入图像描述

This will take few minutes.这将需要几分钟时间。 * *

Voila, you're done!瞧,你完成了!


Let's send a test mail.让我们发送一封测试邮件。

Now in-order to send a mail, you need to add a document to mail collection in your firestore db.现在为了发送邮件,您需要将文档添加到您的firestore数据库中的mail集合中。

Find official documentation here . 在此处查找官方文档。

to: ['someone@example.com'],
message: {
  subject: 'Hello from Firebase!',
  text: 'This is the plaintext section of the email body.',
  html: 'This is the <code>HTML</code> section of the email body.',
}
  • This is the format of document to send mail.这是发送邮件的文档格式。

"to" is an array and "message" is a map . “to”是一个array“message”是一个map

  • Let's create a collection manually ->让我们手动创建一个集合->

在此处输入图像描述

Here's my document window这是我的文件 window

在此处输入图像描述

  • Let's save this document.让我们保存这份文件。

  • If done correctly, within few seconds, you'll see the document automatically update with more fields like attempts etc.如果操作正确,几秒钟内,您将看到文档自动更新更多字段,如attempts等。

  • Check your mail for the email.查看您的邮件以查找 email。


Writing a function.写一个 function。

  • Lets set up Firebase CLI让我们设置Firebase CLI
  • Download Node.js here .在这里下载 Node.js。
  • Run the installer.运行安装程序。
  • Copy the installed path in your drive.复制驱动器中的安装路径。
  • I have mine installed under C:\Program Files\nodejs .我的安装在C:\Program Files\nodejs

在此处输入图像描述

  • Search environment variables in your system tray.在系统托盘中搜索environment variables

在此处输入图像描述

在此处输入图像描述

  • Paste the directory under System Variables -> Path , create new and add.将目录粘贴到System Variables -> Path下,新建并添加。

  • Download and install Firebase CLI by following the steps here.按照此处的步骤下载并安装Firebase CLI . .

  • login to firebase cli using the above doc.使用上述文档登录到 firebase cli。

  • Open your project in code editor, and type firebase init in terminal.在代码编辑器中打开您的项目,然后在终端中键入firebase init

  • Select project and add functions support. Select 项目并添加功能支持。 It'll create a new folder functions .它将创建一个新文件夹functions

  • I've written a function that sends a onboarding email when a new user is created.我写了一个 function,它在创建新用户时发送入职 email。

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

//  sends mail if new user is regestired
exports.userOnboardingMail = functions.auth.user().onCreate((user)=>{
  admin.firestore().collection("mail").add({
    "to": [user.email],
    "message": {
      "subject": "Welcome to Textel Alert! Explore functionalities here.",
      "text": `Hi, ${user.displayName}. \n\nIt's nice to have you on-board.`,
    },
  })
      .then((result) => {
        console.log(
            "onboarding email result: ", result,
            "\ntime-stamp: ", Date.now);
      });
});

Hope I was able to make your day a bit easier:)希望我能让你的一天更轻松一点:)
Upvote if it helped..有帮助的话点个赞吧。。


Additional Links附加链接

Learn firebase cloud functions here . 在这里学习 firebase 云函数。 really recommend this channel.真心推荐这个频道。
Official Trigger-mail docs .官方Trigger-mail 文档
Firebase CLI docs . Firebase CLI文档
Firebase Cloud Functions docs Firebase 云函数文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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