[英]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.让我们开始工作吧。
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.这对于扩展和功能的工作都是必要的。
Click on Extensions
panel under Build
.单击
Build
下的Extensions
面板。
Find Trigger Mail
extension and click on install.查找
Trigger Mail
扩展并单击安装。
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 ->您会看到这样的屏幕->
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 ->你会看到这样的画面 ->
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.这将需要几分钟时间。 *
*
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.',
}
"to" is an
array
and "message" is amap
.“to”是一个
array
, “message”是一个map
。
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。
Firebase CLI
Firebase CLI
C:\Program Files\nodejs
.C:\Program Files\nodejs
。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..有帮助的话点个赞吧。。
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.