0

Can you try the code Please? Guess where I went wrong

    ```
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;

    require 'vendor/autoload.php';

    $mail = new PHPMailer(true);
    try {
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true


            )
        );

        $mail->SMTPDebug = 2;
        $mail->isSMTP();
        $mail->Host = "smtp.gmail.com";
        $mail->SMTPAuth = true;
        $mail->Username = "********@gmail.com";
        $mail->Password = "********";
        $mail->SMTPSecure = "tls";
        $mail->Port = 587;

        $mail->setFrom("********@gmail.com", "My name");
        $mail->addAddress("********@gmail.com");
        $mail->isHTML(true);
        $mail->Subject = "Mailer Test";
        $mail->Body = "This is a Test";

        $mail->send();
        echo "Ok";
    } catch (Exception $e) {
        echo "No";
    }
    ```

There are two duplicate errors that are written twice in output : SMTP Error: Could not authenticate. SMTP Error: Could not authenticate. what's the problem? Can you write the modify code

AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
mehmet
  • 506
  • 3
  • 12
  • 1
    Did you install PHP Mailer trough Composer in the command line? Many times when I install it I also got a messagge that I need to install also other packages to authenticate to a specific mail server, maybe this [question](https://stackoverflow.com/questions/64969226/use-php-mailer-to-send-via-gmail-smtp-error) will help you – Baracuda078 Mar 23 '21 at 14:54
  • 1
    With `$mail->SMTPDebug = 2` you must be getting much more detailed information than that – Álvaro González Mar 23 '21 at 15:06
  • Try not to use `require 'vendor/autoload.php'`. Use `require 'SMTP.php'` and `require 'PHPMailer.php'`, instead (assuming that both are in the same folder of your PHP file). – AbsoluteBeginner Apr 06 '21 at 16:34

1 Answers1

0

As I mentioned in my comment above. If I instal PHPmailer through composer it tels me I need to install also other packages for the right authentication. I installed PHPmailer today again for a new project so I copied the lines it showed me.

phpmailer/phpmailer suggests installing hayageek/oauth2-yahoo (Needed for Yahoo XOAUTH2 authentication)
phpmailer/phpmailer suggests installing league/oauth2-google (Needed for Google XOAUTH2 authentication)
phpmailer/phpmailer suggests installing psr/log (For optional PSR-3 debug logging)
phpmailer/phpmailer suggests installing stevenmaguire/oauth2-microsoft (Needed for Microsoft XOAUTH2 authentication)
phpmailer/phpmailer suggests installing symfony/polyfill-mbstring (To support UTF-8 if the Mbstring PHP extension is not enabled (^1.2))
Dharman
  • 30,962
  • 25
  • 85
  • 135
Baracuda078
  • 677
  • 1
  • 5
  • 10