How to validate SMTP credentials before sending email in PHP?

I want check smtp credentials is valid or not before sending mail is it possible in php , below my code not works and not show any error. please correct me if i am wrong.

Since it’s so hard to find an answer, I’m thinking this might be impossible. If so, I’d like confirmation that it’s impossible.

<?php
use PHPMailer\PHPmailer\PHPMailer; 
include_once "./PHPMailer/PHPMailer.php";

include_once "./PHPMailer/SMTP.php";

$mail = new PHPMailer(true);
$mail->SMTPAuth = true;
$mail->Username = 'email@example.com';
$mail->Password = 'my_awesome_password';
$mail->Host = 'smtp.example.com';
$mail->Port = 465;

// This function returns TRUE if authentication
// was successful, or throws an exception otherwise
$validCredentials = $mail->SmtpConnect();
$validCredentials = false;

try {
    $validCredentials = $mail->SmtpConnect();
}
catch(Exception $error) {
echo "Not connected";
}
?>

I don’t really get what you are trying to do. The comment says that SmtpConnect() does what you want and throws an exception - so you should see any error that occurs directly on screen, or in the error.log of your webserver. Two lines later you are using this function again, but that wont run if the first one already throw an error. And you are not even outputting any information from catched exception. Also the documentation tells you something about error information

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

Is it possible to check or smtp connect before sending mail in PHPMAILER?

your comment says SmtpConnect() does this.

1 Like

I try to do that, I don’t know it have or not I just seen in there was smtpConnect in PHPMailer lab.
Please help if you know, don’t question, I am wrong so correct me don’t questioning, Why I asked because I don’t know… and again you asking… where is moderator??? :cry::cry::cry:

<offtopic>

Modeators are on these forums to make sure discussions stay civil and to delete spam. They are not here to help everyone with their problems - that’s not up to them.

</offtopic>

As for your question, your comment is correct

// This function returns TRUE if authentication
// was successful, or throws an exception otherwise

but then your code goes and does something else instead :man_shrugging:

1 Like
// This function returns TRUE if authentication #This statement is true.
// was successful, or throws an exception otherwise #This statement is false.
$validCredentials = $mail->SmtpConnect(); // Sets $validCredentials to true or false.
$validCredentials = false; // Completely destroys the value returned on the last line.

try {
    $validCredentials = $mail->SmtpConnect(); 
}
catch(Exception $error) { //Returning False is NOT an Exception.
echo "Not connected";
}

EDIT: Sorry, the function CAN throw exceptions, but its normal returns are boolean true/false.

Thank you, My code was correct some guidance was I need.
@m_hutley When i use valid credentials then not show any message but some invalid credentials I want to check it show long error message

Fatal error: Uncaught PHPMailer\PHPMailer\Exception: SMTP Error: Could not authenticate. in /home/username/public_html/PHPMailer/PHPMailer.php:1898 Stack trace: #0 /home/username/public_html/smtp.php(19): PHPMailer\PHPMailer\PHPMailer->smtpConnect() #1 {main} thrown in /home/username/public_html/PHPMailer/PHPMailer.php on line 1898
I want to show error in one line like , if password not matched show authentication failed if connected then show credentials match.

then use the try/catch mechanism

https://www.php.net/manual/en/language.exceptions.php

Try/catch, and then an if to check if the result came back false (or stick the if inside your try, and have it throw an exception if it finds the result was false.)

Just to add (in case it clarifies anything), while the code posted has got a try/catch around one call to smtpConnect(), it doesn’t have one around the first call to that function, and I expect it’s that call which is throwing the “uncaught exception” error.

When I use

if($validCredentials = false) {
   echo "Failed";
} else { echo "connection done";}

But it show same error.
I don’t know how to fix this problem, please help me with shared correct code

you already had that code

try {
    $validCredentials = $mail->SmtpConnect();
}
catch(Exception $error) {
echo "Not connected";
}
```

No, she didnt. Swear i’m talking to a wall sometimes…

try {
    $validCredentials = $mail->SmtpConnect();
    if($validCredentials === false) { throw new Exception("Returned False"); }
}
catch(Exception $error) {
echo "Not connected";
}

Returning false is not the same as throwing an exception, so you have to check for it separately.
(large enough now?)

still same issue

Show the current code, please.

<?php
use PHPMailer\PHPmailer\PHPMailer; 
include_once "./PHPMailer/PHPMailer.php";

include_once "./PHPMailer/SMTP.php";

$mail = new PHPMailer(true);
$mail->SMTPAuth = true;
$mail->Username = 'email@example.com';
$mail->Password = 'my_awesome_password';
$mail->Host = 'smtp.example.com';
$mail->Port = 465;

// This function returns TRUE if authentication
// was successful, or throws an exception otherwise
$validCredentials = $mail->SmtpConnect();
$validCredentials = false;

try {
    $validCredentials = $mail->SmtpConnect();
    if($validCredentials === false) { throw new Exception("Returned False"); }
}
catch(Exception $error) {
echo "Not connected";
}
?>

and

if($validCredentials === false) {  echo "not connected" } else { echo "connected"}

But both not works

Define “not works”.

You still have that first call to smtpConnect() that isn’t part of your try-catch trapping.

// This function returns TRUE if authentication
// was successful, or throws an exception otherwise
$validCredentials = $mail->SmtpConnect();
$validCredentials = false;

so it will still throw an exception.

@droopsnoot thanks now it works, thank you I was calling the function first that is why it happens , but is it possible to do without try snd catch…!