0

I'm trying to insert some values inside a variable, using the method "post", but I'm not getting anywhere, could help me to find out the mistakes?

So when I submit my message, the email already goes, but the information is not collected...

<form class="form" action="phpmailer.php" method="post">
  <input class="font-v1" type="text" name="nome" placeholder="Nome..."><br><br>
  <input class="font-v1" type="text" name="telefone" placeholder="Telefone..."><br><br>
  <input class="font-v1" type="email" name="email" placeholder="Email..."><br><br>
  <textarea class="font-v1" name="messagem" rows="10" cols="30"
                                placeholder="Escreva sua mensagem aqui..."></textarea><br><br>
  <button class="font-v1" class="inner-border inner-outline" type="submit" name="enviar" value="Submit">
    <div class="line-button">Enviar</div>
  </button>
</form>

<?php
    
    $nome = utf8_encode($_POST['nome']);
    $telefone = utf8_encode($_POST['telefone']);
    $email = utf8_encode($_POST['email']);
    $mensagem = utf8_encode($_POST['mensagem']);
        
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'PHPMailer/src/Exception.php';
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';
    
    $mail = new PHPMailer(true);
    
    try {
    
        # Informa que o protocolo a ser utilizado é o SMTP
        $mail->isSMTP();
        # Informe o servidor SMTP
        $mail->Host       = '';
        # Habilita a autenticação por SMTP
        $mail->SMTPAuth   = true;
        # Informe o seu usuário do servidor SMTP, aqui na HOST4 é sempre o seu e-mail completo
        $mail->Username   = ''; 
        # Preencha com a senha do seu e-mail
        $mail->Password   = '';
        # Definimos a criptografia a ser utilizada tls ou ssl
        $mail->SMTPSecure = 'tls';
        # Informe a porta pode ser a 587 ou a 465
        $mail->Port       = 587;                           
    
        # Informe o e-mail remetente recomendamos que seja o mesmo e-mail da configuração do envio
        $mail->setFrom('');
        # Informe o e-mail para onde o e-mail será enviado
        $mail->addAddress('');
        # Você pode definir um e-mail de resposta
       // $mail->addReplyTo('respondaaqui@seudominio.com.br', 'Seu Nome');
    
        # Configura o e-mail para ser enviado no formato HTML
        $mail->isHTML(true);
        # Configura o Assunto do e-mail
        $mail->Subject = 'Contato pelo Site';
        # Aqui você escreve a mensagem que irá no corpo do e-mail
        $mail->Body = '
        Você recebeu uma mensagém do site, enviado por:<br>
        Nome: $nome<br>
        Telefone: $telefone<br>
        E-mail: $email<br><br>
        Mensagem:<br>$mensagem
        ';
        
        # Você pode definir um texto alternativo para clientes de e-mail que não suportem HTML
        $mail->AltBody = 'Mensagem em texto simples';
    
        # Código para realizar o e-mail
        $mail->send();
        
        #em caso de sucesso você pode mostrar uma mensagem ou redirecionar o fluxo para outro lugar
        echo 'E-mail enviado com sucesso';
    
    } catch (Exception $e) {
    
        # Caso ocorra algum problema o script cairá aqui
        echo "O e-mail não pode ser enviado: {$mail->ErrorInfo}";
    }
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Basically, your PHP code is probably running when the page is first loaded. You need to make it check if the form was submitted, so you can only run it when there is some data to process. – ADyson Oct 11 '22 at 22:17
  • Problem solved. Thanks! Although I cant put my arrays in UTF-8. $nome = utf8_decode($_POST['nome']); $telefone = utf8_encode($_POST['telefone']); $email = utf8_encode($_POST['email']); $mensagem = utf8_encode($_POST['msg']); when I check my email - my email is coming like this:
    Message: Olá, this é a new test!
    – AndreBoeng Oct 12 '22 at 13:51
  • It's not really clear exactly what you mean by that, but if you have a new problem, please ask a new question about it and explain the problem fully. Then the whole community can assist with it. – ADyson Oct 12 '22 at 13:54
  • TBH It's unclear why you're encoding and decoding all this stuff by hand anyway, it shouldn't be necessary for utf8 support really. – ADyson Oct 12 '22 at 13:54
  • https://stackoverflow.com/questions/279170/utf-8-all-the-way-through is worth reading – ADyson Oct 12 '22 at 13:55

0 Answers0