Use linux to send SMS & MMS

It has been a while since I wrote a post on this blog. The pain is over, with this new blogpost on how to send SMS and receive MMS with linux. I didn’t find too much information about the process, that’s why I decided to write on this topic.

What you’ll need

First, you need a modem that will work with the softwares we’ll use. In my quest to find something up-to-date I first bought a Huawei E3372 4G stick. The web interface was really cool and can be used to send SMS. There is an API that you can use in order to get and send SMS from the stick. But I wasn’t able to receive MMS with this stock, so I searched for another hardware.

I found this OSTENT Quadriband Modem, which was well recognized by my linux computer. That’s what I needed !

I installed two softwares that’ll work together :

  • SMSTOOLS : this software is used to send and receive SMS. Since a MMS is no more than a SMS with a link included to download the content, SMSTOOLS will do most of the job.
  • WVDIAL : this one enables me to create a connection via 3G and download the content of the MMS.

/etc/wvdial.conf

I first ran wvdialconf that generated a first version of /etc/wvdial.conf

Here is the content of my conf file after trying so many times in order to make it working for FreeMobile in France

[Dialer Free]
Modem type = Analog Modem
Modem = /dev/ttyUSB1
Baud = 115200

Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,"IP","free"

Phone = *99***1#
Username = free
Password = free
Stupid mode = 1
New PPPD = 1
Check Def Route = 1

/etc/smsd.conf

Here is the content of smsd.conf which is used by smstools

devices = GSM1
logfile = /var/log/smsd.log
failed = /var/spool/sms/failed
outgoing = /var/spool/sms/outgoing
checked = /var/spool/sms/checked
incoming = /var/spool/sms/incoming
loglevel = 5
delaytime = 6
errorsleeptime = 12
blocktime = 180
autosplit = 3
receive_before_send = no

[GSM1]
device = /dev/ttyUSB1
incoming = yes
#pin = 0000
baudrate = 115200

The modem’s baudrate has been calculated with wvdialconf

Receiving & Sending SMS

SMS that are coming will be located in /var/spool/sms/incoming and you can write a small file to send a SMS in /var/spool/sms/outgoing.

The content of the file should be :

To: 33606060606
Here is my first SMS with SMSTools !

Here is the content of an incoming SMS

From: 3360606060606
From_TOA: 91 international, ISDN/telephone
From_SMSC: 33606060606
Sent: 21-08-18 18:36:38
Received: 21-08-18 18:38:50
Subject: GSM1
Modem: GSM1
IMSI: 000000000000000
IMEI: 000000000000000
Report: no
Alphabet: ISO
Length: 13

Hello World !

You can easily parse the content of the message to get the sender’s number and the content of the message.

Receiving MMS

In order to read a MMS, here are the steps I use :

  1. Get the content of the SMS, you’ll find a link in the message to download the MMS content. You’ll need to download the link with you mobile connection.
  2. Stop smstools & launch wvdial to download the link
  3. When the download is complete, analyse the content with a decoder (this is multipart message, same as emails)
  4. Stop wvdial and relaunch smstools.

Here is a small part of my code in PHP to download the MMS :

        // We'll find the link to download the MMS in $content
        preg_match( "/http[A-Za-z0-9:\/.\?\-_]*/", $content, $result_link );
        
        if (count($result_link) === 0 || $result_link == FALSE )
        {
            print("There is no link in your SMS");
            exit();
        }

        $mms_link = $result_link[0];

        // We stop SMSTOOLS
        exec("service sms3 stop");

        // We launch wvdial to get a connection via the MODEM
        $wvdial = proc_open('wvdial Free', array(array("pipe","r"), array("pipe","w"), array("pipe","w")), $pipes);

        // We need to see ppp0 up (the interface is visible in ifconfig when wvdial is launched)
        $is_down = true; // Etat de l'interface (false = up, true = down)
        $try     = 0; // Nombre de vérification de l'etat de l'interface
        
        while($is_down & $try <= 6)
        {
            exec('ifconfig ppp0', $output, $code);

            if ($code === 0)
            {
                $is_down = false;
                sleep(5); // We'll wait 5 seconds to be sure that wvdial is running well (dns, gateway ...)
                break;
            }
            else
            {
                $try++;
            }

            sleep(10); // We'll make another try in 10 seconds
        }

        if ($is_down === true)
        {
            print("Impossible to launch ppp0");
            exit();
        }

        // Get the SMIL file (MMS Multipart like EMAIL)
        ob_start();
        passthru('curl --interface ppp0 '.$mms_link);
        $smil_file = ob_get_contents();
        ob_end_clean();

        // SMIL downloaded, we can stop wvdial
        proc_terminate($wvdial);
        exec("killall wvdial"); // Just to be sure it has ended
        exec("service sms3 start");

        // You can now analyse the content of $smil_file with a decoder

Business usage

I made an app that is used by our employees to discuss with our clients.

Ce contenu a été publié dans Technologie. Vous pouvez le mettre en favoris avec ce permalien.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *