Web Developer
Web Developer

Reputation: 55

How to resolve SMTP could not connect error? [post STARTTLS problem]

I have installed php mailer using composer composer require phpmailer/phpmailer. I followed instructions from this website https://www.geeksforgeeks.org/how-to-send-an-email-using-phpmailer.

Below is my code for sending email.

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';


$mail = new PHPMailer(true); 
try 
{ 
    $mail->SMTPDebug = 2;
    $mail->isSMTP(); 
    $mail->Host       = 'mail.example.in'; 
    $mail->SMTPAuth   = true;      
    $mail->Username   = 'username'; 
    $mail->Password   = 'password';       
    $mail->SMTPSecure = 'tls';   
    $mail->Port       = 587;   
    $mail->setFrom('[email protected]', 'Owner'); 
    $mail->addAddress('[email protected]'); 
    $mail->isHTML(true);                                  
    $mail->Subject = "Hello this is subject";
    $mail->Body    = "Hello this is message;
    $mail->send();

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

When I comment $mail->isSMTP(); and send mail, I get the result as message sent but I dont get it in my Gmail inbox.

when I uncomment $mail->isSMTP(); I get error message as shown in below image.

smtp error

My project is hosted in godaddy server. Even if I use php mail() function to send mail, response is mail send successfully, but it does not get delivered into my Gmail inbox

Upvotes: 1

Views: 624

Answers (1)

Chidem S
Chidem S

Reputation: 184

If you want to use Gmail, you should follow the steps here to manually add Google MX records to your GoDaddy account: https://uk.godaddy.com/help/point-my-domains-email-service-to-google-7936

Unfortunately it takes some days for changes to be reflected.

Then you need to disable security features, change your php mailer config like:

$mail->Host = 'localhost';
$mail->SMTPAuth = false;
$mail->SMTPAutoTLS = false; 
$mail->Port = 25; 

Hope this solves.

------EDIT------

Later I figured out that the emails go to spam folder when the port is 25. I changed the connection to TLS and now I receive them as normal.

Here is the code from my website, it works on Godaddy, I receive e-mails to my gmail account.

$mail = new PHPMailer();
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'true';
$mail->Port = 587;
$mail->Host = "smtp.gmail.com";
$mail->Username = "********@gmail.com";
$mail->Password = "**********";
$mail->SetFrom("********@gmail.com");
$mail->AddAddress("********@gmail.com");
$mail->Subject = " your subject";
$mail->Body = "your body"; 

Upvotes: 0

Related Questions