1

I keep getting error when trying to send email using PHP mailer (localhost). or does php mailer not work on localhost?

<?php
//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\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';


//Load Composer's autoloader
require 'vendor/autoload.php';

//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);

    try {
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
        $mail->isSMTP();                                            //Send using SMTP
        $mail->Host       = 'smtp.gmail.com';                     //Set the SMTP server to send through
        $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
        $mail->Username   = 'default@gmail.com';                     //SMTP username
        $mail->Password   = '00000120';                               //SMTP password
        // $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
        $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`

        //Recipients
        $mail->setFrom('NatsuDragneelxd42069@gmail.com', 'Mailer');
        $mail->addAddress('Received@gmail.com', 'Joe User');     //Add a recipient
        //$mail->addAddress('ellen@example.com');               //Name is optional
        $mail->addReplyTo('Noreply@gmail.com', 'Info');
        // $mail->addCC('cc@example.com');
        // $mail->addBCC('bcc@example.com');

        //Attachments
        // $mail->addAttachment('/var/tmp/file.tar.gz');         //Add attachments
        // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

        //Content
        $mail->isHTML(true);                                  //Set email format to HTML
        $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}";
    }

This is the error that I get:

SERVER -> CLIENT: SMTP Error: Could not connect to SMTP host. Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

FallenMoon
  • 11
  • 2
  • 1
    Did you set up PHP for mail sending? If not: do that first, because email isn't magic, you need to configure mail functionality before it'll work. – Mike 'Pomax' Kamermans Jun 27 '22 at 21:36
  • That's not true. Much of the reason PHPMailer exists is because you *don't* have to set up anything locally. The same is not true of PHP's built-in `mail()` command. – Synchro Jun 27 '22 at 21:55
  • Does this answer your question? [PHP mail function doesn't complete sending of e-mail](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – Tangentially Perpendicular Jun 28 '22 at 01:26

2 Answers2

0

I don't know why you commented out this line, but it will make the connection fail because it will attempt to make an unencrypted connection to a port expecting encryption:

// $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;

Uncomment that and you might have more luck. You also might want to try setting SMTPDebug = SMTP::DEBUG_CONNECTION as it will give you more info about the TLS phase of the connection.

This may not solve your whole problem because Gmail no longer (as of May 2022) allows authentication using regular ID and password. You need to either switch to using XOAUTH2 (which PHPMailer supports), or create an App Password in your gmail console.

Also note that gmail won't let you use arbitrary from addresses, only your Username address and predefined aliases.

All this is covered in the PHPMailer troubleshooting guide.

Synchro
  • 35,538
  • 15
  • 81
  • 104
0

all I need to do was fix the less secured but since it's been removed by google itself I search for an alternative less secure apps alternative

FallenMoon
  • 11
  • 2