Sending MMS via PHP

Sending MMS via PHP SearchSearch
Author Message
Robin Cramp
New member
Username: Robin

Post Number: 1
Registered: 04-2007
Posted on Tuesday, April 03, 2007 - 01:45 pm:   

Hi All

I have been working towards being able to send an MMS vis NowSMS, and have the script provided located here:
http://support.nowsms.com/discus/messages/1/1113.ht ml

<?

function PostToHost($host, $port, $username, $password, $data_to_send)
{
$dc = 0;
$bo ="-----------------------------305242850528394";

$fp = fsockopen($host, $port, $errno, $errstr);
if (!$fp) {
echo "errno: $errno \n";
echo "errstr: $errstr\n";
return $result;
}

fputs($fp, "POST / HTTP/1.1\r\n");
if ($username != "") {
$auth = $username . ":" . $password;
echo "auth: $auth\n";
$auth = base64_encode($auth);
echo "auth: $auth\n";
fwrite($fp, "Authorization: Basic " . $auth . "\r\n");
}
fputs($fp, "User-Agent: NowSMS PHP Script\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, "This is a MIME message\r\n\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);
fputs($fp, $ds );
}
$ds = $bo."--\r\n" ;
fputs($fp, $ds);

$res = "";

while(!feof($fp)) {
$res .= fread($fp,1);
}
fclose($fp);


return $res;
}



$file1="C:\TEMP\logo.gif";
$contenttype1 = "image/gif";

$fa = @file($file1);
$xf ="Content-Type: ".$contenttype1."\r\n\r\n".implode("",$fa);
$data["MMSFile\"; filename=\"$file1"] = $xf;

$file2="C:\TEMP\test.smil";
$contenttype2 = "application/smil";

$fa = @file($file2);
$xf="Content-Type: ".$contenttype2."\r\n\r\n".implode("",$fa);
$data["MMSFile\"; filename=\"$file2"] = $xf;

/* Repeat this sequence for additional parts
$file3="C:\TEMP\?????.???";
$contenttype3 = "?????";

$fa = @file($file3);
$xf="Content-Type: ".$contenttype3."\r\n\r\n".implode("",$fa);
$data["MMSFile\"; filename=\"$file3"] = $xf;
*/


/* A quirk of this script is that you must start all non-file parameters with \r\n */
$data["MMSFrom"] = "\r\n" . "sender@domain.com";
$data["PhoneNumber"] = "\r\n" . "+44999999999";
$data["MMSSubject"] = "\r\n" . "Subject of message" ;

/* The MMSText field is optional */
$data["MMSText" ] = "\r\n" . "Hello!" ;



$x = PostToHost("127.0.0.1", 81, "test", "test", $data);
echo $x;








?>
sendmms.php

The problem I seem to be having is knowing how each of the different parts of the script works, ie the parts that require changing by me to make it operational.

Could anyone assist?

Robin

Bryce Norwood - NowSMS Support
Board Administrator
Username: Bryce

Post Number: 7029
Registered: 10-2002
Posted on Wednesday, April 04, 2007 - 07:47 pm:   

Hi Robin,

We should probably revisit this script one of these days, as I believe that there may be easier ways to accomplish this in PHP today.

But here's a basic explanation of that script ...

You don't want to touch the "PostToHost" function. That function runs from the start of the file thru the "}" after "return $res" which terminates the function.

The next part is outside of the function, so it begins the code that the script starts executing when it is run.


quote:

$file1="C:\\TEMP\\logo.gif";
$contenttype1 = "image/gif";

$fa = @file($file1);
$xf ="Content-Type: ".$contenttype1."\r\n\r\n".implode("",$fa);
$data["MMSFile\"; filename=\"$file1"] = $xf;




The first step is define all of the content files that will be included in the MMS. So you would modify the value of "$file1" (i.e., "c:\temp\logo.gif") and "$contentype1" (i.e., "image/gif") parameters.

In this example, 2 files are included in the MMS message, and a third is included but commented out.

For each file, the variable names for "$file1" and "$contenttype1" are changed ... becoming "$file2" and "$contenttype2" ... etc. However, I don't believe that is necessary. You should be able to repeat a block here, change the file name and content type values, but not the variables themselves.


quote:

/* A quirk of this script is that you must start all non-file parameters with \r\n */
$data["MMSFrom"] = "\r\n" . "sender@domain.com";
$data["PhoneNumber"] = "\r\n" . "+44999999999";
$data["MMSSubject"] = "\r\n" . "Subject of message" ;

/* The MMSText field is optional */
$data["MMSText" ] = "\r\n" . "Hello!" ;




The next part sets the value of the message sender (MMSFrom), recipient (PhoneNumber) and subject (MMSSubject). Note that the recipient can be a comma delimited list of multiple phone numbers.

The MMSText field is optional, it just provides an easy way to include a text part into the message.


quote:

$x = PostToHost("127.0.0.1", 81, "test", "test", $data);




The bit that then calls the PostToHost function is something you would need to change.

The first parameter is the IP address or host name of the PC running NowSMS. In this case, it is set to "127.0.0.1", meaning the local PC itself.

The second parameter is the port number of the web interface of NowSMS. The default configuration of NowSMS would use 8800, but the person who wrote this script was using 81.

The third and fourth parameters are a username and password for connecting to NowSMS. These would relate to a user account defined in the "SMS Users" list.

The fifth parameter, $data, should not be changed.

Hope that helps!

-bn
Robin Cramp
New member
Username: Robin

Post Number: 2
Registered: 04-2007
Posted on Wednesday, April 04, 2007 - 08:30 pm:   

Thanks Bryce for explaining the script.

If I can open up my thoughts a little further to see if you can understand where I intend to go with this.

I'm intending to send MMS content requested by SMS by the phone user.
They send their text to defined keywords sent to short codes; these are set up and working via another platform.
Once the SMS is sent, it forwards a URL request to a PHP script that is containing all the necessary information to complete the MMS request.

My question is would this be the script we have be discussing previously be able to include these parameters? and if so I'm miffed as how to forward the users number into the script so that NowSMS can send the content via the Gateway.

I have the phone users parameters that forward with the URL, these are defined as so:


Forwarded to http://domain.name.the.php with parameters:
source=447777777777
dest=short code number
dtime=200704030049
message=Mobile yes
network=33
message_id=39961218906
message_after_match=
matched_keyword=Mobile
matched_subkeyword=yes


Would you be able to see if this is viable to link together, or may be the script is not suitable for this purpose?

Many Thanks

Robin