Emailing images directly from Flash with PHP

For an interactive drawing project I worked on recently I needed to provide the ability for users to email their artwork directly from the app without the need to save any images to a server. I pieced together a solution from various blogs and websites and I decided to provide the full source here.

The function below takes an encoded bitmapdata and an email address and sends them to the PHP script further down in this post. The bytearray passed in must be encoded as a JPG – you can use any of the available encoders such as CoreLib, Faster JPG Encoder, or the Alchemy Encoder.

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
var scriptLocation:String = "http://www.website.com/location/of/script.php";

function sendFile(imgEncoded:ByteArray,email:String):void
{
    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");

        var request:URLRequest = new URLRequest();
        request.requestHeaders.push(header);
        request.url = scriptLocation+"?user="+email+"&r="+Math.round(Math.random()*10000);
        request.method = URLRequestMethod.POST;
        request.data = imgEncoded;
           
        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.BINARY;
        loader.addEventListener(Event.COMPLETE, onSent);
        loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
        try
        {
            loader.load(request);
        }
        catch(error:Error)
        {
        onError(null);
        }
}

function onSent(e:Event):void
{
    // send complete
}

function onError(e:IOErrorEvent):void
{
    // on send error
}

And that is all for the ActionScript side of things, now you just need to put the following PHP script on your server somewhere and reference it in the above AS and you should be getting images emails directly from you app without any saving to a server. Make sure you put your own details into the script for the message, sender and subject.

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
36
37
38
39
40
<?php

if ( isset ( $GLOBALS["HTTP_RAW_POST_DATA"] )) {
   
    $image = $GLOBALS["HTTP_RAW_POST_DATA"];
   
    $to = $_GET['user'];
   
    $subject =   'The Email Subject here'; /* put in your own email subject */
    $bound_text =   "xy$P123";
    $bound =    "--".$bound_text."\r\n";
    $bound_last =   "--".$bound_text."--\r\n";
         
    $headers =  "From: John Smith <john@smith.com/>\r\n"; /* put in your own from details */
    $headers .= "MIME-Version: 1.0\r\n"
        ."Content-Type: multipart/mixed; boundary="$bound_text"";
         
    $message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
        .$bound;
         
    $message .= "Content-Type: text/plain; charset="iso-8859-1"\r\n"
        ."Content-Transfer-Encoding: 7bit\r\n\r\n"
        ."This is where any text for your email message should go!\r\n" /* Put any text messages in this string */
        .$bound;
   
    $message .= "Content-Type: image/jpg; name="drawing.jpg"\r\n"
        ."Content-Transfer-Encoding: base64\r\n"
        ."Content-disposition: attachment; file="drawing.jpg"\r\n"
        ."\r\n"
        .chunk_split(base64_encode($image))
        .$bound_last;
    if(mail($to, $subject, $message, $headers))
    {
         echo 'result=true';
    } else {
         echo 'result=false';
    }
}

?>

I hope this is useful to someone!

Posted toActionScript AIR Flash PHP