5/25/2012

Sending Basic Email from Flash using PHP

Recently had to send an email from Flash. Here's the quickest/easiest way to do it (according to me). It uses a very basic PHP script to send the email. To send data to PHP to send an email, the SWF and the PHP would need to be hosted on a web server with PHP capabilities. We will use URLLoader sending data in variables using URLVariables. The Flash/Actionscript part is pretty straightforward: import flash.net.URLRequest; import flash.net.URLVariables; import flash.net.URLRequestMethod; import flash.net.URLLoader; var variables:URLVariables = new URLVariables(); variables.to = txt_to.text; variables.from = txt_from.text; variables.subject = txt_subject.text; variables.body = txt_message.text; var request:URLRequest = new URLRequest("SimpleSendEmail.php"); request.data = variables; request.method = URLRequestMethod.POST; var urlLoader:URLLoader = new URLLoader(); urlLoader.load(request);URLVariables stores the variables we are passing to the PHPscript (from, to, subject, message). Then it is 'sent' with a URLLoader with POST method. In this example, the PHP file is located in the same directory as the SWF. The PHP part is just as simple: <?php $to = $_POST['to']; $subject = $_POST['subject']; $message = $_POST['body']; $from = $_POST['from']; $headers = "From:".$from."\r\n"; $headers .= "Content-type: text/html\r\n"; mail($to,$subject,$message,$headers); ?>The first 6 lines are just reading and storing the variables sent from flash. The header part is a bit more complicated and allows you to set more email options, such as CC,BCC,etc. You can take a look at the PHP documentation here. The function mail in the last line sends the email. And...that's it! Here is a sample: source This is a quick and easy way to let visitors of your flash site to send you a message via email. And just in case if you're curious, here is the app I made that uses this. Using a webcam, it allows users to measure their Pupillary Distance (distance between their pupils). Then it allows you to email yourself the results. Oh and if you're in the market for some fashionable and affordable glasses, be sure to check Vusion Glasses out!

1 comment:

  1. how would you send an attachment such as an mp3 file. also, looks like the site is outta sight? thanks!

    ReplyDelete