Forwarding SMS Messages to Email Using Ruby

    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: Click here to sign-up for a Plivo account if you haven’t already!

    • Plivo Phone Number: To receive SMS, you must have a Plivo phone number that supports SMS. Numbers can be purchased from the Numbers section of your Plivo Console and use the same as the source number/from number for the outbound SMS. In order to receive SMS you must have a SMS-enabled Plivo phone number to do the same. Please note that you can also purchase numbers using the Numbers API.

      Buy a New Plivo Number

    Install Rails and other modules

    You must install rails, create a rails project and other modules to forward incoming SMS to your email. Here’s how.

    Install Rails

    Use the below command to install Rails

    $ gem install rails
    

    Create a Rails Project

    As we have Rails and its dependencies installed, we can use them to create a new Rails project. As the initial step, using rails we can auto-generate code in the ruby on rails folder structure. Use the below command to start your Rails project.

    $ rails new plivotest
    

    This will create a plivotest directory with the necessary folders & files for development.

    Install Library

    As we have the rails application created, now, let’s add library named Mail by modifying the Gemfile. Open the Gemfile in any IDE/text-editor and add the following line:

    gem 'mail', '~> 2.6'
    

    You can use the below command to install the Mail gem into the bundle:

    $ bundle install
    

    Create a Rails Controller

    Change the directory to our newly created project directory, i.e, plivotest directory and run the below command to create a rails controller.

    $ rails generate controller Plivo email
    

    This will generate a controller named plivo_controller in the app/controllers/ directory and a respective view will be generated in app/views/plivo directory. We can delete the View as we will not need it.

    $ rm app/views/plivo/email.html.erb
    

    Code

    Now, You have to open the app/controllers/plivo_controller.rb file and add the following 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
    
    require 'mail'
    
    class PlivoController < ApplicationController
    def email()
        # The phone number of the person who sent the SMS
        from_number = params[:From]
        # Your Plivo number that will receive the SMS
        to_number = params[:To]
        # The text which was received on your Plivo number
        text = params[:Text]
    
        # Print the message
        puts "Message received from #{from_number}: #{to_number}: #{text}"
        forward_email(text, from_number)
    end
    
    def forward_email(text, from_number)
        options = {
            :address              => "smtp.gmail.com",
            :port                 => 587,
            :user_name            => '<username>',
            :password             => '<password>',
            :authentication       => 'plain',
            :enable_starttls_auto => true
        }
    
        Mail.defaults do
            delivery_method :smtp, options
        end
    
        Mail.deliver do
            to '<recipient_email_address>'
            from '<from_email_addres>'
            subject "SMS from #{from_number}"
            body text
        end
    end
    end
    
    Note:
  • If you use Gmail to send an email, Gmail will share your password with you. Please go to this page to set the password.
  • If you are using Rails>=6, then run the command rails webpacker:install before you proceed.
  • Test & Validate

    Now the plivo_controller is ready forward incoming SMS to your email, you can use the below command to initiate your Rails.

    $ rails server
    

    Your local development server will be started and you can test the rails app via the URL http://127.0.0.1:3000/plivo/email/.

    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 (3000 in this case):

    $ ./ngrok http 3000
    

    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://68b9f8c8da2b.ngrok.io/plivo/email/) 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.