PHPMailer not taking form value

-2

So, I have a sample registry form to test PHPMailer, but PHP is not accepting form IDs with the error:

  

Undefined index

HTML and PHP are just below:

<?php

//ATENÇÃO ESSE TIPO DE ENVIO SOMENTE PARA USO DO GMAIL
//PARA USOS DE OUTROS DOMINIOS UTILIZA-SE OUTRAS CONFIGURAÇÕES

//pegando valores do FORMULARIO DE CONTATO
$nome       = $_POST ["nome"];
$email      = $_POST ["email"];
$mensagem   = $_POST ["mensagem"];

// Importando as Clases do PHPMailer

include("phpmailer\src\class.phpmailer.php");

require 'phpmailer\src\PHPMailerAutoload.php';


$mail = new PHPMailer(true);                              // Passando "true" para o nova class $mail
try {
    //CONFIGURAÇÕES DO SERVER
    $mail->SMTPDebug = 2;                                 // Habilitando
    $mail->isSMTP();                                      // setando o  SMTP
    $mail->Host = 'smtp.gmail.com';                       // Especificando o endereço e o tipo de SMTP servers
    $mail->SMTPAuth = true;                               // Habilitando
    $mail->Username = 'samuel******[email protected]';                 // SMTP e-mail do GMAIL
    $mail->Password = '*********';                           // SENHA DO GMAIL
    $mail->SMTPSecure = 'tls';                            // Habilitando o tipo de criptografia tls
    $mail->Port = 587;                                      // Conectando a porta TCP   

    //E-MAILS RECIPIENTES
    $mail->setFrom('[email protected]', 'Usuario');  
    $mail->addAddress('$email); 

    //CONTEÚDO
    $mail->isHTML(true);                                  // Setando o e-mail para formato HMTL
    $mail->Subject = 'Teste PHPMailer';
    $mail->Body    = 'Essa é uma Mesangem de Teste <b>in bold!</b>';
    $mail->AltBody = 'O corpo deste e-mail esta em formato HTML';

    $mail->send();
    echo 'Mensagem Enviada com Sucesso!';
} catch (Exception $e) {
    echo 'Mensagem não enviada. Erro: ', $mail->ErrorInfo;
}
<!DOCTYPE html>
<html>
    <head>
        <title> Teste de envio dos dados </title>
    </head>
    <body>
        <h4 align="center"> Formulário de Contato </h4>
            <form action="index.php" method="post" align="center">
            <br>
            <div>
                <label>NOME</label>
                <input type="text" id="nome">
            </div>
            <br>
            <div>
                <label>E-MAIL</label>
                <input type="text" id="email">
            </div>
            <br>
            <div>
                <label>MENSAGEM</label>
            </div>
            <textarea rows="10" cols="30" id="mensagem"></textarea>
            <br><br>

            <input type="submit">
        </form>
    </body>
</html>
    
asked by anonymous 06.09.2018 / 20:23

2 answers

1

The name of a input passed in a POST is defined by the name attribute and not by the id attribute.

The id attribute is an identifier within the document, but this identifier is not sent in requests, but rather the name attribute and its value value).

Then your inputs would change from:

 <input type="text" id="nome">

To:

 <input type="text" name="nome">
    
06.09.2018 / 20:58
2

Fixes

As it was, it would not work because of space, because $_POST is an Array and thus is wrong: $_POST ["nome"];

To fix changed by filter_input so help against SQL Injection:

$nome = filter_input(POST, 'nome');

I've added the properties in SMTP , because depending on your server, you can avoid certain errors:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )   
);

I commented on the line # $mail->AltBody = 'O corpo ...'; because since you are using $mail->isHTML(true); then you will only need $mail->Body .

Contrary to what is written O corpo deste e-mail esta em formato HTML , it would be "... NOT in HTML".

In the HTML part, the name attribute was missing in its inputs , correcting by getting:

<input type="text" name="nome">
<input type="text" name="email">
<textarea rows="10" cols="30" name="mensagem">

* As said by @fernandosavio, you can also keep the id attribute, if you use it, if not, better take it as it is not useful.

Final script

$nome     = filter_input(POST, 'nome');
$email    = filter_input(POST, 'email');
$mensagem = filter_input(POST, 'mensagem');

include 'phpmailer\src\class.phpmailer.php';
require 'phpmailer\src\PHPMailerAutoload.php';

$mail = new PHPMailer(true);                              
try {
    $mail->SMTPDebug = 2;  
    $mail->isSMTP(); 
    $mail->Host = 'smtp.gmail.com';    
    $mail->SMTPAuth = true;
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )   
    );
    $mail->Username = 'samuel******[email protected]';    
    $mail->Password = '*********';               
    $mail->SMTPSecure = 'tls'; 
    $mail->Port = 587; 
    $mail->setFrom('[email protected]', 'Usuario');  
    $mail->addAddress('$email'); 

    $mail->isHTML(true);
    $mail->Subject = 'Teste PHPMailer';
    $mail->Body    = 'Essa é uma Mesangem de Teste <b>in bold!</b>';
    # $mail->AltBody = 'O corpo deste e-mail esta em formato HTML';

    $mail->send();
    echo 'Mensagem Enviada com Sucesso!';

} catch (Exception $e) {

    echo 'Mensagem não enviada. Erro: ', $mail->ErrorInfo;
}
<!DOCTYPE html>
<html>
    <head>
        <title> Teste de envio dos dados </title>
    </head>
    <body>
        <h4 align="center"> Formulário de Contato </h4>
            <form action="index.php" method="post" align="center">
            <br>
            <div>
                <label>NOME</label>
                <input type="text" name="nome">
            </div>
            <br>
            <div>
                <label>E-MAIL</label>
                <input type="text" name="email">
            </div>
            <br>
            <div>
                <label>MENSAGEM</label>
            </div>
            <textarea rows="10" cols="30" name="mensagem"></textarea>
            <br><br>

            <input type="submit">
        </form>
    </body>
</html>
    
06.09.2018 / 20:57