Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Send Mail with PHP, ASP, or Perl

Send Mail with PHP, ASP, or Perl

By Chad "Cota" Workman | Published 02/15/2007 | Beginner | Rating:
Chad "Cota" Workman

http://www.chadworkman.com & http://www.plaguestudio.com

Skills include: Photoshop, Flash, ASP, PHP, MS SQL, MySQL, VB, C/C+

Developer and Designer

 

View all articles by Chad "Cota" Workman
Setting up Flash

Assuming your flash IDE is open already let's go ahead and get started. In this tutorial we'll cover sending emails through flash using a server side script in ASP, PHP, or Perl we will cover all three. With this you can add email functionality to your flash website for endless possibilities. There are a few things to note here. It is a very wise choice to restrict some aspects of this application. Allowing users to decide where the email is going can lead to some serious headaches. By giving users that ability you open yourself to the user using it for anonymous emails, so be careful.

So, with that being said, let's get started. In Flash, we can do this all in one frame. We're going to keep it simple. Let's create 3 input text boxes and 1 dynamic text box. This part is actually important because not using instance names can cause some confusion, so don't forget instance names. Create the first text box and give it the instance name of "subject_txt". Next let's create another text box and call it "email_txt", again create the 3rd input text box and call it "message_txt". Make sure you set this text boxes property to multiline. Now create a dynamic text box and call it "status_txt". Now create a button and give it a name of "submit_btn". You should have something that looks like this:


Now let's open up the Actions panel and start typing some codes. We'll be doing a few things here. I won't be able to explain every detail about everything used because this tutorial would be way too long. We're simply going to use a flash object called "LoadVars". We're also going to surround the whole thing inside an "onRelase" function for the submit button. We will also do some data validation. Also note, there is some extra code, simply for the sake of this tutorial that will allow you to decide whether to use the ASP, PHP, or Perl file. Here's the code

/* For this tutorial this variable will hold which server-side file you wish
 to use.   For ASP, set serverLang = "asp", for PHP,
set serverLang = "php"   And for Perl, set serverlang = "cgi"   This is for this tutorial.
 In your application this feature wont be required.
  */

var serverLang:String = "asp"; //Create a loadvars object named email_lv
var email_lv:LoadVars = new LoadVars(); //this function is called when email_lv loads the server-side script.
email_lv.onLoad = function(success) {
 //If the script was successfully loaded, this condition is run
 if (success) {
  /* Though the server-side script was loaded, it does not mean it was
      executed successfully. This condition gets a response from the
   server-side script and determines if it was truly successful. */

  if (email_lv.server_mes == "ok") {
   status_txt.text = "Email Sent";
   /* You can add additional code here. This is only run
      if everything went as planned. */

  }
 } else {
  //email failed to send, but script did load. Likely a server issue.
  status_txt.text = "Email Failed";
 }
}; /*This is the onRelease function for "submit_btn" button. This is only run
  if the button was pressed. */

submit_btn.onRelease = function() {
 /* Here we are validating the data. This insures the email address contains
    both the "@" and ".", If not, it stops the script and alerts the user. */

 if (!email_txt.length || email_txt.indexOf("@") == -1 || email_txt.indexOf(".") == -1) {
  status_txt.text = "Invalid Email.";
 
 //This validates the subject line contains text
 } else if (!subject_txt.length) {
  status_txt.text = "Missing Subject";
 
 //This validates the message body contains text
 } else if (!message_txt.length) {
  status_txt.text = "Missing Message";
 
 //If everything is filled out correctly, this is run.
 } else {
  //Collects the data from the text boxes and gives it to email_lv
  email_lv.email_txt = email_txt.text;
  email_lv.subject_txt = subject_txt.text;
  email_lv.message_txt = message_txt.text;
 
  /* Finally, send the data to the server and get a response.
     As mentioned above, serverlang holds the file extendion for
     the server side language. You can hard code the complete file name. */

  email_lv.sendAndLoad("SendMail."+serverLang, email_lv, "POST");
 }
};


You can make any required additions to fit your project, but you get the idea. Now let's move on .



Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Comments
  • Comment #1 (Posted by Martin - notupforgraps at forspiders.com)
    Rating
    Not bad....
    Remember tu encode your doc-type with utf-8, to allow forigners to send mail in flash also.
    Could have been nice if you used an example with the flash.net.FileReference class, to attach files in an email
     
  • Comment #2 (Posted by Berndt - berndt.alexandersson at visionstudios.se)
    Rating
    Code doesn't work
    //email failed to send, but script did load. Likely a server issue.
    status_txt.text = "Email Failed";
    Tested program on to different servers
     
  • Comment #3 (Posted by Cota - Cworkman at evilarts.com)
    Rating
    Most likely a server issue. You can contact me via email for assistance.
     
  • Comment #4 (Posted by Erik - erik at efdesignco.com)
    Rating
    Script works great only minor error in PHP script see below

    PHP code has error. This line:
    $to = <a href="mailto:'your@email.address'">'your@email.address'</a>

    should read $to = 'your@email.address'

    Your var should not be a hyperlink PHP does not understand
     
  • Comment #5 (Posted by Yura - yura at cube.co.ua)
    Rating
    great! but u have error in line:
    if (!email_txt.length || email_txt.indexOf("@") == -1 || email_txt.indexOf(".") == -1) {

    it should looks like

    if (email_txt.length<=1 || email_txt.text.indexOf("@") == -1 || email_txt.text.indexOf(".") == -1) {


     
  • Comment #6 (Posted by an unknown user)
    Rating
    Very well written with lots of comments.
     
  • Comment #7 (Posted by domio)
    Rating
    Couldn't get it to work. I would submit and it would just hang. It would say on the little left corner "Transferring data from..." and then just hang there. What permissions do I put the php file. 644? Ive tried 777 and all combo and still no luck.
     
  • Comment #8 (Posted by Tara)
    Rating
    I couldn't get this to work, so I downloaded the zip file, uploaded it EXACTLY as is, only changing the processing page to PHP, and nothing happens when I click the submit button?
     
  • Comment #9 (Posted by Khanh703 - kngo at wrayward.com)
    Rating
    edit for ASP:
    objmail.From = request("email_txt")
    objmail.To = request("email_txt")
    objmail.Subject = request("subject_txt")
    objmail.Body = request("message_txt")
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.