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.

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.

<?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 &lt;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!