Delwinn
Delwinn

Reputation: 1019

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!

Upvotes: 1

Views: 738

Answers (1)

Delwinn
Delwinn

Reputation: 1019

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 Console


Configuring Extensions

  • Click on Extensions panel under Build.

  • Find Trigger Mail extension and click on install.

Trigger mail

Here's the main part:
enter image description here

  • Click on next 2 times.

Grant all necessary permissions.

enter image description here

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 config

URI

If the mail I'm linking is [email protected], this will be your SMTPS format:

smtps://[email protected]@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

App Password

  • Click on Generate.

  • You'll see such a screen ->

enter image description here

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

enter image description here

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

enter image description here

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: ['[email protected]'],
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 ->

enter image description here

Here's my document window

enter image description here

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

enter image description here

  • Search environment variables in your system tray.

enter image description here

enter image description here

  • 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 My app! 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

Upvotes: 3

Related Questions