Home > database >  PHPmailer unable to connect
PHPmailer unable to connect

Time:04-19

I've been trying to get PHPmailer to work but I stumble constantly on the same error. I made a gmail account and normally all settings should be right.

I get the following error: Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

This is the code I'm currently using:

<?php 

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 {
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'mymail.com';
    $mail->Password   = 'mypassword';
    $mail->SMTPSecure = 'ssl';encryption
    $mail->Port       = 465;

    $mail->setFrom('mymail', 'Dev4');
    $mail->addAddress('anothermail');
    $mail->addReplyTo('[email protected]', 'Information');

    $mail->isHTML(true);
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

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

CodePudding user response:

You just need to fix the following two lines...

    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;

and add

    $mail->Mailer = 'smtp';

CodePudding user response:

He you can use that : https://github.com/Liam-Nothing/PhpMailer-gmail-no-composer is preprogramming for php mailer using GMAIL.

  • Related