0

I'm having endless troubles creating a contact form in PHP.

I first tried the method to write the data (as outlined in the question here: HTML Form Not Outputting To CSV File (Or Displaying Correct Error Messages)) and now I'm just trying the method of sending the info in an e-mail to myself.

Here's my jsfiddle:

http://jsfiddle.net/tqs6g/1/

Here's the PHP I've written to send the info to myself (I understand it's not very elegant coding but I just want to get something to work first and then I was going to use arrays, etc.):

<?php

if (isset($_POST['brandname']) && isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['email']) && isset($_POST['website'])){
    $brandname = $_POST['brandname'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    $website = $_POST['website'];

    if(!empty($brandname) && !empty($firstname) && !empty($lastname) && !empty($email)){
        $to = 'myemail@example.com';
        $subject = 'Submission Form';
        $body = $firstname;
        $headers = 'From: '.$email;

        if (@mail($to, $subject, $body, $headers)){
            echo 'Yeah yeah';
            }else{
                echo 'yeah no.';
                }

        }else{
        echo 'Please fill out all required fields.';
        }

    }
?>

The e-mail I used in this example is obviously not my e-mail (I use my real one in my code). Basically what happens is I fill out the form and hit submit and it reloads the form.php page. I check my e-mail and nothing has come through. If I check the source code for the site the words 'Yeah yeah.' show up in the code (from where it echos out when it successfully sends the e-mail). So the code seems to be running properly but it's not outputting anything.

Do I have to set up some mail servers/functions through my localhost?

What am I missing out on?

Community
  • 1
  • 1
MillerMedia
  • 3,651
  • 17
  • 71
  • 150
  • If you're working locally (e.g. with XAMPP) you need to set a path to sendmail in `php.ini` in order to send emails. This may be helpful: http://www.codingforums.com/archive/index.php/t-77553.html – Quasdunk Jan 07 '12 at 19:34
  • Could you turn errors an warnings on, or check your logs? – mat Jan 07 '12 at 19:36
  • My php.ini already has: [mail function] ; For Win32 only. SMTP = localhost smtp_port = 25 . Is this the line that I should be looking at? – MillerMedia Jan 07 '12 at 19:38
  • What warnings would I need to look at? As far as it looks like, the code is running correctly since everything reloads and it sends a successful echo back. Where would I start with my debug? – MillerMedia Jan 07 '12 at 19:39
  • I changed the SMTP = localhost to SMTP = smtpout.secureserver.net (which is the settings for a GoDaddy e-mail that I have set up) and it still doesn't work... – MillerMedia Jan 07 '12 at 20:00
  • @MxmastaMills yep, that's the area to look at. As far as I remember, you also have to modify something in the `sendmail.ini` which should be located in your `xampp\sendmail\ ` folder (I guess, I'm working with linux now). Also try googling for something like `xampp set up sendmail`. – Quasdunk Jan 07 '12 at 20:02
  • Hmm yeah I've googled around a bit, all I've found are people mentioning to edit the lines in the php.ini file. I'll try to locate the sendmail.ini file to edit. – MillerMedia Jan 07 '12 at 20:05
  • @MxmastaMills since you want to use GoDaddy, here's how to do it: http://stackoverflow.com/questions/1993070/sending-emails-with-wamp – Quasdunk Jan 07 '12 at 20:10
  • @Quasdunk Cool thanks. For some reason I don't have an sendmail.ini on my system. I'll have to look into that. – MillerMedia Jan 07 '12 at 21:01
  • It appears that this is a huge problem for user of Mac OSX (which I am) and there are alot of threads online about it. It looks like the best best for me right now is to upload to my server and test it there even though it would be nice to have a localhost solution at some point. – MillerMedia Jan 07 '12 at 22:12
  • 1
    @MxmastaMills Alright, I see... I can relate to your problem, since Linux and Mac OS are based on UNIX. The best solution I've found: Install PostFix. This seems quite promising and quite similar to what I did: http://hints.macworld.com/article.php?story=20081217161612647 - maybe my question about that will also help you a little: http://askubuntu.com/questions/88117/sending-mails-with-php-lampp-and-postfix - Good luck! – Quasdunk Jan 07 '12 at 23:56
  • @Quasdunk Great, thanks for getting me on the right track. I did upload my files to the FTP and ran them on the web and they worked fine but I'm still going to try to get it working so I don't have to go through uploading every time I want to check something. I'll report back if I find a fix. – MillerMedia Jan 08 '12 at 00:00

1 Answers1

0

You don't need to set anything up on your server, it works fine for me.

Try replacing the @mail with just mail. mail(...) always works for me.

H Bellamy
  • 22,405
  • 23
  • 76
  • 114
  • 1
    The [`@` 'operator'](http://thesmithfam.org/blog/2006/05/07/php-the-operator/) suppresses error messages. `@mail` and `mail` call the same function. – Alex Jan 07 '12 at 19:27
  • @H Bellamy I tried that but it didn't change anything. – MillerMedia Jan 07 '12 at 19:40