I am sending an E-mail to the User in Flutter using Firebase Cloud Functions.
1. Install NodeJS: -
Download NodeJS Click here
Install Node.js skip the additional features.
After installing successfully verify that NodeJS is installed successfully or not using the following CMD command
node -v
You get the installed version number of NodeJS.
2. Install Firebase Tools: -
Write the following command to install Firebase Tools CMD
npm install -g firebase-tools
3. Login to Firebase: -
firebase login
It will open a web browser and you must log in with your Google Account.
4. Create Project: -
Open the Firebase Console
Create a new project.
Setup with the default settings no need to change anything.
Open the File Explorer.
Create a new folder where you want to create a project.
mkdir sendMail
cd sendMail
Initialize the Firebase project on local pc
firebase init
Enter y
Select Language: JavaScript
Go with the default settings.
And your Firebase Project is created on a Local PC.
Open the VS-Code and open that folder.
5. Install packages: -
To send mail you have to install 2 packages.
After opening your project in VS-Code.
cd functions
npm install nodemailer
npm install cors
6. Setup your G-mail Account: -
Please don’t use your account.
Open the Google page and sign in with your account.
Open the following two links and enable both.
7. Write a function to send mail: -
Under the functions folder, index.js write code to send mail as below.
You can use your own HTML as a body.
You can also take the username as an argument to display its name in the mail
const functions = require('firebase-functions');const admin = require('firebase-admin');const nodemailer = require('nodemailer');const cors = require('cors')({origin: true});admin.initializeApp();let transporter = nodemailer.createTransport({service: 'gmail',auth: {user: 'agile.developers1804@gmail.com',pass: '*******' //you your password}});exports.sendMail = functions.https.onRequest((req, res) => {cors(req, res, () => {// getting dest email by query stringconst dest = req.query.dest;const mailOptions = {from: 'Agile Developers <agile.developers1804@gmail.com>', //
to: dest,subject: 'Welcome to ABC', // email subjecthtml: `Dear User, Welcome to ABC, <p>thank you for choosing us `};// returning resultreturn transporter.sendMail(mailOptions, (erro, info) => {if(erro){return res.send(erro.toString());}return res.send('Sended');});});});
8. Run the Function: -
//TO-DO go-to your project folder
firebase serve
//here you can find your URL to test Firebase Cloud Function
Here you can find the URL to call that function. You can call that function using the following URL
http://localhost:5000/fcfdemo-b3a7f/us-central1/sendMail?=”destEmailID”
Example: — http://localhost:5000/fcfdemo-b3a7f/us-central1/sendMail?=bhavikvashi180498@gmail.com
9. Deploy the Firebase Function: -
Go to your project directory
Open cmd here
firebase deploy
10. Open Firebase Console and Grab the Deployed URL: -
Open Firebase Console
Here you can find the URL of deployed Firebase function.
In this case, your URL is: https://us-central1-fcfdemo-b3a7f.cloudfunctions.net/sendMail
11. Create a Flutter project and use this URL to send mail: -
You can also accept mail via TextFormField to make it more accurate.
Use I have just used my Email ID to send
import 'package:flutter/material.dart';import 'package:http/http.dart' as http;void main() {runApp(MyApp());}class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',home: HomePage(),);}}class HomePage extends StatelessWidget {@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text("Firebase Cloud Function"),),body: Center(child: RaisedButton(onPressed: ()async{var res=await http.get('https://us-central1-fcfdemo- b3a7f.cloudfunctions.net/sendMail?dest=bhavikvashi180498@gmail.com');print(res.body);},child: Text('Send Mail'),),),);}}
12. Run Firebase Cloud Function: -
Run your app
Press the Button Send Mail
And receiver has new mail in his Inbox