-1

I'm making a mail form with PHP and I have something like this:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $to = '[email protected]'; 
    $subject = 'Test mail';
    $body = "From: $nombre\n E-Mail: $email";

    if ($_POST['submit']){
        mail ($to, $subject, $body);
        header("Location: http://www.example.com/");
    }
?>

As you can see the code is really simple, it's just sending a name and an adress but it doesn't work. Whenever I try to send it, the page just change the title to "mypage/send.php" and that's all.

It does not even redirect me to an example page.

I've tried:

  • Using headers (as recommended by others from here).
  • Checking if there's a grammatical error.
  • Testing it in a real page and in localhost with SMPT.

EDIT: HTML USED:

<form method="post" action="send.php">
   <input type="text" name="name" placeholder="Your name">
   <input type="email" name="email" placeholder="Email">
   <input type="submit" value="send">
</form>
  • Put this at the start of the script `ini_set('display_startup_errors',1); ini_set('display_errors',1); error_reporting(-1);` – castis Nov 04 '14 at 23:55
  • mind you, this will not rid you of problems, but it will tell you what they are. – castis Nov 05 '14 at 00:02

2 Answers2

1

Firstly, you don't have an assigned variable called $nombre so having error reporting set would have thrown an undefined variable warning; you may have meant to use $name instead.

Check your Spam since you don't have a proper From: in your header, and/or the Email is being rejected altogether due to that reason, this is a common thing nowadays.

Check that all your form elements are indeed named, this includes your submit button, since your conditional statement depends on this, and you haven't provided your HTML form.

I.e.: name="name" and name="email" and for the submit button name="submit".

It's also better to use isset(). I.e.:

if(isset($_POST['submit'])){...}

instead of if ($_POST['submit'])

For more information on mail/headers, visit:

Example from PHP.net:

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

Add error reporting to the top of your file(s) which will help find errors either.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

For more information on mail/headers, visit:

Example from PHP.net:

<?php
$to      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

A few things to note are:

  • Is PHP installed on your server and properly configured?
  • Is mail installed and properly configured?
  • If running from your own computer, all of the above apply.

"it's just sending a name and an adress but it doesn't work"

  • You will need to be more precise with this. "It doesn't work" doesn't define nor describe how it's not working, or any error messages you may have gotten.

If it isn't redirecting, then you may be outputting before header, another common mistake, which is something that error reporting will catch, do use it.

Here is an article on Stack about outputting before header (headers already sent), should you get that warning:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

You should check the value of $_POST["submit"] with var_dump and the return value from mail(which is boolean).

Obs.: It is a good pratice to filter all input coming from $_GET and $_POST.

Leandragem
  • 100
  • 1
  • 12