简体   繁体   中英

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

  • 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 .

  • Go on and do that now (check bottom left side of window).

  • Go on and set-up firestore database and storage . This is necessary for both extension and functions to work.

Firebase 控制台


Configuring Extensions

  • Click on Extensions panel under Build .

  • Find Trigger Mail extension and click on install.

触发邮件

Here's the main part:
在此处输入图像描述

  • Click on next 2 times.

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:

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

Use this in the SMTPS connection URI field.

Password

This is a little hectic step.

  • Enable 2 factor Authorization in your Gmail here .

  • 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 .

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.

  • Let Email documents collection be the same.

  • 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.

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 .

  • Let's create a collection manually ->

在此处输入图像描述

Here's my document 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.

  • Check your mail for the email.


Writing a function.

  • Lets set up Firebase CLI
  • Download Node.js here .
  • Run the installer.
  • Copy the installed path in your drive.
  • I have mine installed under C:\Program Files\nodejs .

在此处输入图像描述

  • Search environment variables in your system tray.

在此处输入图像描述

在此处输入图像描述

  • Paste the directory under System Variables -> Path , create new and add.

  • Download and install Firebase CLI by following the steps here. .

  • login to firebase cli using the above doc.

  • Open your project in code editor, and type firebase init in terminal.

  • Select project and add functions support. It'll create a new folder functions .

  • I've written a function that sends a onboarding email when a new user is created.

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 . really recommend this channel.
Official Trigger-mail docs .
Firebase CLI docs .
Firebase Cloud Functions docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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