Mobile Me(dia)

Shawn Van Every Shawn.Van.Every@nyu.edu
Spring 2009
H79.2690.1

Week 2 - SMS/MMS to Email, PHP/MySQL and Mobile Web

MMS

MMS or Multimedia Messaging Service is a protocol for sending messages to and from mobile phones that include multimedia content. This can be audio, text, video, images and so on.

Although MMS isn't as popular as SMS and people aren't as comfortable with it, it does provide an easy way for us to start experimenting with media created on the mobile device. As with SMS, the US mobile carriers typically provide MMS to email gateways so that we can start prototyping applications that allow people to send multimedia to us.

More about MMS: Wikipedia: Multimedia Messaging Service

Expanding the PHP script for Multimedia

The parseMailScript that we started working with last week has been updated to allow it to receive and save multimedia attachments that the carriers send through. Those attachments are saved onto the server that runs the script.

Download it here: PHP Mail Popper

There are a couple of additional things in the script that we didn't get to last week. The first is that the script is setup to recieve messages only from certain domains. In the script there is a partial list of domains that the carriers themselves use for the mail gateways, you can expand the list to add in domains that you want to be able to recieve content from. Careful though, the reason that this is limited is due to spam issues.

Here is the existing code:
	// A list of domains that are allowed, put your own domain in this so you can post via email (iphone and other)
	$allowed_domains = array("mms.att.net","mobile.att.net","cingularme.com","messaging.sprintpcs.com","tmomail.net","vtext.com","mmode.com","alltel.net","vzwpix.com","mms.mycingular.com","nyu.edu","walking-productions.com","gmail.com"); 		
		
To add a domain, you would simply add to this list:
	// A list of domains that are allowed, put your own domain in this so you can post via email (iphone and other)
	$allowed_domains = array("mms.att.net","mobile.att.net","cingularme.com","messaging.sprintpcs.com","tmomail.net","vtext.com","mmode.com","alltel.net","vzwpix.com","mms.mycingular.com","nyu.edu","walking-productions.com","gmail.com","myadditionaldomain.com"); 
		
The next part that we didn't get to is a list of attachments that the carriers might send through in an MMS message that we don't want saved. For instance, T-Mobile sends a T-Mobile graphic with each message. To tell the parser not to save those attachments, add them to this list:
	// A list of attachment filename regular expressions that you don't want included in the output of the script
	$bad_attachments = array("masthead.jpg","dottedLine_600.gif","spacer.gif","video.gif","dottedLine_350.gif"); 
		
Related to this, there is a list of text that we don't want saved. This list is in the form of a regular expression. If you don't know regular expressions, have no fear, just substitue ".*" for whitespace:
	// A list of text regular expressions that you don't want included
	$bad_text = array("nothingatall",
		"PIX.*FLIX.*Messaging",
		"To.*learn.*how.*you.*can.*snap.*pictures.*with.*your.*wireless.*phone",
		"To.*learn.*how.*you.*can",
		"www\.verizonwireless\.com",
		"To.*play.*video.*messages.*sent.*to.*email",						"process.*when.*asked.*to.*choose.*an.*installation.*type.*Minimum.*Recommended.*or.*Custom.*select",
		"If.*you.*can.*read.*this.*text",
		"^T-Mobile\$",
		"If.*you.*are.*having.*trouble.*playing.*this.*attachment",
		"This.*Video.*Message.*was.*sent.*from.*a.*T-Mobile.*video.*phone",
		"\.footer.*{",
		"font-family:.*Arial,.*;",
		"font-size.*;",
		"color:.*;",
		"text-decoration:.*;",
		"normal.*{",
		"This message was sent using service from Verizon Wireless!",
		"visit /getitnow/getflix.",
		", QuickTime 6.5 or higher is required. Visit www.apple.com/quicktime/download to download the free player or upgrade your existing QuickTime Player. Note: During the download",
		"Minimum for faster download."
	); 
		

Automatically Running the script

*nix servers, like itp's server typically have a utility called cron. Cron gives us the means to run script or applications automatically based on time.

To run the parseMailScript.php automatically, you would first create a text file on your machine with the following lines:
MAILTO="your@email.address"
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/php /path/to/parseMailScript.php

		
This tells the cron application to run the PHP script parseMailScript.php every 5 minutes (:00, :05, :10 ..) every hour, of every day, of every month on every day of the week (each of the "*"s means "every").

The first line, the line starting with MAILTO means to send any output generated by the script to your email address. This means that if you leave $print_output = true that you will get an email every 5 minutes forever. I would suggest that when you are done testing that you turn off $print_output by setting it to "false".

To activate this cron script, you would first make sure that it is saved as a "unix" file with "unix" line breaks. Most text editors such as TextWrangler and TextPad have the capability built in.

You would then upload it to the server that is running the parseMailScript.php, log in via SSH and issue the following command on the command line:
		crontab name_of_cron_script
		
if you then issue the "crontab -l" command you should see that the contents of the script are now set.

Any time you want to update the cron, just edit the file and issue the crontab command again.

PHP 101

PHP is a very useful language for doing many types of development. Primarily it is used for web development (hence the name, Hypertext Preprocessor) but it is certainly not limited. For our purposes we will be using PHP as both a web development tool and a command line scripting tool (such as how we are using it with the "parseMailScript").

Basic HTML

<html> <!-- Required for all HTML pages -->
	<head> 
		<title>The Top Bar Title</title>
	</head>
	<body>
		Some information
		<!-- A Comment Tag -->
	</body>
</html> <!-- Closing tag, required -->
		
Example 1

Basic HTML with PHP
PHP = Hypertext Preprocessor

<html> <!-- Required for all HTML pages -->
        <head>
                <title>The Top Bar Title</title>
        </head>
        <body>
                Some information
		<br> <!-- A line break -->
                <!-- A Comment Tag -->
		<?  // Denotes the start of PHP processing
        		echo "The Date and Time is: ";  // print a string, end with a semicolon
        		$mydata = "July 29, 2004"; // Assign a variable 
        		echo $mydata; // print the string variable
		?> 

        </body>
</html> <!-- Closing tag, required -->
		
Example 2
Take note of the different comment styles.
The "//" style within PHP and "<--" outside of PHP (normal HTML)
Variables begin with "$".
"=" are assignment.
"echo" is the print function.
";" ends line of code.
PHP files are saved with ".php" not ".html"

Creating a PHP file for use on the command line

PHP can be developed in any text editor and doesn't require any compilation. To start writing a PHP script for use on the command line we need to start our file with the "shebang" and the path to the PHP executable. When using PHP as a web development tool this is not nessecary.

#!/usr/bin/php
By default, our version of php outputs HTTP headers so we need to turn that off by passing the "-q" flag to the php executable.
#!/usr/bin/php -q
Following the "shebang" line, we need to use <?PHP to tell PHP that we want to it to start interpreting code (PHP is meant to be mixed with HTML and therefore it will not interpret any code that is outside of <?PHP and ?>)

		#!/usr/bin/php -q
		<?PHP
		
		// Put Your Code Here
		
		?>
		
As is evident in the above example, "//" is used to make single line comments and "/* ... */" is used to make multiple line comments.

Let's make a PHP file that outputs "Hello World!";
		#!/usr/bin/php -q
		<?PHP
		
		echo("Hello World!\n");
		
		?>
		
As you can see, PHP is very similar to many other languages. There are a bunch of built-in functions (such as echo) and arguments are passed into functions through paranthesis. Strings are put into single or double quotes and the newline char is the \n. Lines of code are ended with semi-colons.

To execute this application, save it as something.php and upload it to a server (social is probably a good place).

You can execute it by SSHing onto itp's server and issuing the following command:

		php php101_1.php
		
You can skip the beginning "php" if you make the file executable:

		chmod 755 php101_1.php
		./php101_1.php
		
More Information:
  • PHP: Basic Syntax - Manual


  • Variables

    Variables in PHP are not strictly typed, meaning that you do not have to differentiate between strings, integers and so on. They start with a "$".

    		<?PHP
    			$myString = "hello world\n";
    			echo($myString);
    			$myInteger = 1003;
    			echo($myInteger . "\n");
    			$somethingelse = false;
    			echo($somethingelse . "\n");
    		?>
    		
    More Information:
  • PHP: Types - Manual
  • PHP: Variables - Manual
  • PHP: Constants - Manual


  • Mathematical Operators

    		<?PHP
    			$aValue = 0;
    			$aValue++;
    			echo("aValue now = " . $aValue . "\n");
    			$aValue = $aValue + $aValue;
    			echo("aValue now = " . $aValue . "\n");
    			// % + - * / ++ -- and so on, same as in Processing/Java
    		?>
    		
    More Information:
  • PHP: Expressions - Manual
  • PHP: Operators - Manual


  • Control Structures, Logical Operators and Loops

    		<?PHP
    		
    			// If Statement
    			$aValue = 0;
    			if ($aValue == 0)
    			{
    				echo("aValue is 0");
    			}
    			else if ($aValue == 1)
    			{
    				echo("aValue is 1");
    			}
    			else if ($aValue >= 2)
    			{
    				echo("aValue is greater than or equal to 2");
    			}
    			else
    			{
    				echo("aValue something else");
    			}
    			echo("\n");
    			// Other Logical Operators ==, >, <, >=, <=, ||, &&
    
    		
    			// For loop
    			for ($i = 0; $i < 10; $i++)
    			{
    				echo("\$i = $i\n");
    			}
    			
    			// Also While
    		?>
    		
    More Information:
  • PHP: Control Structures - Manual


  • Arrays and Loops

    		<?PHP
    			// Pretty normal array
    			$anArray = array();
    			$anArray[0] = "Something";
    			$anArray[1] = "Something Else";
    			for ($i = 0; $i < sizeof($anArray); $i++)
    			{
    				echo($anArray[$i] . "\n");
    			}
    			
    			// Key Value Map
    			$anotherA = array("somekey" => "somevalue", "someotherkey" => "someothervalue");
    			$keys = array_keys($anotherA);
    			$values = array_values($anotherA);
    			for ($i = 0; $i < sizeof($keys); $i++)
    			{
    				echo($keys[$i] . " = " . $values[$i] . "\n");
    			}
    		?>
    		
    More Information:
  • PHP: Arrays - Manual


  • Functions

    		<?PHP
    			
    			function myFunction($somearg)
    			{
    				// You would do something here
    				return "You passed in $somearg";
    			}
    			
    			$passing_in = "Hello World";
    			$return_value = myFunction($passing_in);
    			echo($return_value);
    			
    			echo("\n");
    		?>
    		
    More Information:
  • PHP: Functions - Manual


  • Classes and Objects

    		<?PHP
    			class MyClass
    			{
    				var $myClassVar;
    
    				function set_var($new_var)
    				{
    						$this->myClassVar = $new_var;
    				}
    
    				function get_var()
    				{
    						return $this->myClassVar;
    				}
    			}
    	
    			$aClass = new MyClass;
    			$aClass->set_var("something");
    			echo("Var: " . $aClass->get_var() . "\n");		
    		?>
    		
    Classes and Objects in PHP are very similar to Java/Processing. Syntactically though they don't use the "." operator, instead using "->". Also, in the class definition you need to use the "this" keyword.

    More Information:
  • PHP: Classes and Objects - Manual


  • Some Interesting Functions

    isset()
    		<?PHP
    			$somevar = NULL;
    			if (isset($somevar))
    			{
    				echo("somevar is set");	
    			}
    			else
    			{
    				echo("somevar is NOT set");
    			}
    		?>
    		
    mail
    			$mailmsg = "A Mail Message";
    			$mailsubject = "A Mail Subject";
    			$mailfrom = "vanevery@walking-productions.com";
    			$mailto =  "vanevery@walking-productions.com;
    			$mailheaders = 'From: ' . $mailfrom . "\n"; 
    			$mailheaders .= 'Reply-To: ' . $mailfrom . "\n"; 
    			$mailheaders .= 'Return-Path: ' . $mailfrom . "\n";
    			$mailheaders .= "Message-ID: <".time().">\n"; 
    			$mailheaders .= "X-Mailer: Shawns PHP Mailer Function\n";          
    			ini_set('sendmail_from',$mailfrom);
    			$return = mail($mailto, $mailsubject, $mailmsg, $mailheaders);
    			ini_restore('sendmail_from'); 
    			echo "Mail Sent?: ";
    			var_dump($return);		
    		
    More information:
  • PHP: Mail - Manual


  • More Information:
  • Tons More Here: PHP: Function Reference - Manual