Errors connecting to SMTP from gmail using PHPMailer

3

Hello,

I'm trying to send email using PHPMailer however I'm having some errors. First errors with the SMTP class not found. I discovered that I need to create a require for the PHPMailerAutoload.php file in my code. After this, problems such as "SMTP connect failed" began to occur. I checked the git manual, and the list of errors that may be however nothing solved 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 occurring:

I await!

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

    
asked by anonymous 20.09.2016 / 15:31

1 answer

0

Opa,

I found the problem! I used the example that the site Locaweb posted to create the connection with PhpMailer, but I was wrong to authenticate. I checked that the username and password were correct so I noticed that I needed to enable the configuration in gmail.

StillgettinganerrorIdecidedtoaddthefollowingpropertyanditworked!

  

$mail->SMTPSecure='tls';

Followthelinkfortheexample: link

Vlw !!

    
20.09.2016 / 17:21