Send MMS Message with PHP

The sendmms.php script (sendmms.php) can be used to send an MMS message via NowSMS with PHP. MMS messages can obtain multimedia content objects including images, video, audio and vCard contacts.

Version 2 of this script adds support for SSL/TLS.

If you are migrating from the version 1 script, SSL/TLS requires a slight change in parameters. The first two parameters for the old version were host name or IP, followed by the port number. In the new version, instead these are combined into a single parameter, which is the URL of the NowSMS server (e.g., http://127.0.0.1:8800 or https://sample.smshosts.com/).

The Version 2 script can be downloaded at the following link: http://www.nowsms.com/download/sendmms-php.txt.

The sendmms.php script is considerably more complex than the sendsms.php script. The reason for this increased complexity is because an MMS message is more complex than an SMS message.

The first part of sendmms.php consists of PHP functions that you will call in your PHP script … namely MmsInit, MmsAddField, MmsAddFile and MmsSend. Include these functions in your PHP script “as is” without editing them.

After these functions, sendmms.php contains a simple example showing how to use these functions to send an MMS message through a NowSMS server.

1.) Begin by using MmsInit to initialise the MMS message structure.

$mmsMessage = MmsInit();

2.) Add header fields and attributes desired for your MMS message, calling the MmsAddField fuction.

$mmsMessage = MmsAddField ($mmsMessage, "PhoneNumber", "+447777777777");

$mmsMessage = MmsAddField ($mmsMessage, "MMSFrom", "+447777777777");

$mmsMessage = MmsAddField ($mmsMessage, "MMSSubject", "Subject of Message");

$mmsMessage = MmsAddField ($mmsMessage, "MMSText", "Hello!");

The “PhoneNumber” field specifies the recipient(s) for the MMS message. This can be a comma delimited list of phone numbers, or it can be the name of a NowSMS distribution list.

The “MMSFrom” field specfies the sender of the MMS message. Normally, this would be a phone number, short code or e-mail address. (If the message is sent via a modem, this will be ignored, but if you have multiple modems, this field can be used to select a particular modem.)

The “MMSSubject” field specifies the subject of the MMS message, and is optional.

The “MMSText” field to specifies some text to be included in the MMS message, and is also optional. Text can also be included in an MMS message as a text file reference.

3.) Specify the files (usually images) to include in the MMS message. Files can be added using a local directory path or remote URL. For a local directory path, use MmsAddFile.

The last parameter of MmsAddFile is the MIME content type (e.g., “image/gif”, “image/jpeg”, “image/png”, “text/plain” or “application/smil”). However, note that current versions of NowSMS ignore the MIME content type when messages are submitted via the interface used by this PHP script. Instead, NowSMS uses the file extension to determine the content type (e.g., “.gif”, “.jpg”, “.png”, “.txt”, “.smil”.

$mmsMessage = MmsAddFile ($mmsMessage, "f:/temp/file.gif", "image/gif");

An MMS message can contain one or more of these file objects.

When using MmsAddFile, the files referenced in the PHP script must be local files, residing on the same server as the PHP script.

To include a file (image) by URL reference, instead of a local file, use MmsAddField with field name MMSFile:

$mmsMessage = MmsAddField ($mmsMessage, "MMSFile", "https://www.nowsms.com/nowsmsicon.png");

4.) Use MmsSend to submit the MMS message to your NowSMS Server.

$nowsmsHostURL  = "https://sample.smshosts.com";    // URL of NowSMS Server

$nowsmsUsername = "username";                       // "SMS Users" account name 

$nowsmsPassword = "password";                       // "SMS Users" account password

 

$x = MmsSend ($nowsmsHostURL, $nowsmsUsername, $nowsmsPassword, $mmsMessage);

The MmsAddField function can be used to specify any NowSMS URL parameter that is valid for sending an MMS message.

The Version 2 script can be downloaded at the following link: http://www.nowsms.com/download/sendmms-php.txt.

<?php

 

/* MmsInit - Create an MMS message object */

function MmsInit () {

 

   $data = "";

   return $data;

}

 

 

/* MmsAddFile - Add a local file to an MMS message object */

function MmsAddFile ($data, $file, $contenttype)

{

 

   $fa = @file($file);

   $xf ="Content-Type: ".$contenttype."\r\n\r\n".implode("",$fa);

   $data["MMSFile\"; filename=\"$file"]   = $xf;

 

   return $data;

}

 

/* MmsAddField - Add a name/value parameter to an MMS message object */

function MmsAddField ($data, $fieldname, $fieldvalue) {

 

   $data[$fieldname] = "\r\n" . $fieldvalue;

 

   return $data;

 

}

 

/* MmsSend - Send an MMS message via a NowSMS server.

             Uses HTTP POST multipart/form-data */

 

function MmsSend ($nowsmsURL, $username, $password, $data_to_send)

{

 

    $https = false;

    $port = 80; // default port

 

    // parse http:// or https:// prefix

    $parts = explode("://", $nowsmsURL);

    if (count($parts) > 1) {

       if ($parts[0] == "https") {

          $https = true;

          $port = "443";

       }

       $host = $parts[1];

    }

    else $host = $nowsmsURL;

 

    // parse port number if present

    $parts = explode(":", $host);

    if (count($parts) > 1) {

       $host = $parts[0];

       $port = $parts[1];

    }

 

    // remove trailing slash

    $parts = explode("/", $host);

    if (count($parts) > 1) {

       $host = $parts[0];

    }

 

    // Add stream protocol prefix

    if ($https) $streamHost = "tls://" . $host . ":" . $port;

    else $streamHost = "tcp://" . $host . ":" . $port;

 

    // Turn off SSL certificate check

    // TODO: Turn back on

    if ($https) {

       $context = stream_context_create([

          'ssl' => [

              'verify_peer' => false

          ]

       ]);

    }

    else $context = stream_context_create([]);

 

    $mimeFiller = "This is a MIME message\r\n\r\n";

    $dc = strlen($mimeFiller);

    $bo ="-----------------------------mime-boundary-marker";

 

    $fp = stream_socket_client($streamHost, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);

    if (!$fp) {

        echo "errno: $errno \n";

        echo "errstr: $errstr\n";

        return "ERROR " . $errno . " - " . $errstr;

    }

 

    fputs($fp, "POST / HTTP/1.0\r\n");

    if ($username != "") {

       $auth = $username . ":" . $password;

       $auth = base64_encode($auth);

       fwrite($fp, "Authorization: Basic " . $auth . "\r\n");

    }

    fputs($fp, "User-Agent: NowSMS PHP Script\r\n");

    fputs($fp, "Host: $host\r\n");

    fputs($fp, "Accept: */*\r\n");

    fputs($fp, "Content-type: multipart/form-data; boundary=$bo\r\n");

 

    foreach($data_to_send as $key=>$val) {

        $ds =sprintf("%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n%s\r\n",$bo,$key,$val);

        $dc += strlen($ds);

 

    }

    $dc += strlen($bo)+3;

    fputs($fp, "Content-length: $dc\r\n");

    fputs($fp, "\r\n");

    fputs($fp, $mimeFiller);

 

    foreach($data_to_send as $key=>$val) {

        $ds =sprintf("%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n%s\r\n",$bo,$key,$val);

        fputs($fp, $ds );

    }

    $ds = $bo."--\r\n" ;

    fputs($fp, $ds);

 

    $res = "";

 

    while(!feof($fp)) {

        $res .= fread($fp,1);

    }

    fclose($fp);

 

 

    return $res;

}

 

// EXAMPLE usage of above functions

 

// Initialise the MMS Message object wth MmsInit

$mmsMessage = MmsInit();

 

// Set MMS message fields with MmsAddField

// "PhoneNumber" is the recipient, and can be a comma deilmited list of recipients or the name of a NowSMS distribution list 

// "MMSFrom" is the sender - ignored by modems

// "MMSSubject" is the optional subject 

// "MMSText" is an optional text part of the message.  Text parts can also be added as file references 

 

$mmsMessage = MmsAddField ($mmsMessage, "PhoneNumber", "+447777777777");

$mmsMessage = MmsAddField ($mmsMessage, "MMSFrom", "+447777777777");

$mmsMessage = MmsAddField ($mmsMessage, "MMSSubject", "Subject of Message");

$mmsMessage = MmsAddField ($mmsMessage, "MMSText", "Hello!");

 

// Files (usually images) can be added using a local directory path or remote URL.

//

// For a local directory path, use MmsAddFile.

// The last parameter is the MIME content type, e.g., "image/gif", "image/jpeg", "image/png", "text/plain" or "application/smil" ...

// however, note that current versions of NowSMS ignore the MIME content type when messages are submitted via the interface

// used by this PHP script. 

// Instead, NowSMS uses the file extension to determine the content type (e.g., ".gif", ".jpg", ".png", ".txt", ".smil"

 

 

// $mmsMessage = MmsAddFile ($mmsMessage, "f:\\temp\\file.gif", "image/gif");

 

// To include a file (image) by URL reference, use MmsAddField with field name MMSFile 

 

$mmsMessage = MmsAddField ($mmsMessage, "MMSFile", "https://www.nowsms.com/nowsmsicon.png");

 

// Set parameters for connecting to the NowSMS server 

$nowsmsHostURL  = "https://sample.smshosts.com";    // URL of NowSMS Server

$nowsmsUsername = "username";                       // "SMS Users" account name 

$nowsmsPassword = "password";                       // "SMS Users" account password

 

// Use MmsSend to send the message.

// The HTTP response from the server is returned by this function, and this example echoes it

 

$x = MmsSend ($nowsmsHostURL, $nowsmsUsername, $nowsmsPassword, $mmsMessage);

echo $x;

 

 

?>

The MmsAddField function can be used to specify any NowSMS URL parameter that is valid for sending an MMS message.

For example … here is an incomplete list of additional parameter fields that can be specfied using the MmsAddField function.

“MMSFile” – As noted earlier, this script attaches local files to the MMS message. However, what if you want to include files that reside on a separate web server instead? In that case, do not use the MmsAddFile function. Instead, use $mmsMessage = MmsAddField ($mmsMessage, “MMSFile”, “http://www.nowsms.com/media/logo.png” ); and specify the file components as URL references via the “MMSFile” parameter field.

“MMSDeliveryReport” – “Delivery Report” specifies whether or not a delivery report is requested for the message. Set to “Yes” to request a delivery report. Note that any delivery report would be directed back to the phone number or e-mail address specified in the “MMSFrom” address.

“MMSReadReport” – “Read Report” specifies whether or not a read receipt is requested for the message. Note that the receiving client may choose not to send a read receipt. Any read receipt report would be directed back to the phone number or e-mail address specified in the “MMSFrom” address.

“MMSPriority” – “Priority” is a user defined priority to be associated with the message. Generally, any priority definition associated with the message is ignored by the underlying transport, but the receiving client may decide to display messages differently based upon this priority setting.

“MMSMessageClass” – “Message Class” is an attribute defined in the MMS specifications. “Personal” is the message type that is used for standard user-to-user communications. Other defined message classes that are supported by this parameter include: “Informational” and “Advertisement”.

“MMSWAPPush” – Set to “Yes” to indicate that the message being sent should be sent as an “Multimedia Content Push” message via WAP Push instead of as an MMS message.

“MMSSMSPush” – Set to “Yes” to indicate that the message being sent should be sent as an “Multimedia Content Push” message via a URL link in a text SMS message instead of as an MMS message.

It is also possible to specify forward locking and DRM constraints to be applied against the content of the MMS message. Forward locking and DRM constraints apply to non-text parts of the MMS message (i.e., in a forward locked message, text could still be forwarded, but images or video could not). Please note that not all devices support forward locking and DRM constraints, therefore use these parameter settings only after testing thoroughly with mobile phones used by your message recipients.

“MMSForwardLock” – Forward locking is the most basic level of DRM (Digital Rights Management). When “Forward Lock” is set to “Yes”, this indicates that the receiving device should not allow any non-text objects in the message to be forwarded off of the device. The device may allow the user to extract pictures, videos or sounds from the message and save them on the phone. However, any such objects remain forward locked, such that they cannot be forwarded to another user or transferred to another device. (IMPORTANT NOTE: NOT ALL DEVICES SUPPORT FORWARD LOCK, WHEN NOT SUPPORTED THE CONTENT WILL APPEAR AS GARBAGE OR MAY BE REJECTED BY THE OPERATOR MMSC.)

“DRMRestrict” – Beyond forward locking, More advanced DRM (Digital Rights Management) restrictions can be applied to limit the number of times that the user can access an object, or start and end dates can be specified to limit how long the user can access an object.

These advanced DRM restrictions can be applied by setting “DRMRestrict” to “Yes”. When this setting is enabled, forward lock is also implied, and the value of the “MMSForwardLock” setting is ignored. (IMPORTANT NOTE: NOT ALL DEVICES SUPPORT DRM RESTRICTIONS, WHEN NOT SUPPORTED THE CONTENT WILL APPEAR AS GARBAGE OR MAY BE REJECTED BY THE OPERATOR MMSC.)

“DRMRestrictTextXML”“Yes” specifies that the rights object should be encoded in text XML format. “No” specifies that the rights object should be encoded in binary XML format. The default is “No”.

When DRM Restrictions are specified, it is generally necessary to specify one or more DRM Permissions and one or more DRM Constraints regarding the MMS message content.

DRM Permissions specify what types of access are allowed against the objects in a message that is protected with DRM.

For example, an audio or video object requires “Play” permission before the user can access it. An image requires “Display” permission before the user can access it, and it requires “Print” permission if the user is to be allowed to print the image to a printer , perhaps over Bluetooth. An application requires “Execute” permission before the user can make use of the application. In all cases, the forward locking is assumed, so that the user is not allowed to forward or transfer the object from the device.

If you are sending multiple types of objects in the MMS message, specify all permissions that are required for the different types of objects that are being sent.

“DRMPermissionPlay” – Set to “Yes” to enable DRM “Play” Permission.

“DRMPermissionDisplay” – Set to “Yes” to enable DRM “Display” Permission.

“DRMPermissionExecute” – Set to “Yes” to enable DRM “Execute” Permission.

“DRMPermissionPrint” – Set to “Yes” to enable DRM “Print” Permission.

DRM Constraints specify constraints with regard to how long a DRM protected object object should remain accessible to the user.

“DRMConstraintCount” – “# of Accesses (count)” specifies the the user can only access the DRM protected object this number of times before access is no longer allowed.

“DRMConstraintStart” – “Start Date (yyyy-mm-dd)” specifies that the user will not be allowed to access the DRM protected object until on or after the specified date. (Note that you must specify the date in yyyy-mm-dd format, e.g., 2008-12-24.)

“DRMConstraintEnd” – “End Date (yyyy-mm-dd)” specifies that the user will not be allowed to access the DRM protected object after the specified date. (Note that you must specify the date in yyyy-mm-dd format, e.g., 2008-02-24.)

“DRMConstraintInterval” – “# of Days (interval)” specifies that the user will be allowed to access the DRM protected object for this number of days after initial receipt of the object. The user can either enter a number of days here, or they can enter any valid value defined for the “” element in the OMA DRM Rights Expression Language specification. For example, P2Y10M15DT10H30M20S represents a duration of 2 years, 10 months, 15 days, 10 hours, 30 minutes and 20 seconds.

 

System Requirements for using this script:

This script connects to a NowSMS server and posts a request to the NowSMS server to send a message.

If you do not have a NowSMS server installed, this script will not work.

NowSMS server software is installed on a Windows PC and to be able to send SMS or MMS messages, NowSMS also requires a GSM modem, Android phone, or SMS service provider connection.

Free 30-day trial versions of the NowSMS software can be downloaded at http://www.nowsms.com/download-free-trial

Either the Now SMS/MMS Gateway or NowSMS Lite can be used. NowSMS Lite can send SMS and MMS messages using a single GSM modem or Android phone as a modem connection. The full product supports multiple modems and/or service provider connections.

Before attempting to interface with NowSMS using scripts, you should verify that NowSMS is configured correctly and can send SMS messages using its built in web interface. For Android phone or GSM modem connections, there are quick start guides that can be found here: http://www.nowsms.com/doc/quick-start-guide

Once you have verified that NowSMS is working on its own, it will be necessary to change IP addresses referenced in the script to point to your NowSMS server. Our sample scripts use an IP address of 127.0.0.1, which is a special loopback address for the current PC. If your script is running on a different system than the NowSMS server, change 127.0.0.1 to a host name or IP address that is valid for your environment. It is also necessary to define an SMS user account on the NowSMS server with account credentials that match with the user account in the script.