I do not receive email via Phpmailer

Asked

Viewed 799 times

0

I’m using Phpmailer to send a message via form, but it’s not working. I’d like to know where I’m going wrong. Remember that the email that will receive the form information is Gmail. Follow my code for better understanding.

HTML:

<form id="main-contact-form" name="contact-form" action="mail.php" method="post">
    <div class="row  wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
        <div class="col-sm-6">
            <div class="form-group">
                <input type="text" name="name" class="form-control" placeholder="Nome" required="required">
            </div>
        </div>
        <div class="col-sm-6">
            <div class="form-group">
                <input type="email" name="email" class="form-control" placeholder="E-mail" required="required">
            </div>
        </div>
    </div>
    <div class="form-group">
        <input type="text" name="subject" class="form-control" placeholder="Assunto" required="required">
    </div>
    <div class="form-group">
        <textarea name="message" id="message" class="form-control" rows="4" placeholder="Escreva sua Mensagem" required="required"></textarea>
    </div>                        
    <div class="form-group">
        <button type="submit" class="btn-submit">Enviar</button>
    </div>
</form>   

PHP:

    <?php
$Nome       = $_POST["name"];   // Pega o valor do campo Nome
$Email       = $_POST["email"];   // Pega o valor do campo Telefone
$Assunto      = $_POST["subject"];  // Pega o valor do campo Email
$Mensagem   = $_POST["message"];   // Pega os valores do campo Mensagem

// Variável que junta os valores acima e monta o corpo do email

$Vai        = "Nome: $Nome\n\nE-mail: $Email\n\nAssunto: $Assunto\n\nMensagem: $Mensagem\n";
date_default_timezone_set('Etc/UTC');
require_once("phpmailer/class.phpmailer.php");

define('GUSER', '[email protected]');   // <-- Insira aqui o seu GMail
define('GPWD', 'senhaaaa');        // <-- Insira aqui a senha do seu GMail

function smtpmailer($para, $de, $de_nome, $assunto, $corpo) { 
    global $error;
    $mail = new PHPMailer();
    $mail->isSMTP();       // Ativar SMTP
    $mail->SMTPDebug = 2;       // Debugar: 1 = erros e mensagens, 2 = mensagens apenas
    $mail->SMTPAuth = true;     // Autenticação ativada
    $mail->SMTPSecure = 'SSL';  // SSL REQUERIDO pelo GMail
    $mail->Host = 'smtp.gmail.com';// SMTP utilizado
    $mail->Port = 465;          // A porta 587 deverá estar aberta em seu servidor
    $mail->Username = GUSER;
    $mail->Password = GPWD;
    $mail->SetFrom($de, $de_nome);
    $mail->Subject = $assunto;
    $mail->Body = $corpo;
    $mail->AddAddress($para);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Mensagem enviada!';
        return true;
    }
}

// Insira abaixo o email que irá receber a mensagem, o email que irá enviar (o mesmo da variável GUSER), 

 if (smtpmailer('[email protected]', '[email protected]', 'renata', 'Assunto do Email', $Vai)) {

    Header("location:http://www.dominio.com.br/obrigado.html"); // Redireciona para uma página de obrigado.

}
if (!empty($error)) echo $error;
?>

Folder hierarchy:

TECMOV
- css/
- fonts/
- images/
- js/
- phpmailer/
- index.php
- mail.php
  • Kirito, beware of posting code with original user and password, always try to rename to something that does not compromise the security of your information, we will believe that no one has seen and that no one will access the editing history to see. change your password

  • thank you I forgot to modify it

1 answer

2


Come on:

  • The door of SMTP from Gmail is not 25, but 465 (SSL) or 587 (TLS);
  • If this email has two-step authentication, you should disable it;
  • You have a limit of 2000 messages per day;
  • In the Phpmailer documentation, there is an example of sending via Gmail;
  • $mail->SMTPDebug = 2; does not mean what is in your comment, but what is in the documentation:

$SMTPDebug:

SMTP class debug output mode.

Debug output level. Options:

  • 0 In the output
  • 1 Commands
  • 2 Data and Commands
  • 3 As 2 plus Connection status
  • 4 Low-level data output

I hope I’ve helped

  • Hello friend thanks for the post just did not understand the second step

  • friend made the pssao to apasso and edited my document but now appears this error: Fatal error: Class 'SMTP' not found in C: xampp htdocs TECMOV phpmailer class.phpmailer.php on line 1439

  • 1

    The second step refers to two-step authentication (you type in the email password and then enter a code, which was sent via SMS, app, or other mode). If the email used to send has this authentication enabled, you should disable it. About the error you mentioned, you changed something you shouldn’t... Post the updated code, maybe?

  • I’ll do it now from a glance at this updated code that I will post in a few moments

  • ready this ai the edited code I took a look also at the link Voce sent me and tbm I changed my php.ini went in the smtp_port line it was in 25 and put it and 465 more memo so it appears this error--- Fatal error: Class 'SMTP' not found in C:xampp htdocs TECMOV phpmailer class.phpmailer.php on line 1439

  • 1

    You must make the require of PHPMailerAutoload.php to resolve this error, you must also disable two-step authentication that is active in this email to be able to use the script. Consider following example of Phpmailer itself

  • 2-step authentication in my gmail it gets into configurations and this ?

  • friend worked I did a test and it worked but when I sent 2 second form no longer worked gave the following error:Mail error: SMTP Connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

  • I managed to arrange thanks for your help Here’s your answer as accepted ^^^

  • @Kirito good to know it worked!

Show 5 more comments

Browser other questions tagged

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