Forwarding SMS Messages to Email Using Node.js

    Overview

    Businesses get communications through many channels. It can be handy to have a searchable archive of messages in one place. Forwarding SMS messages to email lets you keep both kinds of messages in one spot. Plivo makes it easy to forward SMS messages to email using the most popular web development languages. Here we walk through the process with .NET.

    Prerequisites

    • Plivo account: Sign up for a Plivo account if you don’t have one already.

    • Plivo phone number: To receive SMS, you must have a Plivo phone number that supports SMS. You can purchase numbers from the Numbers page of the Plivo console or by using the Numbers API.

      Buy a New Plivo Number

    The code example below presumes you have a Gmail account, but it’s easy to edit the code to support another SMTP client.

    Install Node.js and other modules

    You must set up and install Node other modules to Forward Incoming SMS to Email. Here’s how.

    Installation

    Operating SystemInstructions
    macOS and LinuxTo see if you already have Node.js installed, run the command node --version in the terminal. If you don't have it installed, you can install it from here .
    WindowsTo install Node.js on Windows you can download it from here and install.

    Create project

    • Create a project directory, run the following command:

      $ mkdir mynodeapp
      
    • Change the directory to our project directory in the command line:

      $ cd mynodeapp
      

    Install npm modules

    • Install the SDK using npm

      $ npm install express
      $ npm install nodemailer
      $ npm install body-parser
      

    Forward SMS to Email

    Create a file (we called ours email.js) and paste into it this code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    
    var express = require('express');
    var nodemailer = require("nodemailer");
    var app = express();
    
    app.use(express.urlencoded({extended: true}));
    app.set('port', (process.env.PORT || 5000));
    app.all('/email_sms/', function(request, response) {
        // Sender's phone number
        var from_number = request.body.From || request.query.From;
        // Receiver's phone number - Plivo number
        var to_number = request.body.To || request.query.To;
        // The text which was received
        var text = request.body.Text || request.query.Text;
        // Print the message
        console.log('Message received from: ' + from_number + ': ' + text);
    
        var transporter = nodemailer.createTransport({
            service: 'gmail',
            auth: {
                user: "<email_address>",
                pass: "<password>"
            }
        });
    
        var mailOptions = {
            from: "<from_email_addres>", // sender address
            to: "<recipient_email_address>", // comma separated list of receivers
            subject: "SMS from ",
            from_number, // Subject line
            text: text // plaintext body
        };
    
        transporter.sendMail(mailOptions, function(error, info) {
            if (error) {
                console.log(error);
            } else {
                console.log('Email sent: ' + info.response);
            }
        });
    });
    
    app.listen(app.get('port'), function() {
        console.log('Node app is running on port', app.get('port'));
    });
    

    To run this file on the server, go to the folder where the file resides and use the command

    $ node email.js
    
    Note: If you use Gmail to send email, you'll have to use an app password, which will be treated as your password to send email from the app.

    Expose your local server to the internet

    To receive Incoming Messages and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.

    ngrok block diagram

    Install ngrok and run it on the command line, specifying the port that hosts the application on which you want to receive messages (5000 in this case):

    $ ./ngrok http 5000
    

    Ngrok will display a forwarding link that you can use as a webhook to access your local server over the public network.

    Sample ngrok CLI

    Create an Application

    1. Create a Plivo application by visiting Messaging > Applications and clicking on Add New Application, or by using Plivo’s Application API.
    2. Give your application a name — we called our Email SMS. Enter your server URL (for example https://61d8fb8f250c.ngrok.io/email_sms/) in the Message URL field and set the method as POST.
    3. Click on 'Create Application' to save your application.

    Create Application

    Assign a Plivo number to your app

    1. Navigate to the Numbers page and select the phone number you want to use for the application.
    2. Select XML Application from the Application Type drop-down list, and Email SMS (the name of the application) from the Plivo Application drop-down list.
    3. Click on Update Number to save.

    Assign Application

    Test and validate

    Then send a text message to the Plivo number you associated with the application using a regular mobile phone.The incoming message should be reflected in your email.