Forward fax to email with SendGrid and Node.js

It's 2019 and you need to receive a fax. What do you do? You could buy a fax machine, hook it up to a phone line and hand out your number. But it's 2019 not 1979, we're living in the future, so let's grab Node.js, pick a couple of APIs and turn that fax into an email instead.

It's 2019 and you need to receive a fax. What do you do? You could buy a fax machine, hook it up to a phone line and hand out your number. But it's 2019 not 1979, we're living in the future, so let's grab Node.js, pick a couple of APIs and turn that fax into an email instead.

You're going to need a Twilio account, a SendGrid account and this noise to remind you what you're missing out on as you build your very own fax-to-email converter.

When you have a number ready to go we're going to need to set up to receive some web hooks. You might think fax would work the same as messaging, but it's more like voice calls. We need to respond to two incoming webhooks. For the first one we have two choices: to receive or reject the fax. We can do this with the <Receive> or <Reject> fax TwiML verbs.

Rejecting an incoming fax hangs up the connection and we're done. Opting to receive an incoming fax means Twilio will answer the incoming call and receive the fax on your behalf. To do this, we need to set a second webhook URL as the action attribute of the <Receive> element that will be requested when the fax has been fully received.

This second webhook is where we're going to do all the work of downloading the fax as a PDF and sending it as an email. We'll build this as a Twilio Function using Node.js (though you can do this in any language and host the application yourself).

Downloading the fax

Config

We're going to use the request npm module to both download the fax PDF file, like my teammate Sam did with media in MMS messages, and also to send it on to the SendGrid API. Open up the Twilio console's Runtime dependencies section and add request version 2.88.0.

While you're in the config section, create an API key in your SendGrid account (make sure it has permission to send emails) and save it as an environment variable called SENDGRID_API_KEY.

We need two more bits of config before we build our Function. You need an email address to send the fax to, and one to send from too. Add TO_EMAIL_ADDRESS and FROM_EMAIL_ADDRESS to the environment variables.

Make sure you save the config before you move on to the next part.

Writing the Function

Create a new Function and choose the blank template. We'll start the code off by requiring request and creating the handler function.

const request = require('request');

exports.handler = function(context, event, callback) {

}

The webhook request sends us a URL which describes the location of the PDF file containing the fax. The URL is in the parameter MediaUrl.

We're going to download that PDF with request. To send it on to the SendGrid API, we're going to need it as a Buffer and we can do that with request by setting the encoding to null.

Add the following to your Function:

exports.handler = function(context, event, callback) {
const faxUrl = event.MediaUrl;
request.get({ uri: faxUrl, encoding: null }, (error, response, body) => {
// body is the PDF file as a Buffer object
});
}

Now we need to build up the request we want to send to the SendGrid API. I explored this before when I built a Function to forward SMS messages as emails. Add the following code within the callback:

  request.get({ uri: faxUrl, encoding: null }, (error, response, body) => {
const email = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: New fax from ${event.From},
content: [
{
type: 'text/plain',
value: 'Your fax is attached.'
}
],
attachments: []
};
// more to come
}

We add to and from email addresses using the environment variables we saved earlier. The subject says that there is a new fax from the number that sent it and the content is a simple message that says there is a fax attachment. Finally we add an array of attachments.

If the fax was downloaded successfully, we'll add it as an attachment to the email. To do so, we provide it as an object with three keys:

  • content: a base64 encoded string from the Buffer of the PDF we downloaded
  • filename: created from the fax's Sid identifier
  • type: the MIME type of the file which we can get straight from the headers in the response from downloading the fax
  request.get({ uri: faxUrl, encoding: null }, (error, response, body) => {
const email = { ... };
if (!error && response.statusCode === 200) {
email.attachments.push({
content: body.toString('base64'),
filename: ${event.FaxSid}.pdf,
type: response.headers['content-type']
});
}
// more to come
}

If there was an error downloading the fax, we skip adding the attachment but we will go on to send the email anyway as a notification.

Now we've built up the email, we need to send it to the SendGrid API. We'll send as JSON, packaging the email object we've created here as the body and adding the API token we created earlier as authorization.

If the response is a success with a 202 status code then we can send an empty TwiML <Response> to Twilio to let it know everything was fine. If there was an error then we pass the error or the body as the first argument to the callback so that our Function logs it as an error.

  request.get({ uri: faxUrl, encoding: null }, (error, response, body) => {
const email = { ... };
if (!error && response.statusCode == 200) {
// add attachment
}
request.post(
{
uri: 'https://api.sendgrid.com/v3/mail/send',
body: email,
auth: {
bearer: context.SENDGRID_API_KEY
},
json: true
},
(error, response, body) => {
if (error) {
return callback(error);
} else {
if (response.statusCode === 202) {
return callback(null, new Twilio.twiml.VoiceResponse());
} else {
return callback(body);
}
}
}
);
}

That's all we need to forward the fax on. The full code is below:

const request = require('request');

exports.handler = function(context, event, callback) {
const faxUrl = event.MediaUrl;

request.get({ uri: faxUrl, encoding: null }, (error, response, body) => {
const email = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: New fax from ${event.From},
content: [
{
type: 'text/plain',
value: 'Your fax is attached.'
}
],
attachments: []
};
if (!error && response.statusCode === 200) {
email.attachments.push({
content: body.toString('base64'),
filename: ${event.FaxSid}.pdf,
type: response.headers['content-type']
});
}
request.post(
{
uri: 'https://api.sendgrid.com/v3/mail/send',
body: email,
auth: {
bearer: context.SENDGRID_API_KEY
},
json: true
},
(error, response, body) => {
if (error) {
return callback(error);
} else {
if (response.statusCode === 202) {
return callback(null, new Twilio.twiml.VoiceResponse());
} else {
return callback(body);
}
}
}
);
});
}

Give the Function a path and save it.

Putting it all together

Head back to edit your fax capable number. In the "Voice & Fax" section make sure you are set to accept incoming faxes.

For "A fax comes in" select TwiML and then click the red button to create a new TwiML Bin to receive the incoming fax call. Enter the following TwiML, replacing the action URL with your Function URL:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Receive action="FUNCTION_URL" />
</Response>

Save the number config and you're ready to receive your faxes as emails.

Testing it out

As we already established, it's 2019 and we don't have a fax machine to test this with. Now, you can either head down to your local library or print shop and ask to borrow theirs, or pop open the Twilio API explorer and send yourself a fax via the API (you can use your existing fax number as both the To and From number here). When sending a fax you need to have a PDF hosted somewhere that Twilio can reach it. If you don't have one, feel free to use our test PDF file here.

Don't forget to play the authentic sounds of a fax machine as you send it off into the world.

Wait a couple of minutes (faxes take time!) and then check your email.

You should have received your new fax.

Time to celebrate!

No fax machines were harmed in the writing of this post

With the power of a Twilio number, JavaScript, a Twilio Function, a TwiML Bin and the SendGrid API we can now receive faxes direct to our email inbox.

We've seen how to use request to download files and then post them directly to the SendGrid API. You could use the same technique to forward incoming MMS messages to your email too.

Did you ever imagine that JavaScript and emails would solve fax? Got any other ideas for the future of classic tech? Drop me a message in the comments below or on Twitter with your other retro-futuristic inventions. Long live faxes in 2019!


By : Phil Nash



Top 7 Most Popular Node.js Frameworks You Should Know

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser.

One of the main advantages of Node is that it enables developers to use JavaScript on both the front-end and the back-end of an application. This not only makes the source code of any app cleaner and more consistent, but it significantly speeds up app development too, as developers only need to use one language.

Node is fast, scalable, and easy to get started with. Its default package manager is npm, which means it also sports the largest ecosystem of open-source libraries. Node is used by companies such as NASA, Uber, Netflix, and Walmart.

But Node doesn't come alone. It comes with a plethora of frameworks. A Node framework can be pictured as the external scaffolding that you can build your app in. These frameworks are built on top of Node and extend the technology's functionality, mostly by making apps easier to prototype and develop, while also making them faster and more scalable.

Below are 7of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Express

With over 43,000 GitHub stars, Express is the most popular Node framework. It brands itself as a fast, unopinionated, and minimalist framework. Express acts as middleware: it helps set up and configure routes to send and receive requests between the front-end and the database of an app.

Express provides lightweight, powerful tools for HTTP servers. It's a great framework for single-page apps, websites, hybrids, or public HTTP APIs. It supports over fourteen different template engines, so developers aren't forced into any specific ORM.

Meteor

Meteor is a full-stack JavaScript platform. It allows developers to build real-time web apps, i.e. apps where code changes are pushed to all browsers and devices in real-time. Additionally, servers send data over the wire, instead of HTML. The client renders the data.

The project has over 41,000 GitHub stars and is built to power large projects. Meteor is used by companies such as Mazda, Honeywell, Qualcomm, and IKEA. It has excellent documentation and a strong community behind it.

Koa

Koa is built by the same team that built Express. It uses ES6 methods that allow developers to work without callbacks. Developers also have more control over error-handling. Koa has no middleware within its core, which means that developers have more control over configuration, but which means that traditional Node middleware (e.g. req, res, next) won't work with Koa.

Koa already has over 26,000 GitHub stars. The Express developers built Koa because they wanted a lighter framework that was more expressive and more robust than Express. You can find out more about the differences between Koa and Express here.

Sails

Sails is a real-time, MVC framework for Node that's built on Express. It supports auto-generated REST APIs and comes with an easy WebSocket integration.

The project has over 20,000 stars on GitHub and is compatible with almost all databases (MySQL, MongoDB, PostgreSQL, Redis). It's also compatible with most front-end technologies (Angular, iOS, Android, React, and even Windows Phone).

Nest

Nest has over 15,000 GitHub stars. It uses progressive JavaScript and is built with TypeScript, which means it comes with strong typing. It combines elements of object-oriented programming, functional programming, and functional reactive programming.

Nest is packaged in such a way it serves as a complete development kit for writing enterprise-level apps. The framework uses Express, but is compatible with a wide range of other libraries.

LoopBack

LoopBack is a framework that allows developers to quickly create REST APIs. It has an easy-to-use CLI wizard and allows developers to create models either on their schema or dynamically. It also has a built-in API explorer.

LoopBack has over 12,000 GitHub stars and is used by companies such as GoDaddy, Symantec, and the Bank of America. It's compatible with many REST services and a wide variety of databases (MongoDB, Oracle, MySQL, PostgreSQL).

Hapi

Similar to Express, hapi serves data by intermediating between server-side and client-side. As such, it's can serve as a substitute for Express. Hapi allows developers to focus on writing reusable app logic in a modular and prescriptive fashion.

The project has over 11,000 GitHub stars. It has built-in support for input validation, caching, authentication, and more. Hapi was originally developed to handle all of Walmart's mobile traffic during Black Friday.

Difference between AngularJS, React, Ember, Backbone, and Node.js.

The most common thing between all of them is that they are Single Page Apps. The SPA is a single page where much of the information remains the same and only some piece of data gets modified when you click on other categories/option.

Node.js Tutorial for Beginners | Node.js Crash Course | Node.js Certification Training

This courseis designed for professionals who aspire to be application developers and gain expertise in building real-time, highly-scalable applications in Node.js. The following professionals can go for this course :

Why learn Node.js?

Node.js uses JavaScript - a language known to millions of developers worldwide - thus giving it a much lower learning curve even for complete beginners. Using Node.js you can build simple Command Line programs or complex enterprise level web applications with equal ease. Node.js is an event-driven, server-side, asynchronous development platform with lightning speed execution. Node.js helps you to code the most complex functionalities in just a few lines of code...

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

The Complete Node.js Developer Course (3rd Edition)

Angular & NodeJS - The MEAN Stack Guide

NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

Docker for Node.js Projects From a Docker Captain

Intro To MySQL With Node.js - Learn To Use MySQL with Node!

Node.js Absolute Beginners Guide - Learn Node From Scratch

React Node FullStack - Social Network from Scratch to Deploy

Selenium WebDriver - JavaScript nodeJS webdriver IO & more!

Complete Next.js with React & Node - Beautiful Portfolio App

Build a Blockchain & Cryptocurrency | Full-Stack Edition