Forwarding SMS Messages to Email Using Go

    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.

    Set up Your Go Dev Environment

    Once you have the above prerequisites set, You can follow the below instructions to create an app to forward incoming SMS to your email.

    Install Go

    You can install Go from the Official Installer.

    Create a project

    • Create a project directory, run the following command:

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

      $ cd mygoapp
      

    Forward SMS to Email

    Below is the code to Forward Incoming SMS to Email.

    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
    45
    46
    
    package main
    
    import (
      "fmt"
      "net/smtp"
      "net/http"
    )
    
    func handler(w http.ResponseWriter, r *http.Request) {
    	fromnumber := r.FormValue("From")
    	tonumber := r.FormValue("To")
    	text := r.FormValue("Text")
    	print("Message Received - ", fromnumber, " ", tonumber, " ", text)
    
      // Sender data.
      from := "<from@gmail.com>"
      password := "<email_password>"
    
      // Receiver email address.
      to := []string{
        "<sender@example.com>",
      }
    
      // smtp server configuration.
      smtpHost := "smtp.gmail.com"
      smtpPort := "587"
    
      // Message.
      message := []byte(text)
    
      // Authentication.
      auth := smtp.PlainAuth("", from, password, smtpHost)
    
      // Sending email.
      err := smtp.SendMail(smtpHost+":"+smtpPort, auth, from, to, message)
      if err != nil {
        fmt.Println(err)
        return
      }
      fmt.Println("Email Sent Successfully!")
    }
    
    func main() {
      http.HandleFunc("/email_sms/", handler)
    	http.ListenAndServe(":8080", nil)
    }
    

    Save this code in any file (name the file something like email.go). To run this file on the server, go to the folder where this file resides and use the following command:

    $ go run email.go
    

    And you should see your basic server app in action on http://localhost:8080/email/

    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.

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

    $ ./ngrok http 8080
    

    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://349f5c931df4.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.