Join us
@sofiatarhonska ・ Oct 04,2022 ・ 6 min read ・ 1203 views ・ Originally posted on mailtrap.io
Firebase is a Google-owned web and mobile application development platform that allows you to create high-quality apps and take your business to the next level. You can also use the platform to send emails and enhance the capabilities of your apps.
In this article, we’re going to talk about how to send emails with Firebase and Nodemailer and how to test them with Mailtrap.
Are you ready?
Let’s get going…
Step 1.1: Create a Firebase project
Go to your instance of the Firebase UI and create a new project.
Step 1.2: Install Firebase CLI
The Firebase CLI is a versatile utility that provides you with an easy way to manage, view, and deploy code and assets to your Firebase project.
You can install the utility on your development environment using a method that matches your preferences and use cases. Regardless of how you install it, you’ll still get the same functionalities.
For this tutorial, we’ll use the following command in our Windows Command Prompt to install the CLI globally:
Note that you’ll need to have Node.js installed on your system before running the above command.
After installing it, you can create a new directory that will contain the code for this Firebase project.
Then, go to the directory and run the following command to sign in to your Firebase account:
Then, you’ll need to follow the ensuing prompts so that you can be authenticated into the Firebase platform. This way, you’ll be granted the right to access your Firebase projects locally, as your local machine will be connected to Firebase.
Step 1.3: Initialize Firebase SDK for Cloud Functions
Cloud Functions allow you to access Firebase events directly in your application. This way, you can easily integrate with the Firebase platform and accomplish a wide range of tasks.
Initializing Firebase SDK for Cloud Functions will allow you to create an empty project that contains some dependencies you can use to build the rest of your application.
After logging in successfully, you can initialize a Firebase project in your local directory by following these steps:
After the installation is complete, the structure of your new directory will look something like this:
For the rest of this tutorial, we’ll only make use of the functions directory. And, we’ll use the index.js file to put all our code.
Step 1.4: Install Firebase Admin SDK
The Admin SDK allows you to access Firebase from privileged environments and carry out various tasks, such as sending Firebase Cloud Messaging messages programmatically.
To install it, on the Command Prompt, navigate to your functions folder and run the following command:
Then, go to your index.js file and import and initialize the Admin SDK.
Here is the code:
Mailtrap is a free email testing tool to view and share emails in a development environment. You can use it to inspect and debug your emails and avoid spamming your real customers.
Setting it up is quick and easy. Just go to the signup page and register for a free account.
Then, go to the SMTP settings tab in your inbox and copy the details for host, port, username, and password. We’ll use these details in the next section of this tutorial.
You can also use Mailtrap Email API and send emails on production. After testing, you only need to complete a 5-minute domain verification, and you should be good to go.
Nodemailer is a simple module for sending email with Node.js applications. It comes with a wide range of features for allowing you to send emails fast, efficiently, and securely.
To install Nodemailer, go to your functions directory and run the following command:
Then, go to your index.js file and import it into your project.
Here is the code:
Next, create a reusable Nodemailer transporter object using your Mailtrap’s SMTP information.
Here is the code:
In this case, transporter will be an object that can be used to send emails.
Notice that’s the same code available in your Mailtrap inbox—if you select Nodemailer, under the Integrations section.
For this tutorial, we’ll create a Firebase HTTP Cloud Function, which will be triggered whenever its URL is executed in the browser.
Here is its syntax:
As you can see on the code above, we named the function emailSender (you can call it any name). Then, we used the functions library with the https API and the onRequest event to register the function.
The callback event handler function accepts two parameters: req and res. While the req object provides you with access to the properties of the initial HTTP request sent by the client, the res object allows you to send a response back to the client.
Next, let’s use the Nodemailer module to build the email message inside the emailSender function.
Here is the code:
As you can see in the code above, we started by defining mailOptions using the following properties: from, to, subject, and html. More options are available on the Nodemailer documentation.
Notice that we used req.query.dest to get the recipient’s email address by a query string.
Next, we applied the previously created transporter object to send the email message using the sendMail() method. We passed two parameters to the method: the data that defines the mail content and a callback function that is executed once the message is delivered or fails.
While err is the error object returned when the message fails, the info object includes the results of the sent message.
In this case, our function will return the sent email message, if everything is successful, or an error message if the message is not delivered.
Lastly, we’ll need to deploy the project’s function to Firebase. So, we’ll use the Firebase CLI again.
To do this, navigate to your functions directory and run the following command:
Once the upload process is completed successfully, you’ll get a function URL that you can use to trigger the Cloud Function.
The URL will look like this:
Notice that the Firebase project ID and the function’s name are in the URL. To execute the function, we just include the dest parameter in the URL and run it in a browser:
That’s how to send the email!
Here is the entire code for the index.js file:
As we’ve illustrated in this tutorial, sending emails with Firebase, Nodemailer and Mailtrap is simple and straightforward. Mailtrap offers you a useful way to test your emails in a pre-production environment. This way, you can debug your email samples before distributing them to real users.
Want to go fancy and send emails from pure JavaScript (yes, it’s possible!), we have thought about you and prepared a guide.
And don’t forget that you can also send emails via Mailtrap Email API, which is among the quickest possible options.
Happy sending emails!
Thank you for reading our article on sending emails with Firebase and Nodemailer that was originally published on Mailtrap Blog.
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.