Sending Email with Firebase Functions and Nodemailer
Serverless function that allows you to send email with Nodemailer and no backend
In this post we will be using Firebase Functions to send email with Sendgrid. Nodemailer is a NodeJS based emailing module. Although Nodemailer is completely free, Firebase team recommends Sendgrid to developers to use with Firebase Functions. I have another example here that shows you how to use Sendgrid with Firebase functions. The code will make a single function that you can view in the firebase console for quick debugging.
Installation
This step can be skipped if you already have a Firebase functions project setup.
- initialize firebase functions via the command line
To do this you need to run the following command on your command line tool
firebase init
- Install the needed package: firebase-functions, nodemailer, nodemailer-smtp-transport and cors
yarn add firebase-functions nodemailer-smtp-transport cors nodemailer
Code
Below is the full code for this example. The JSON body that is being received is simple form data and we will be sending the information to an admin to let them know that their website has collected new user data. We will also be using a gmail address to send email from.
const functions = require("firebase-functions");
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
const cors = require("cors")({
origin: true
});
exports.emailMessage = functions.https.onRequest((req, res) => {
const { name, email, phone, message } = req.body;
return cors(req, res, () => {
var text = `<div>
<h4>Information</h4>
<ul>
<li>
Name - ${name || ""}
</li>
<li>
Email - ${email || ""}
</li>
<li>
Phone - ${phone || ""}
</li>
</ul>
<h4>Message</h4>
<p>${message || ""}</p>
</div>`;
var sesAccessKey = 'YOURGMAIL@gmail.com';
var sesSecretKey = 'password';
var transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: sesAccessKey,
pass: sesSecretKey
}
}));
const mailOptions = {
to: "myemail@myemail.com",
from: "no-reply@myemail.com",
subject: `${name} sent you a new message`,
text: text,
html: text
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error.message);
}
res.status(200).send({
message: "success"
})
});
}).catch(() => {
res.status(500).send("error");
});
});
This example grabs data that was sent via req.body
. Once we have the data we start to construct an email. Nodemailer supports both text and html messages. In this example we craft out an html based body that uses no styling. Once we have our body, we start to create our email. The email consist of 5 fields: to, from, subject, text and html. After the email is crafted, we just need to log into our gmail and then attempt to send the message. If we succeed, we send a 200(okay) status code as well as a nice little success message. Errors will result in a 500(server error) status code and an error message.
Deploying
Run Firebase deploy on your command line to deploy this function.
firebase deploy --only functions
If successful, you will see it on your firebase console under the name emailMessage