Luis Uribe
Luis Uribe

Reputation: 21

problems with server SMTP PHPMailer

I have a problem with phpmailer, I have the correct SMPT server data and also the username and password, but it still marks me an error in the credentials, this is my code

  <?php 
include ("class.phpmailer.php");
include ("../vendor/phpmailer/phpmailer/PHPMailer.php");
include ("../vendor/phpmailer/phpmailer/src/SMTP");
 // Import PHPMailer classes into the global namespace
    // These must be at the top of your script, not inside a function
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require '../vendor/phpmailer/phpmailer/src/Exception.php';
    require '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
    require '../vendor/phpmailer/phpmailer/src/SMTP.php';
    require '../vendor/autoload.php';

    $name = trim(stripslashes($_POST['contactName']));
    $email = trim(stripslashes($_POST['contactEmail']));
    $subject = trim(stripslashes($_POST['contactSubject']));
    $contact_message = trim(stripslashes($_POST['contactMessage']));


    // Load Composer's autoloader


    // Instantiation and passing `true` enables exceptions

    $mail = new PHPMailer(true);
    try {

     //Server settings
    $mail->SMTPDebug = 2;                                       // Enable verbose debug output
     $mail->isSMTP();                                            // Set mailer to use SMTP
     $mail->Host       = 'smtp.gmail.com';  // Specify main and backup SMTP servers
     $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
     $mail->Username   = '[email protected]';                     // SMTP username
     $mail->Password   = '******';                             // SMTP password
     $mail->SMTPSecure = 'TSL';                                  // Enable TLS encryption, `ssl` also accepted
     $mail->Port       = 587;                                    // TCP port to connect to


        //Recipients
        $mail->setFrom('[email protected]');
        $mail->addAddress($email, $name);     // Add a recipient


        // Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $contact_message;
        $mail->AltBody = $contact_message;

        $mail->send();
       echo "Mensaje enviado";
    } catch (Exception $e) {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }


?>

and this is the error that marks me SMTP Error: Could not authenticate. Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.

Upvotes: 0

Views: 94

Answers (1)

zedfoxus
zedfoxus

Reputation: 37099

There is a line where SMTPSecure has to be specified. You have that line, but tls has been mistyped as TSL. Let's change that:

Change

$mail->SMTPSecure = 'TSL'; 

to

$mail->SMTPSecure = 'tls'; 

Additionally, you should add an if/then surrounding $mail->send(); like so:

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

If the mail is not sent, your if statement will catch that and report the error. See an example of such error handling in PHPMailer example: https://github.com/PHPMailer/PHPMailer/blob/master/examples/gmail.phps

Upvotes: 1

Related Questions