how to add a feedback form in your flash applications
1. Introduction
1.1 Before start
Before get into this tutorial you should know if your server allows you to send mail through PHP. To do that you can write this sample code in a new php file:<?
mail("yourname@yourdomain","test","Ok, it works");
?> Replace "yourname@yourdomain" with your email address and upload this file to your remote server. Now from the browser open that page and then check you mail inbox... if mail arrives you have the permission to send mail, otherwise contact your administrator.
Note: For Windows users (eg, you're testing the script from your local web server), you can likely set the SMTP directive in the php.ini
configuration file to your isp's SMTP mail server - the same as you use
for outgoing mail in your email client (Eudora, Outlook, etc.) . However,
check with your ISP before doing this!
eg
SMTP = mail.your_isp.com
2. Start Up
2.1 The mail() function
Now we can start building our feedback form using FlashMX/PHP (i think one of the most used application in flash).Let's start analyzing how the php mail() function works.
The syntax is: mail(to, subject, body, headers)
- to : recipient mail address
- subject: mail subject
- body: mail body message
- headers: additional mail parameters. In the header you can define From, Reply-To, Cc, Bcc, X-Priority, X-Mailer, etc etc...
For more detailed info go to the php official reference and take a look there.
2.2 The PHP code.
Now we will analyze the php code which sen the email with the paramenters sent by the Flash form.
<?
if(!empty($_POST['sender_mail'])
|| !empty($_POST['sender_message'])
|| !empty($_POST['sender_subject'])
|| !empty($_POST['sender_name']))
{
$to = "yourname@yourdomain.com"; // replace with your mail address
$s_name = $_POST['sender_name'];
$s_mail = $_POST['sender_mail'];
$subject = stripslashes($_POST['sender_subject']);
$body = stripslashes($_POST['sender_message']);
$body .= "\n\n---------------------------\n";
$body .= "Mail sent by: $s_name <$s_mail>\n";
$header = "From: $s_name <$s_mail>\n";
$header .= "Reply-To: $s_name <$s_mail>\n";
$header .= "X-Mailer: PHP/" . phpversion() . "\n";
$header .= "X-Priority: 1";
if(@mail($to, $subject, $body, $header))
{
echo "output=sent";
} else {
echo "output=error";
}
} else {
echo "output=error";
}
?>
Since all the mail arguments (except for the $to variable) are passed by another page (Flash in this case) we need to add the if statement at the beginning in order to check if they are all defined.
So, if one of those HTTP_POST_VARS variales is empty, don't run the mail code and return an error message: output=error
Otherwise, if all those variables exist and they're not empty, we define our mail paramenters paying atention to these lines:
$subject = stripslashes($_POST['sender_subject']); $body = stripslashes($_POST['sender_message']);
We do that because if user input this text: "I'm the one" (example) and we dont' use the stripslashes function, we will receive an email with this text: "I\'m the one". In fact stripslashes returns a string with backslashes stripped off.
Then we add this headers to our email:
$header = "From: $s_name <$s_mail>\n"; $header .= "Reply-To: $s_name <$s_mail>\n"; $header .= "X-Mailer: PHP/" . phpversion() . "\n"; $header .= "X-Priority: 1";
From defines the mail sender, Reply-To (not necessary here) is necessary when the reply recipient is different from the sender email. (When you press the 'reply' button on your mail reader, the email is sent, if defined, to the Reply-To address).
Header finishes with the X-Mail (The php page itself and the php running version) and the X-Priority of the mail (1 is urgent, 3 is normal)
If mail is sent, return the "sent" output, if fails returns a "error" message.
