PHP mail does not send email

Asked

Viewed 183 times

0

I’ve been finding a problem in making a contact form, because I’m not succeeding and I don’t know why it doesn’t work.

Follow applied code:

HTML

<form name="sentMessage" id="contactForm" method="POST" action="contact_me.php" novalidate>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="SEU NOME*" id="name" required data-validation-required-message="Seu nome faltou.">
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="email" class="form-control" placeholder="SEU E-MAIL*" id="email" required data-validation-required-message="Por favor, adicione um email.">
                <p class="help-block text-danger"></p>
            </div>
            <div class="form-group">
                <input type="tel" class="form-control" placeholder="SEU TELEFONE*" id="phone" required data-validation-required-message="Por favor, adicione um telefone.">
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <textarea class="form-control" placeholder="SUA MENSAGEM*" id="message" required data-validation-required-message="Por favor, adicione uma mensagem."></textarea>
                <p class="help-block text-danger"></p>
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="col-lg-12 text-center">
            <div id="success"></div>
            <button type="submit" class="btn btn-xl anime">ENVIAR MENSAGEM</button>
        </div>
    </div>
</form>

PHP

<?php 

if (!$_POST['submit'])
{
    $quebra_linha = "\n"; 
    $emailsender = "[email protected]"; 
    $nomeremetente = $_REQUEST['name']; 
    $emaildestinatario = "[email protected]"; 
    $assunto = "[CONTATO] SITE RESPONSAVEL"; 
    $mensagem = $_REQUEST['message']; 
    $email = $_REQUEST['email']; 
    $phone = $_REQUEST['phone'];

    $mensagemHTML = ' Olá, 
    tenho uma nova mensagem para você diretamente do site!

    Nome: '.$nomeremetente.' Assunto: '.$empresa.' E-mail: '.$email.' Telefone: '.$phone.' Mensagem: '.$mensagem.'';

    $headers = "MIME-Version: 1.1".$quebra_linha;
    $headers = "Content-type: text/html; charset=iso-8859-1".$quebra_linha; 
    $headers = "From: ".$emailsender.$quebra_linha; 
    $headers = "Reply-To: ".$emailsender.$quebra_linha;

    if (mail($emaildestinatario, $assunto, $mensagemHTML, $headers, "-r". $emailsender)) {
        echo ""; 
        echo ""; 
    } else { 
        echo ""; 
    } 
} 
?>

Apparently the action="" is not redirecting when I press the button submit. I tried with Phpmailer too, but I find the same problem. The index was saved as index.php and I tried using PHP_SELF, but I wasn’t very successful either.

  • What error do you see? Or you’re not passing values?

  • Tip: In PHP there is a constant called PHP_EOL which can be used for line breaking.

  • No error is shown as it does not redirect to action;

  • 1

    Considering the existence of attributes data-validation-required-message you must be using some JS library to validate the form. If so, what is it? Generally these libraries acting on the event submit form and, if somehow you are returning false, the request is cancelled.

  • 1

    !$_POST['submit']? Would not be if($_POST['submit']) or if(isset($_POST['submit']))? See in the browser F12 which is the answer and if the request is sent as you want, so you can isolate the problem, whether it is in the client or the server.

  • 1

    Of the one name to the submit, and puts in the if(isset($_POST['nomedosubmit'])){echo "Passou";}

  • If you do not change the end of the browser url to contact_me.php, then there is javascript blocking the form submission as reported by @Andersoncarloswoss, if you change the url, then just follow the comments of Inkeliz, Max Rogério and replies sent.

Show 2 more comments

2 answers

5

There is no input with Submit name in your HTML. So, if will never run. In fact, none of its inputs has the attribute "name". Capturing a field sends via post is done through the name of this field. Example

<form method="post>
   <input type="text" name="nome_do_campo"/>
   <input type="submit" value="Enviar"/>
</form> 

PHP would take the field up this way:

<?php
$campo = $_POST['nome_do_campo'];
?>

<?php
//Se quisermos executar o código só se o POST tiver sido feito:
if(isset($_POST['nome_do_campo']))
{
   $campo = $_POST['nome_do_campo'];
}
?>
  • Has a <button type="Submit" class="btn btn-Xl anime">SEND MESSAGE</button> in its form. Works the same way as input.

  • If his form has not been javascript-barred, then the problem is that if of the beginning assumes that there is a $_POST['Submit'] variable and takes the opposite value , when the correct one would be for a name="Submit" in the html form button and check if it exists using if(isset($_POST['Submit'])) in php script.

0

SUMMARY OF THE OPERA:

In PHP take the negation sign (exclamation mark) before $_POST.

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

Modifications to the HTML Code:

Name all inputs so that PHP can get the HTML form data

<form name="sentMessage" id="contactForm" method="POST" action="contact_me.php" novalidate>
<div class="row">
    <div class="col-md-6">
        <div class="form-group">
            <input type="text" class="form-control" placeholder="SEU NOME*" name="name" id="name" required data-validation-required-message="Seu nome faltou.">
            <p class="help-block text-danger"></p>
        </div>
        <div class="form-group">
            <input type="email" class="form-control" placeholder="SEU E-MAIL*" name="email" id="email" required data-validation-required-message="Por favor, adicione um email.">
            <p class="help-block text-danger"></p>
        </div>
        <div class="form-group">
            <input type="tel" class="form-control" placeholder="SEU TELEFONE*" name="phone" id="phone" required data-validation-required-message="Por favor, adicione um telefone.">
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="col-md-6">
        <div class="form-group">
            <textarea class="form-control" placeholder="SUA MENSAGEM*" name="message" id="message" required data-validation-required-message="Por favor, adicione uma mensagem."></textarea>
            <p class="help-block text-danger"></p>
        </div>
    </div>
    <div class="clearfix"></div>
    <div class="col-lg-12 text-center">
        <div id="success"></div>
        <input type="submit" name="submit" value="vete" class="btn btn-xl anime">
    </div>
</div>
</form>
  • That name="Submit" you put that made a difference. But you should have checked with if( isset($_POST['submit']) ) . If you have used button with type="Submit" or input type="Submit" it is the same. You answered the question correctly but slipped in this detail. I thought it was unfair that you received two negatives in your answer because you identified the problem correctly. I gave a +1 to decrease the injustice.

  • Thanks Antonio Alexandre. What happens is that many vote negative and I think they even do some experiment or post something to help. I simply took the trouble to put it online on my server and test every way until it gave a satisfactory result. And believe it the way I posted it with button not funfa nor the stick!!! Will be missing some library?

  • It is good to know that anyone who does any page to post on the Internet has to be aware that there are a multitude of variations (browsers, versions of browsers, etcc) that can define whether or not the funnel. Don’t put something into practice that only attracts the most up-to-date.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.