Errors when connecting to gmail SMTP using Phpmailer

Asked

Viewed 813 times

4

Hello,

I am trying to email using Phpmailer but am having some errors. First errors with the SMTP class not located. I found that I need to create a require for aruqivo Phpmailerautoload.php in my code. After this, problems such as "SMTP connect failed" started to occur. I checked the git manual, and the list of bugs that might be but nothing fixed my problem. This is my code:

<?php

    $txtName    = "Bruno";
    $txtAs    = "As";
    $txtEmail    = "Text mail";
    $txtMensage    = "Text Body";
    $mensageBody         = "<b>Name:</b> ".$txtName." <br><b>As:</b> ".$txtAs."<br><b>Message:</b> ".$txtMensage;

    require 'phpmailer/PHPMailerAutoload.php';
    require 'phpmailer/class.smtp.php';

    function smtpmailer($to, $from, $nameDes, $as, $body) {
        global $error;
        $mail = new PHPMailer();

        $mail->IsSMTP();
        $mail->SMTPDebug = 2;
        $mail->SMTPSecure = 'tls';
        $mail->Host = 'smtp.gmail.com';
        $mail->Port = 587;
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = true;
        $mail->Username = '[email protected]';
        $mail->Password = 'pass';
        $mail->SetFrom($from, $nameDes);
        $mail->Subject = $as;
        $mail->Body = $body;
        $mail->AddAddress($to);
        $mail->IsHTML(true);

        if(!$mail->Send()) {
            $error = "<font color='red'><b>Mail error: </b></font>".$mail->ErrorInfo;
            return false;
        } else {
            $error = "<font color='blue'><b>Mensagem enviada com Sucesso!</b></font>";
            return true;
        }
    }

     if (smtpmailer('[email protected]', '[email protected]', $txtName, $txtAs, $mensageBody)) {
         Header("location: sucesso.php");
     }
     if (!empty($error)) echo $error;

    ?>

Below are images of the project tree and the error that is happening:

Project Tree Erro

Look forward to!

I used Telnet to see the smtp and returned this: 220 smtp.gmail.com ESMTP h25sm15994499qtc.38 - gsmtp

  • 1

    Just one remark, no longer use password connection with Google SMTP. You even have the option to enable "less secure apps" in the account panel to allow this, but modern applications must use XOAUTH2, with individual permission per application (it would have been much simpler if Google had allowed individual passwords per service, but opted for the proprietary path to have more control over the ecosystem).

  • Got it, but how can I configure the code to use this type of authentication? Is there any way? I noticed that has the port 25 and the port 465 also, but I tried the telnet in both but does not connect.

  • See if you have more details on $mail->errorMessage();, or some kind of debug, to see the exact cause of the error. The ports are 587 for TLS, 465 for SSL and 25 "dry".

  • see if using $mail = new PHPMailer( true ); debug help (I don’t know if this parameter still works)

  • I put true as parameter and added $mail->Smtpdebug = 2; I got the following message:

  • 2016-09-20 14:11:08 SMTP ERROR: failed to connect to server: (0) 2016-09-20 14:11:08 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Fatal error: Uncaught Exception 'phpmailerException' with message 'SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting' in C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php:1487 Stack trace: #0 C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php(1323): Phpmailer->smtpSend('Date: Tue, 20 S...', '<Strong>This is...') #1

  • C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php(1203): Phpmailer->postSend() #2 C: xampp htdocs Cucamanager Ws usuario sendemail.php(34): Phpmailer->send() #3 {main} thrown in C: xampp htdocs Cucamanager Ws usuario phpmailer class.phpmailer.php on line 1487

  • Yeah, it still worked. Better to go back to the way you were and try to figure out what the problem is that keeps you from connecting even. Might be some silly little thing. This test you did with telnet was on the same machine that runs PHP?

  • Yes, it was on the same machine. I am in the company network, but here is released access to gmail, I do not know if this can be the problem.

Show 4 more comments

1 answer

0

Opa,

I found the problem! I used the example that the site Locaweb posted to create the connection with Phpmailer, but it was with error when authenticating. I checked that the user and password were correct so I noticed that I needed to activate the configuration in gmail.

inserir a descrição da imagem aqui

Still getting error I decided to add the following property and it worked!

$mail->Smtpsecure = 'tls';

Follow the link to the example: http://wiki.locaweb.com.br/pt-br/Enviar_e-mails_pelo_PHP_usando_o_PHPMailer

Vlw!!

Browser other questions tagged

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