Receive an MMS

    If you own an MMS-enabled phone number, your phone number is capable of receiving MMS messages from US and Canada long code phone numbers.

    Getting Started

    1. Sign up for a free Plivo trial account.
    2. Check out our server-side SDKs page and install the right helper based on the programming language you want to use.
    3. Buy a Plivo phone number. An MMS enabled phone number is required to receive MMS messages. You can buy a Plivo phone number in Buy Numbers tab on your Plivo account UI.
    4. Use a web hosting service to host your web application. There are many inexpensive cloud hosting providers that you can use for just a few dollars a month. Follow the instructions of your hosting provider to host your web application.
      Note: If you are using a Plivo Trial account for this example, you can only send MMS to phone numbers that have been verified with Plivo. Phone numbers can be verified at the Sandbox Numbers page.

    Set up a Web Server

    Let’s assume your web server is located at http://example.com. Below is a snippet to set up a route on your webserver. Now when we send an HTTP request to http://example.com/receive_mms this route will be invoked.

    Note: For PHP, the route will be example.com/receive_mms.php.

    1. Copy the relevant code below into a text file and save it. Let’s call it receive_mms.
      Note: Make sure to use the appropriate file extension for your code (e.g., receive_mms.py for Python).
    2. Next, you will now have to configure this URL in your Plivo application.

    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
    
    from flask import Flask, request, make_response, Response
    
    app = Flask(__name__)
    
    
    @app.route('/receive_mms/', methods=['GET', 'POST'])
    def inbound_sms():
        # Sender's phone number
        from_number = request.values.get('From')
        # Receiver's phone number - Plivo number
        to_number = request.values.get('To')
        # Print the message
        
        num_media = int(request.values.get('MediaCount'))
          
        print('Message received - From: %s, To: %s' %(from_number, to_number))
        
        if num_media:
          print('%d media received' % (num_media))
          for i in range(num_media):
            print('Media %d - %s' % (i + 1, request.values.get('Media%d' % (i))))
          
        return 'Message Recevived'
    
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', debug=True)
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    
    require "sinatra"
    
    post "/receive_mms/" do
      # Sender's phone number
      from_number = params[:From]
      # Receiver's phone number - Plivo number
      to_number = params[:To]
      # Print the message
      
      num_media = params[:MediaCount].to_i
      media_urls = []
      
      for i in 0..num_media
         media_urls.push(params["Media#{i}"])
        
      puts "Message received - From: #{from_number}, To: #{to_number}"
    
      if num_media > 0
        puts "#{num_media} media received"
        media_urls.each_with_index{|media_url, index|
          puts "Media #{index + 1} - #{media_url}"
        }
      end
    end
    
    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
    
    var express = require('express');
    var bodyParser = require('body-parser');
    var app = express();
    
    app.use(bodyParser.urlencoded({
        extended: true
    }));
    app.use(function (req, response, next) {
        response.contentType('application/xml');
        next();
    });
    app.set('port', (process.env.PORT || 5000));
    app.all('/receive_mms/', 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;
        // Number of media
        var num_media = request.body.MediaCount;
        //Print the message
        console.log('Message received - From: ' + from_number + ', To: ' + to_number + ');
        
        if(parseInt(num_media) > 0){
          console.log(num_media + 'received');
          for(i=0;i<parseInt(num_media);i++){
             console.log("Media "+(i+1)+" : "+request.body.Media[i]);
          }
         }
    });
    app.listen(app.get('port'), function () {
        console.log('Node app is running on port', app.get('port'));
    });
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    <?php
        require 'vendor/autoload.php';
        // Sender's phone numer
        $from_number = $_REQUEST["From"];
        // Receiver's phone number - Plivo number
        $to_number = $_REQUEST["To"];
        // Prints the message
        $num_media = (int)$_REQUEST["MediaCount"];
        for ($i = 0; $i < $num_media; $i++) {
          $mediaNum = $i + 1;
          $mediaURL = $_REQUEST["Media" .$i];
          echo("Media $mediaNum - $mediaURL");
        }
        echo("Message received - From $from_number, To: $to_number");
    ?>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    import static spark.Spark.*;
    
    public class ReceiveSms {
        public static void main(String[] args) {
            get("/receive_mms", (request, response) -> {
                // Sender's phone number
                String from_number = request.queryParams("From");
                // Receiver's phone number - Plivo number
                String to_number = request.queryParams("To");
                
                String mediaCount = request.queryParams("MediaCount");
                // Print the message
                System.out.println(from_number + " " + to_number);
    
                int numMedia = Integer.parseInt(mediaCount);
                
                for (int i = 0; i < numMedia; i++) {
                  System.out.println("Media " + (i + 1) + " - " + request.queryParams("Media" + i));
                }
                return "Message Received";
            });
        }
    }
    
    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
    
    package main
    
    import (
    	"net/http"
    	"strconv"
    
    	"github.com/go-martini/martini"
    )
    
    func main() {
    	m := martini.Classic()
    	m.Get("/receive_mms", func(w http.ResponseWriter, r *http.Request) string {
    		w.Header().Set("Content-Type", "application/xml")
    		fromnumber := r.FormValue("From")
    		tonumber := r.FormValue("To")
    
    		fmt.println("Message Received - ", fromnumber, " ", tonumber)
    		if num_media := strconv.Atoi(r.FormValue("MediaCount")); num_media > 0 {
    			for i := 0; i < num_media; i++ {
    				fmt.println("Media %d - %s", i+1, r.FormValue("Media%d", i))
    			}
    		}
    		return "Message Received"
    	})
    	m.Run()
    }
    
    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
    
    using Nancy;
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    
    namespace NancyStandalone
    {
        public class FunctionsModule : NancyModule
        {
            public FunctionsModule()
            {
                Post("/receive_mms", parameters =>
                {
                    // Sender's phone number
                    String from_number = Request.Form["From"];
                    // Receiver's phone number
                    String to_number = Request.Form["To"];
                    int mediaCount = Request.Form["MediaCount"];
                    if (mediaCount > 0)
                    {
    
                        for (int i = 0; i < mediaCount; i++)
                        {
                            Console.WriteLine("Media Url is {1}", Request.Form["Media" + i.ToString()]);
                        }
    
                    }
                    // Print the message
                    Console.WriteLine("Message received - From: {0}, To: {1}", from_number, to_number);
    
                    return "Message received";
                };
            }
        }
    }
    

    Create an Application

    1. Create an Application by visiting the Application Page and click on New Application or by using Plivo’s Application API.
    2. Give your application a name. Let’s call it Receive MMS. Enter your server URL (e.g. http://example.com/receive_mms) in the Message URL field and set the method as POST. See our Application API docs to learn how to modify your application through our APIs.
    3. Click on Create 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 this app.
    2. Select Receive MMS (name of the app) from the Plivo App dropdown list.
    3. Click on Update to save.

    Assign Phone Number to Receive MMS App

    If you don’t have a number, go to the Buy Number page to purchase a Plivo phone number.

    MMS Phone Number Search

    Test it out

    Send an MMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL with the parameters listed in the XML Request - Messages Documentation.