treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Chris's simple contact form doesn't work

  • Hi all!

    I want to use the contact form which Chris is created and when I click on send button the message comes and tells "The message has been sent successfully" but actually I don't get any emails.

  • Simple questions, but important:

    What type of server are you running it on? (Apache, windows?)

    Are you running it on a "live" server, or from a personal (local) installation?

    Are you sure you are not sending emails? If you are on apache with valid PHP, on a real live server that is actually capable of sending email (your local installation probably won't be if that's what you're doing), then it could be that your email is being filtered out by your spam filter.

  • I'm on apache server now and using the script in live server.

    I also checked my spam folder but nothing is there.

    I thought it could because of I didn't touch "$EmailFrom = "chriscoyier@gmail.com";" then I change it to mine but again it didn't work.

    Please help!

  • Does PHP's mail() work correctly?

    <?php
    $to      = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
        'Reply-To: webmaster@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    

    Change nobody@example.com to your own email and the example.com bit of webmaster@example.com to your own domain name.

  • Do you have PHP installed on the server? Or just Apache? It will not work with out PHP.

  • If PHP were not installed, I doubt the application would appear correctly. He'd see php tags, for a start, and probably wouldn't see a "The message has been sent successfully" message.

  • I have these lines:

    $EmailFrom = "vblig@yahoo.com";

    $EmailTo = "vblig@yahoo.com";

    $Subject = "Nice & Simple Contact Form by CSS-Tricks";

    $Name = Trim(stripslashes($_POST['Name']));

    $Subject = Trim(stripslashes($_POST['Subject']));

    $Email = Trim(stripslashes($_POST['Email']));

    $Message = Trim(stripslashes($_POST['Message']));

    but yours is different from mine I think.

  • Why don't you just use a script that does work right out of the gate?

  • @AlirezaM

    Yes, mine is different from yours. That's because I want you to isolate components of the application in order to work out where it is going wrong.

    The script I posted uses the mail() function and some variables and nothing else. If it doesn't work, then the problem is somewhere in that function. If that works, then the problem is elsewhere.

    The application works. I have installed it on my own domain and I receive emails fine. Therefore there is something on your end that is different.

    To test my script, create a blank php file (call it testmail.php or something), copy the code I posted into it and save it into your domain. Then visit the script. mail() will then be used as expected and you can see if you receive the email.

  • @websitecodetutorials

    which script for example?

    @BenWalker

    I didn't get exactly what to do. I want to use the mail script as chris's and show a message for success or error after sending mails.

  • I want to use the mail script as chris's and show a message for success or error after sending mails.

    I understand that. But you say you have uploaded the scripts onto the server and when you complete the form it gives you the success message, but you don't receive the email.

    I am trying to help you establish what isn't working. You say you are on an actual server and I assume it is running PHP fine. Mail isn't being filtered out as spam and I know that Chris' scripts (here) work on my server, so the fault isn't with them.

    The only function that I can see being a problem is mail(), as everything else is string manipulation or would lead to an error and wouldn't (I think) have produced the "Your message has been sent!" feedback when you use it.

    So, I posted code above that cut out a lot of code so that you could test, on its own, whether mail() was working on your server. All I want you to do is save that code in a PHP file (on your server) then visit the page. You won't see anything in your browser, but it should send an email to the address you enter. If it doesn't, we know the problem is with the mail() function and you'll probably need to talk to your web host about resolving it. If it does work, we can dig deeper into why the script isn't working for you.

  • Post complete code here so we can see script in its entirety. From the code sample you provided it is unclear if you are passing variables to the mail() function correctly.

  • @BenWalker

    I uploaded the file mail.php holding your codes on my hosting but it didn't work then I decided to check in another hosting and it worked. So as you said it was about my hosting and I should talk with the moderators to see what they can do for me.

    Thank you so much for these kind helps! I really apperciate your work.