AS3 / PHP email form HELP

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by phils13776, May 11, 2011.

  1. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Hello all,

    Im having problems setting up an email form using action script 3 and php. I have tried numerous variation of forms with no luck. The form is up on the site. It confirms that the message was sent. However i never get a message on my hosted email. PLZ HELP!



    HERE IS THE ACTION SCRIPT
    (sample that Im using):


    // Set text formatting colors for errors, waiting..., and success mechanisms
    var errorsFormat:TextFormat = new TextFormat();
    errorsFormat.color = 0xFF0000;

    var waitingFormat:TextFormat = new TextFormat();
    waitingFormat.color = 0x339900;

    var successFormat:TextFormat = new TextFormat();
    successFormat.color = 0x3366FF;

    // hide the little processing movieclip
    processing_mc.visible = false;

    // Assign a variable name for our URLVariables object
    var variables:URLVariables = new URLVariables();

    // Build the varSend variable
    var varSend:URLRequest = new URLRequest("http://www.lavidapictures.com/contact_parse.php");
    varSend.method = URLRequestMethod.POST;
    varSend.data = variables;

    // Build the varLoader variable
    var varLoader:URLLoader = new URLLoader;
    varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    varLoader.addEventListener(Event.COMPLETE, completeHandler);


    // Handler for PHP script completion and return
    function completeHandler(event:Event):void{
    // remove processing movieclip
    processing_mc.visible = false;
    // Clear the form fields
    name_txt.text = "";
    email_txt.text = "";
    msg_txt.text = "";
    // Load the response from the PHP file
    status_txt.text = event.target.data.return_msg;
    status_txt.setTextFormat(successFormat);
    }

    // Add an event listener for the submit button and what function to run
    submit_btn.addEventListener(MouseEvent.CLICK, ValidateAndSend);

    // Validate form fields and send the variables when submit button is clicked
    function ValidateAndSend(event:MouseEvent):void{

    //validate form fields
    if(!name_txt.length) {
    status_txt.text = "Please enter your name.";
    status_txt.setTextFormat(errorsFormat);
    } else if(!email_txt.length) {
    status_txt.text = "Please enter an email address";
    status_txt.setTextFormat(errorsFormat);
    } else if(!validateEmail(email_txt.text)) {
    status_txt.text = "Please enter a VALID email address";
    status_txt.setTextFormat(errorsFormat);
    } else if(!msg_txt.length) {
    status_txt.text = "Please enter a message.";
    status_txt.setTextFormat(errorsFormat);
    } else {

    // All is good so send the message to the parse file
    // Show the little "processing_mc" movieclip
    processing_mc.visible = true;

    // Ready the variables for sending
    variables.userName = name_txt.text;
    variables.userEmail = email_txt.text;
    variables.userMsg = msg_txt.text;

    // Send the data to the php file
    varLoader.load(varSend);

    // Put a temporary message in the response field while the PHP file sends back
    // If the code does not connect to the PHP file this message will remain visible to user
    status_txt.text = "Waiting for server connection...";
    status_txt.setTextFormat(waitingFormat);

    } // close else after form validation

    } // Close ValidateAndSend function //////////////////////////////////////////////////////////////


    // Validate email function
    function validateEmail(str:String):Boolean {
    var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
    var result:Object = pattern.exec(str);
    if(result == null) {
    return false;
    }
    return true;
    }
    ////////////////////////////////////


    HERE IS THE PHP SCRIPT:

    <?
    // Create local variables from the Flash ActionScript posted variables
    $senderName = $_POST['userName'];
    $senderEmail = $_POST['userEmail'];
    $senderMessage = $_POST['userMsg'];

    // Strip slashes on the Local variables for security
    $senderName = stripslashes($senderName);
    $senderEmail = stripslashes($senderEmail);
    $senderMessage = stripslashes($senderMessage);

    // IMPORTANT - Change these lines to be appropriate for your needs - IMPORTANT

    $to = "bryan@lavidapictures.com";
    $from = "$senderEmail";
    $subject = "Contact from your site";

    // Modify the Body of the message however you like
    $message = "Message from your website:

    Their Name: $senderName
    Their Email: $senderEmail
    Their Message is below: $senderMessage";

    // Build $headers Variable

    $headers = "From: $from\r\n";
    $headers .= "Content-type: text\r\n";
    $to = "$to";
    // Send the email
    mail($to, $subject, $message, $headers);

    // Assemble the message that goes back to Flash
    // The flash ActionScript is looking for a return variable of "return_msg"
    $my_msg = "Thanks $senderName, your message has been sent.";
    // Print the data back to flash who is patiently waiting for it in the onCompleteHandler
    print "return_msg=$my_msg";
    // Exit script
    exit();
    ?>
     
  2. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    have you seen this script work somewhere? I find AS3 a bit different than what I'm used to seeing.

    here is what you got:
    Code:
    / Build the varSend variable
    var varSend:URLRequest = new URLRequest("http://www.lavidapictures.com/contact_parse.php");
    varSend.method = URLRequestMethod.POST;
    varSend.data = variables;
    
    // Build the varLoader variable
    var varLoader:URLLoader = new URLLoader;
    varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    
    
    I would expect something more like this: (also you should access your php script not using http protocol but access it locally)
    Code:
    var loader:URLLoader = new URLLoader();
    var req:URLRequest = new URLRequest("http://www.lavidapictures.com/contact_parse.php");
    var variables:URLVariables = new URLVariables();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    req.method = URLRequestMethod.POST;
    

    here is 1 ready made for ya


    Also, maybe the problem is not the AS3, if your sure that php parser was actually called, then you should get an email because that parser works on it's own. If any data was not carried over from flash it would still send you the email. You may not get the content you expected in the email, but if that parser ran you would get an email.

    The only thing I might see that parser not sending the email is if the header is empty. I'm not sure what happens then. So try this:

    change:
    Code:
    $from = "$senderEmail";
    to something static like this:
    Code:
    $from = "bryan@lavidapictures.com";
    then run the script directly, you wont get content inside the email but you should get an email. If you don't, maybe the issue is somewhere else.
     
    Last edited: May 11, 2011
  3. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the response. I will try what you mentioned. BTW i looked that link before. The source link is broken. I believe i tried that script with no luck. I tried 4 diff ones. :(
     
  4. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    i downloaded it and it works fine for me, you just have to open the "contact_form.php" file and make changes to line #17, 25, and 26 replacing the website name and email address.

    Dont forget to grab the :
    1 - "js" folder
    2- the code inside the "index.html" file and put it in your own page
    3- "contact_form.swf" file
    4 - "contact_form.php"

    all this have to be in the same folder on your website. (you will find all this in the "deploy" folder inside the zip file.)
    I attached the file if your having problems downloading it.
     

    Attached Files:

    Last edited: May 11, 2011
  5. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    i tried the code below and now the php is not being called and there is no confirmation.
    http://www.lavidapictures.com/

    I will attempt to use the one you provided next and let you know.

    I have a feeling its something with x10. Maybe i have to change something. I setup the email account through cpanel. Maybe there settings i need to change. The mailbox works as i can send and receive from my email client (from/to bryan@lavidapictures.com).



    NEW CODE PHP:

    <?

    var loader:URLLoader = new URLLoader();
    var req:URLRequest = new URLRequest("contact_parse.php");
    var variables:URLVariables = new URLVariables();
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    req.method = URLRequestMethod.POST;


    $senderName = $_POST['userName'];
    $senderEmail = $_POST['userEmail'];
    $senderMessage = $_POST['userMsg'];

    // Strip slashes on the Local variables for security

    $senderName = stripslashes($senderName);
    $senderEmail = stripslashes($senderEmail);
    $senderMessage = stripslashes($senderMessage);


    $to = "bryan@lavidapictures.com";
    $from = "bryan@lavidapictures.com";
    $subject = "Contact from your site";
    // Modify the Body of the message however you like
    $message = "Message from your website:

    Their Name: $senderName
    Their Email: $senderEmail
    Their Message is below: $senderMessage";


    $headers = "From: $from\r\n";
    $headers .= "Content-type: text\r\n";
    $to = "$to";

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


    $my_msg = "Thanks $senderName, your message has been sent.";

    print "return_msg=$my_msg";

    exit();
    ?>
     
  6. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    don't assign the "$to" variable a second time, just erase that line above.

    when you run this script directly you may not get feed back and just a white page. if you want feedback for sure when you run it directly replace
    Code:
    mail($to, $subject, $message, $headers);
    with
    Code:
    if(mail($to, $subject, $message, $headers)){
    echo "email sent";
    }else{
    echo "email not sent";
    }
    
    keep a copy of the original in case you want to use it, then again it's all written here in this forum anyways... If you follow the instructions with the zip file you'll be able to scrap all this.
     
  7. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Alright I had tried then one you supplied. I just did it again just in case. It gets a server error.
    see here www.lavidapictures.com.

    I uploaded all the files including the js folder/file. The php script is posted below.

    :( this is frustrating. I never had so much trouble with anything.


    <?php
    if(empty($_POST['senderEmail'])){
    echo"Error: No email address found";
    exit;
    }
    $senderName = $_POST['senderName'];
    $senderEmail = $_POST['senderEmail'];
    $senderMessage = nl2br($_POST['senderMessage']);
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";


    $headers .= "From: bryan@lavidapictures.com <> \n";
    $headers .= "Reply-To: " . $senderEmail . "\n\n";

    $siteName = "lavidapictures.com";
    $to = "bryan@lavidapictures.com ";

    $toSubject = "Message from $senderName via $siteName";
    $emailBody = "From: $senderName <br />
    Email: $senderEmail <br /> <br />
    Message: <br />
    $senderMessage
    <br />";
    $message = $emailBody;
    $ok = mail($to, $toSubject, $message, $headers);
    if($ok){
    echo "returnValue=1";
    }else{
    echo "returnValue=0";
    }
    ?>
     
  8. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    let see fisrt if your mail function works... create a new php file and put this in it
    Code:
    <?php
    $headers = "From: bryan@lavidapictures.com\r\n";
    if(mail("bryan@lavidapictures.com", "this is my subject", "this is my message", $headers)){
    echo "email sent";
    }else{
    echo "email not sent";
    }
    ?>
    
    then run the script and check you mail.

    also...
    I see your sending the email in text/html format, if I remember correctly someone on here said that the php mail function only work for text version, (Content-Type: text/plain) no html. I would check on that. If so, that probably the server error your getting. If you have access to your error logs file in your Cpanel check what it's telling you about the error.
     
    Last edited: May 11, 2011
  9. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    No sure if I "ran" it correctly. I created new php file(with your script) uploaded it. Then I accessed it at:
    www.lavidapictures.com/test.php The response was email not sent.

    Its strange if the previous files worked on your site; why not mine.
     
  10. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    I'll try it on the x10 server later, I'm about to leave for work. I had tried it on my personal site could be a different story.

    if that simple mail function (test.php) did not work that's a problem, and you'll have to take that up with X10, that's as simple as it gets. Probably the problem to all your problems.

    ---------- Post added at 10:40 AM ---------- Previous post was at 06:55 AM ----------

    I can confirm that the problem doesn't envolve the code in the zip file. It works fine on my site, but I get the same "server error" when i uploaded it to my free X10 account. Don't break your head with the code, it's a server configuration somewhere. If you can check the error log file it might be logged in there. I would go but I can't access my cpanel from work. We're behind a proxy and it won't let me go to my cpanel from my work station.

    this one won't work, and I tried various versions, like changing the "$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";" to "Content-type: text/plain;" to no success.
    http://bidzey.x10.mx/deploy/

    this is the identical code running here successfully, and it's set to send you the message, I will be deleting this page on your request, or no later than tonight before I go to bed if I don't hear from you. I sent you an email using it, and it said "sent successfully" check you email as soon as you read this.
    ( URL AS BEEN DELETED )


    so once you figured out why you get a server error and fix it, just follow the instruction in the post where the zip file is attached and your problems will be over.
    look at post #19 here, and try that first
     
    Last edited: May 12, 2011
  11. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Thanks bidzey. I confirm that the second link says that it has been sent. However no messages when when i check my webmail through cpanel. I had read the thread you posted previously, yet it was n o help.

    I guess i have to open a ticket. Thanks for all your help. I will update you on the situation.

    ---------- Post added at 06:35 PM ---------- Previous post was at 06:21 PM ----------

    The funny thing is my auto reply that the "email was received" works. I got the message in Outlook. So it gets picked up by the server, and the script works. Is there something wrong with the webmail (SquirrelMail)?? AAAARRRGHHH!
     
  12. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    I'm having a boring afternoon so I'm passing time with this :smile:

    Here is a new zip file. I implimented PHPmailer with the code of contact_form.php. Go to your cpanel get your smtp configurations and change the info. you'll need to put info on line #11, 32, 34, and 35. Basically your supplying your email address, smtp host name, and your email username and password.

    It doesn't give an error it says sent successfully but I didn't recieve an email yet. Sometime our server is slow.
     

    Attached Files:

  13. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    BTW, I got your message in my email client. So that second form works. Was that second link on the x10 server? I just had to check it on a different device...sorry i didn't tell you in the previous post.


    P.S. ,, the link that is now deleted..
     
    Last edited: May 12, 2011
  14. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    the one that worked came from my server not x10, and yes I deleted I just wanted to show you there's nothing wrong with the code itself and it works fine.

    the one on x10 is different now of the version when I had originally posted. I added PHPMailer to it. It says "sent successfully" but I'm not getting the emails so I'm not sure what's going on.

    I also got a hard time consentrating now because I just got news my nephew qualified to the final round of tryouts for team canada under 17. :smile: He also got drafted in the OHL last week by Owen Sound Attacks... whoo whoo!!!
     
    Last edited: May 12, 2011
  15. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Let me know if you can get one working on X10. Im still waiting to hear back from x10.
     
  16. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    Yes I got it... for some reason it took 5-10 minutes to recieve the email, but I got it.

    go here and try it out I got it so you'll receive the email. You might have to wait a few minutes if it's like mine. I'm using the SMTP that x10 gave me. Not Gmail, doing so your opening up your gmail account to any staff at X10. I'm sure they're an honest bunch, but it's just not safe practice.

    Here is the new package, there's 6 comments in the code (instructions) starting at the top. Let me know if you got that email I sent you using the flash form on the x10 server. You wont need to open a ticket with x10 if you got it.

    put all files and the 2 folders (js, language) in the same directory on your site.
     

    Attached Files:

    Last edited: May 12, 2011
  17. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    I got your email. Great, this is looking good. I hope it works on my site now.
     
  18. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0
    see my post above, i was modifying it when you posted :)
    My free account was as stuborn as yours so it should work on your account too

    also the part of the script where you got "$mail->AddReplyTo"

    this should be set like this, my comment may not be clear on that one;
    Code:
    $mail->AddReplyTo($senderEmail,$senderName);
    I deleted the script on my account b.t.w. in case someone wants to spam :smile:
     
    Last edited: May 12, 2011
  19. phils13776

    phils13776 New Member

    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    i will attempt and let you know how it goes

    ---------- Post added at 02:00 AM ---------- Previous post was at 12:52 AM ----------

    HEY BIDZEY THANKS!!! IT WORKSSSSSS YAAAAA. Youre the best. Im not getting it in my squirrel mail. But as long is it sends.


    Thanks very much. I hope implementing it into my flash project will trouble free :D
     
  20. bidzey75

    bidzey75 New Member

    Messages:
    53
    Likes Received:
    0
    Trophy Points:
    0

Share This Page