-1

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

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $to = 'mymail@gmail.com'; 
    $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      = '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);
?>

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      = '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);
?>

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
  • sorry for 'nombre'. i'm argentinian and i was making the code in spanish and i'm used to declare variables in spanish. On the other hand, changing it and putting 'name' correctly doesn't work.I tried issets and that didn't work either :( – Nico Pavlotsky Nov 05 '14 at 00:48
  • @NicoPavlotsky Not a problem, I understand. Have you gone over my entire answer, included error reporting at the top of your PHP file(s), gone through the checklist etc.? It would also be nice to see your HTML form. – Funk Forty Niner Nov 05 '14 at 00:49
  • well, wait 5 mins and i'll add the HTML to the question. For the other things, yes, I checked all lots of times but nothing solves the problem. A question now: if I add that line for debugging or so, where does the report appear? – Nico Pavlotsky Nov 05 '14 at 00:52
  • @NicoPavlotsky The error will appear on your screen. – Funk Forty Niner Nov 05 '14 at 00:52
  • If you want here's the webpage: http://sinvenderse.com/reiki/index.htm – Nico Pavlotsky Nov 05 '14 at 01:02
  • Check the form at the end of the page, the captcha does nothing, I just added it to code it with jQuery Later – Nico Pavlotsky Nov 05 '14 at 01:02
  • @NicoPavlotsky I noticed your `index.htm` file contains `` in the source. You know that it will not work, unless your file is `.php` extension. I also filled the form using `email@example.com` whether it worked or not. – Funk Forty Niner Nov 05 '14 at 01:11
  • Gonna change it and post results here... – Nico Pavlotsky Nov 05 '14 at 01:18
  • @NicoPavlotsky I also noticed ` – Funk Forty Niner Nov 05 '14 at 01:26
  • @NicoPavlotsky I also noticed your submit button is not named `` <= **very important**. – Funk Forty Niner Nov 05 '14 at 01:48
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