2

I have been trying to set up a simple form on my website that sends an email to my inbox (not the website visitor's) with email address that the visitor enters into the form. It isn't very complicated, but as I am testing, I can't seem to get any email address to work. I don't get any error message either. I will paste the code below.

At the very least, I'd like to know if there could be a problem with the DNS or host configurations. This is my first time building a website, so I'm unsure if the problem is in the code, the HTML (a separate file not pasted here), or the server or host settings.

To make matters more difficult, I have once gotten an error message along the lines of something not configured properly, but I can't seem to get the error message anymore. I don't think I changed any code. That makes me suspect there may be something else going on server-side. Is that feasible?

Thanks in advance for taking a peek.

<?php

$to = "MyEmailInbox@gmail.com";
$from = "mail@example.com";

$headers = "From: " . $from . "\r\n";

$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];


if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{ 
    if (mail($to, $subject, $body, $headers, "-f " . $from))
    {
        echo 'You will be notified on <b>  ' . $_POST['email'] . '</b> :)';
    }
    else
    {
       echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';   
    }
}
else
{
   echo 'There was a problem with your e-mail (' . $_POST['email'] . ')';   
}

As far as the index.html is concerned, I have a single form next to a Send button. The form presumably takes the user information, filters nonsense out, and then the Send button calls the PHP script. Please find the relevant index.html code below:

<!-- SIGN UP SECTION ############################################### -->

<section id="signup">

    <div class="row">

        <!-- Title --> 

        <div class="seven columns centered">

            <h2>Sign up to receive our newsletter!</h2>

            <!-- Begin the Form --> 

            <form action="form_sender.php" method="post">  

            <!-- Input of E-Mail --> 

            <div class="eight columns">

                <input name="email" class="email" type="text" placeholder="YOUR E-MAIL PLEASE ?">

            </div>

            <!-- Send Button -->

            <div class="four columns">

                 <button name="send" type="submit" class="submit">SEND</button>

            </div>

            <!-- End of the Form --> 

            </form> 


        </div>

        <!-- Text Promise we do not spam -->

        <div class="twelve columns centered">

        <p class="spam">We do not spam.</p>

        </div>

    </div>

</section>
Erika Electra
  • 1,854
  • 3
  • 20
  • 31
  • what's getting echoed on submit? Get rid of everything after $headers and try again – Bryan Sep 14 '13 at 09:08
  • Do you try to send mails via localhost? Or is it an dns known address? When localhost, you have to set smtp-settings in the php.ini. Under several conditions you have to do this on your online server, too. Some hosters allows it, some does not. For debugging first set ERROR_REPORTING(E_ALL); Then tell us what is there. – Dennis Ziolkowski Sep 14 '13 at 09:10
  • on which OS do you develop? – gherkins Sep 14 '13 at 09:10
  • If the email address is valid (passes FILTER_VALIDATE_EMAIL), then I say 'You will be notified on ' and reconfirm with the submitted email address. – Erika Electra Sep 14 '13 at 09:11
  • Re: Dennis --- I have no idea. I just bought a domain name, paid for some hosting, and then started writing HTML and scripts for the content. I don't know whether the emails are being sent via localhost or dns known address. Where might I set these parameters? Sorry, I am really just beginning at this. – Erika Electra Sep 14 '13 at 09:13
  • Re* Mark Resolved --- I'm developing on Windows 7, but using Dreamweaver to build my page. I got the script template from a PHP textbook and have been tinkering with it as I learn PHP, but without much error feedback, I'm not having much luck. :/ – Erika Electra Sep 14 '13 at 09:15
  • added error_reporting(E_ALL); to the beginning of the form after – Erika Electra Sep 14 '13 at 09:19
  • What does your code echoes? 1.You will be notified on or 2.There was a problem with your e-mail and make sure you are running this script on webserver not on localhost. – Amit Singh Sep 14 '13 at 09:53
  • @Cindeselia You already declared `$from` in `$headers = "From: " . $from . "\r\n";` so you don't need it in `if (mail($to, $subject, $body, $headers, "-f " . $from))` – Funk Forty Niner Sep 14 '13 at 14:24
  • The code echoes 'There was a problem with your e-mail.' from the nested else statement inside the filter_var. So a valid e-mail passes the filter, but it does NOT pass the mail($to, $subject, ...) statement. Re: Fred-ii-, I did remove the " -f" $from and the error echo still occurs. No mail. :( – Erika Electra Sep 22 '13 at 07:33
  • Here are some related articles I found during my frantic search. I apologize for the repetition: http://stackoverflow.com/questions/14243307/php-mail-email-postemail-in-recipients and http://stackoverflow.com/questions/17533863/how-to-configure-php-to-send-e-mail and http://stackoverflow.com/questions/4633227/why-mail-fails-in-php though the causes were entirely different. – Erika Electra Sep 22 '13 at 12:18

1 Answers1

1

If you are getting your $_POST variables right, which you can check using "echo", i dont think there is any bug in your code i tried your code without the &_POST vars an it worked fine.

I presume you are using wamp or xampp and testing this on your local system. Upload the code on webserver and everything will be fine.

Amit Singh
  • 2,267
  • 4
  • 25
  • 50
  • Sorry for asking an amateurish question, but if I am FTP-uploading my code to the server (along with index.html, from which it is called), then do I need to configure some mail function somewhere? My domain is registered on register.com and hosted on awardspace.net, but beyond that I didn't configure anything like SMTP -- not sure if it is even possible or necessary. – Erika Electra Sep 14 '13 at 12:41
  • Fist of all u dont have to be sorry,none of us were born with any such knowledge. Everyone started someday and had same issues u are facing ryt now. Second I don't think configuring SMTP is a problem here.. It's just you are not getting the $_POST vars PROBABLY. Can you upload the code of index.html for further clarifications? – Amit Singh Sep 14 '13 at 13:45
  • Thanks for your kindness and support, sanki! I edited my original post with index.html code for the form and button. I also checked that SMTP was on for the host, and it is indeed active. – Erika Electra Sep 14 '13 at 14:00
  • Anytime @Cindeselia, I ran your code at my end and i didn't have to change a single line, everything was perfect. And now since you have mentioned that SMTP is also active, i really find myself unable to find out what went wrong. Anyways if you get the answer plz do post your answer here. – Amit Singh Sep 14 '13 at 14:45
  • [Here is a link that can help you with SMTP config](http://www.9lessons.info/2009/10/send-mail-using-smtp-and-php.html) – Amit Singh Sep 14 '13 at 15:22
  • Thanks for confirming that the code worked for you. The problem was, indeed, not using a $from address in the header field that comes from my hosting domain. In other words, I needed to log into my hosting service, create an email that ends in the domain name (...@domainname.com), and use that as my $from field. – Erika Electra Sep 22 '13 at 12:16
  • Thanks for the generosity :) What did you used before in $from address?? Here i used my gmail id and it worked with that too. – Amit Singh Sep 22 '13 at 14:35
  • Previous, I tried both my gmail address (valid) and example@mail.com, but I think both failed because awardspace.net, my host, requires sending email to be on the domain name. I am not sure if this varies from hosting company to hosting company. I think I might need to find a better hosting company if your experience has differed... Note: I chose awardspace because it was free. Off-topic, but I will take any recommendations you may have.. – Erika Electra Sep 23 '13 at 14:58
  • @Cindeselia : As long as its free stick to it :) – Amit Singh Sep 23 '13 at 15:13