Monday, April 20, 2009

Sending emails from Flash using PHP

This is a small tutorial which sends out mail from your flash file(AS2). This works similar to 'email to friend' options in websites or 'comments' in blogs. This is a basic tutorial which will help you to send emails to a particular email ID.


Step 1: In the Flash IDE create 3 input text fields. One to enter the Name , second to enter Email ID of the sender, third one to type message.

Step 2: Assign instance names for the input text fields as follows. 'userName' for Name text field, 'userEmail' for Email ID text field, and 'msgText' for message text field.

Step 3: Create a button say 'GO' and assign the instance name as 'submitBtn'.

Step 4: Now create a new layer and rename it as 'action'. Press F9. Here you need to right your program.

Copy paste below given code to actions layer.


Selection.setFocus(_root.userName)
//This sets the cursor focus into the Name text box.
stop(); // This stops the movieclip.

// -----Here comes the form-----

var gatherForm:LoadVars = new LoadVars();

function sendForm() {

// The mail ID to which the mail has to be sent.
gatherForm.email_to = "recepient@domain.com
";
// Catching the inputted value in the user name field
gatherForm.visitor_name = _root.userName.text;
// Catching the inputted value in the user Email ID field
gatherForm.visitor_email = _root.userEmail.text;
// Catching the inputted value in the message bo
gatherForm.visitor_message = _root.msgText.text;
//this points to the script posted below
gatherForm.sendAndLoad("email.php", gatherForm, "POST");
}


Step 5: Trace the action from Go button

_root.submitBtn.onRelease = function() {

if (_root.userName.text == "") {
_root.msgText.text = "There seems to be an error, Please check that you have filled out the form correctly.";
} else {
sendForm();

_root.msgText.text = "Thank you. You will receive a confirmation e-mail from us here at wherever.";
/
}
};

Step 6: Now create a PHP file with following code and keep it it your main directory. Save the file as email.php.

//******?php
$sendTo = "recepient@domain.com";
$subject = "My comments";
$headers .= "Reply-To: " . $_POST["visitor_email"] . "\r\n";
$headers .= "Return-Path: " . $_POST["visitor_email"];
$message = $_POST["visitor_message"];

mail($sendTo, $subject, $message, $headers);

?>****/

Explanation:

////?php This indicates the the beginning of the PHP code. This tells the server that we are gonna write a PHP code!!!
//mail() --->
It is a great PHP in built function which allows you to sends out mail. It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. The syntax for mail function is as follows:
mail (recipient, subject, message, other headers);
//$sendTo = "recepient@domain.com " ---> Replace this code with the email address you wish your form to send to.

//$subject ---> In PHP '$' denotes a variable. Thus this is the variable called
subject. The values here is the text you assigned. The same will appear on the subject line of the mail.
//$message ---> The variable
message. This will have the value inputted by the sender through your flash file.

//$_POST ---> This is for retrieving values from the variables that Flash sent. Since Flash sent the variables using post, we would use $_POST to get them into variables of our own. $_POST is a special global variable in a PHP script that contains all of the posted variables sent to that script as an associative array. Using brackets ([]) and a variable name, you can then retrieve those variables. For those variables sent by this particular example, you would obtain their value in PHP using $_POST["visitor_name"], $_POST["visitor_email "], and $_POST["visitor_message"].

?> ---> Indicates that the PHP code ends here.

Please note that this program will work for LINUX based server which supports PHP and with SMTP enabled.

No comments:

Post a Comment