Does PHP mailer work on localhost or is there something wrong here?
P粉969253139
P粉969253139 2024-02-25 17:46:09
0
1
429

I keep getting an error message when trying to send an email using the PHP mailer (localhost). Or the php mailer won't 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 I get:

Server-> Client: SMTP error: Unable to connect to SMTP host. Message could not be sent. Mailer Error: SMTP Error: Unable to connect to SMTP host.

P粉969253139
P粉969253139

reply all(1)
P粉364129744

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

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

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

This may not solve your entire problem, as Gmail (as of May 2022) no longer allows authentication using a regular ID and password. You'll need to switch to using XOAUTH2 (supported by PHPMailer), or create an application password in the Gmail console.

Also note that gmail does not allow you to use any address, only your username address and predefined aliases.

All of this is covered in the

PHPMailer Troubleshooting Guide.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template