Reputation: 1019
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
Reputation: 1019
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
.
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.
Click on Extensions
panel under Build
.
Find Trigger Mail
extension and click on install.
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 [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
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.
Let Email documents collection be the same.
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.
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.',
}
"to" is an
array
and "message" is amap
.
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.
Firebase CLI
C:\Program Files\nodejs
.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 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..
Learn firebase cloud functions here. really recommend this channel.
Official Trigger-mail
docs.
Firebase CLI docs.
Firebase Cloud Functions docs
Upvotes: 3