http://www.chadworkman.com & http://www.plaguestudio.com
Skills include: Photoshop, Flash, ASP, PHP, MS SQL, MySQL, VB, C/C+
Developer and Designer
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
You can make any required additions to fit your project, but you get the idea. Now let's move on .
I'm assuming you are at least slightly familiar with classic ASP, so I will allow the comments in the code to provide the map for whats going on here. The SendMail.ASP file uses CDONTs to send the email. Make sure your server supports CDONTS. Here's the ASP code:
[code]
<%
dim themail, thename, themessage, i
'Gets the incoming variables from flash
themail = Request("email_txt")
thesubject = Request("subject_txt")
themessage = Request("message_txt")
'Decalre and create email object
dim objmail
set objmail = Server.CreateObject("CDONTS.Newmail")
'error handler, if error encountered, ignore it and proceed
On Error resume next
'build the email using the variables from flash
objmail.From = themail
objmail.To = themail
objmail.Subject = thesubject
objmail.Body = themessage
objmail.Send
'error handler, if error encountered, ignore it and proceed
On Error resume next
'If any errors were encounter then run this code
If Err.Number <> 0 then
'tells flash ASP failed and terminates the ASP file.
Response.Write "&server_mes=fail"
Response.End
else
'Send message back to flash saying everything was ok.
Response.Write "&server_mes=ok"
End if
%>
[/code]
Assuming your server supports CDONT's, then this script should work well. Let's move on.
Here is the PHP code, it doesnt almost the exact same thing the ASP code does. For those that skipped the ASP code, I'm assuming you are familiar with PHP and can use the comments in the code to follow. The PHP code was writen by Josh Musselwhite.
[php]
<?php //tells the server this is php
//-------------------------------------
// the below code set the headers. There can be a bit difficult to understand fully.
// Information about them and all the commands below can be found at the PHP manual
// NOTE TO ALL USERS - Gmail and some older server configs DO NOT like \r\n, just use \n with it comes to html emails.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Your Websites Name Here <' . $_POST['email'] . '>' . "\r\n"; //you can put your websites name in here or anything else you like
//$headers .= 'Bcc: another@email.com' . "\r\n"; //this is optional.
//------------------------------
// gets variables from flash
$to = 'your@email.address' //inset the email address it goes to here
$subject = $_POST['subject_txt'];
$message = $_POST['message_txt'];
if ($_POST['message_txt'] != "") { //this checks to make sure someone didn't visit the page manually by making sure message_txt is not empty
$ok = mail($to, $subject, $message, $headers); //this line sends the mail and returns true or false
if($ok) {
echo "&server_mes=ok&"; //if mail was send print to the screen "&server_mes=ok"
} else {
echo "&server_mes=fail&"; // if mail was NOT sent, print to the screen "&server_mes=fail"
}
}
?>[/php]
Note, Gmail does not like the "\r\n" method of new lines, use "\n" instead.
Ok, one final server side method. For those that for some reason can't use ASP or PHP you can use Perl. Perl is one of those server side scripts that doesn't care if you're using Unix/Linux or Windows, it will run on both. There are 2 Perl files, don't worry about the "SubParse" file, just focus on the main file, "SendMail.cgi". Here's the code:
[code]
#!/usr/local/bin/perl
require "subparse.lib";
&Parse_Form;
$theMail = $formdata{'email_txt'};
$theSubject = $formdata{'subject_txt'};
$theMessage = $formdata{'message_txt'};
open (MAIL, "|/usr/sbin/sendmail -t") || &ErrorMessage;
print MAIL "To: $theMail \nFrom: $theMail\n";
print MAIL "Subject: $theSubject\n";
print MAIL "$theMessage\n";
close (MAIL);
print "Content-type: text/html\n\n";
Print "&server_mes=ok";
sub ErrorMessage {
print "&server_mes=Fail";
exit; }
[/code]
So there you have it. Sending mail through flash with 3 methods. Which to use it completely up to you. Modifying the code is fairly easy if you just follow the blue prints that have been laid out for you.