0

I am trying to make an extremely simple form but it is not working for me. The point is a form where people write only their names and surnames, press "send" and done. I have writen the code below:

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

        <p>
          <label>name</label><br>
          <input name="name" placeholder="name here"><br><br>

          <label>surname</label><br>
          <input name="name2" placeholder="surname here"><br><br>

        </p>
        <p>
          <input id="submit" name="submit" type="submit" value="send it!"><br>
        </p>

            <?php
                $name = $_POST['name'];
                $name2 = $_POST['name2'];
                $from = 'website.se'; 
                $to = '[email protected], [email protected]'; 
                $subject = 'new person is coming';


                $headers = "MIME-Version: 1.0" . PHP_EOL;
                $headers .= "From: $from". PHP_EOL;
                $headers .= "Content-type: text/html;charset=UTF-8 ". PHP_EOL;

                $body = "<strong>From:</strong><br><br> $name $name2<br><br> <strong>Count on me, I want to come!</strong>";


                if ($_POST['submit'] && $name != '' && $name2 != '') {
                        if (mail ($to, $subject, $body, $headers)) { 
                        echo '<p>Your name has been sent</p>';
                    } else { 
                        echo '<p>You need to fill up all fields</p>'; 
                    } 
                }

            ?>

      </form>

What happens is that besides no e-mail is sent to me, I get the error message (the one specified here: else { echo '<p>You need to fill up all fields</p>'; } ) instead of the success message that I specify on the code. Can some one give me a hint of what is wrong?

Thanks very much in advance!

7
  • 2
    What error message you get Commented Jan 19, 2014 at 15:22
  • What isn't working? Can you give us an error message? We've going to need more information! Commented Jan 19, 2014 at 15:23
  • Enable error reporting in PHP Commented Jan 19, 2014 at 15:23
  • The one I specified here: else { echo '<p>You need to fill up all fields</p>'; } Commented Jan 19, 2014 at 15:23
  • try to check your post variables with isset() Commented Jan 19, 2014 at 15:25

3 Answers 3

3

From http://php.net/manual/en/function.mail.php

mail() returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

You are getting a false back from mail() which means that the message is being rejected by whatever PHP is set up to use.

You can temporarily use the following to enable all errors and print those errors to the screen to give you an idea of what is failing.

error_reporting(E_ALL);
ini_set('display_errors', true);

PHP will throw a notice such as the following which you can use to debug:

NOTICE (5): Unexpected Error: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at "ip " port portip, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() (# 2).

Please remember, just because mail is accepted for delivery does not mean that it will arrive.

Sign up to request clarification or add additional context in comments.

7 Comments

I just did that and I get the "Undefined index" errors on the lines $name = $_POST['name'];, $name2 = $_POST['name2']; and if ($_POST['submit'] && $name != '' && $name2 != '') {
@viriato See the first part of my answer.
@PezCuckow sure: "Notice: Undefined index: name in /customers/7/9/7/forall.se/httpd.www/index_rost.php on line 228 Notice: Undefined index: name2 in /customers/7/9/7/forall.se/httpd.www/index_rost.php on line 229 Notice: Undefined index: submit in /customers/7/9/7/forall.se/httpd.www/index_rost.php on line 242"
undefined index are warnings/notices, no errors. BEcause before you submit the form, the POST are not set
You can ignore the undefined index errors for now (though you should probably fix that see @jeroen's post), what error are you getting from mail when you submit the form?
|
1

Currently when you just load the form (and before you submit anything) your entire code block is being run.

You should wrap your whole mail section in a block that is only exectuted when a POST request is made:

if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
   $name = $_POST['name'];
   // ...

}

That way you will not get any warnings about unset indices, etc.

About the mail problem, the mail() function returns false, so your message is not accepted for delivery. That could be any number of things on the server setup, but it could also be something simple, like you not specifying a valid e-mail address.

You should try changing:

$from = 'website.se'; 

to:

$from = '[email protected]';

4 Comments

But how do I do if I want to have a name only on $from?
@viriato You would need to set up an address that automatically deletes the mails, like [email protected].
I meant a name, like a name of a website or my own name
@viriato That would probably be something like Your Name <[email protected]>
0

Well it's pretty easy what is happening. I just formated it properly and... look what you're doing:

if ($_POST['submit'] && $name != '' && $name2 != '') {
    if (mail ($to, $subject, $body, $headers)) { 
        echo '<p>Your name has been sent</p>';
    } else { 
        echo '<p>You need to fill up all fields</p>'; 
    } 
}

All fields are set correctly, but mail() returns false.

Correct code would be:

if ($_POST['submit'] && $name != '' && $name2 != '') {
    if (mail ($to, $subject, $body, $headers)) { 
        echo '<p>Your name has been sent</p>';
    } else { 
        echo '<p>Mail could not be sent</p>'; 
    } 
} else { 
    echo '<p>You need to fill up all fields</p>'; 
}

3 Comments

Sorry, could you develop a little more? I don't understand where is my mistake...
Your message is not echoed if the form is empty, but if your mail could not be sent.
the question is, WHY can't the mail be sent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.