Search code examples
smtpphpmailer

PHPMailer - Could not connect to SMTP host


Trying to implement PHPMailer on BlueHost VPS using Gmail SMTP server. Fails with "Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host." Have an app password on my google account for PHPMailer, which is used in the script. I have tried many combinations of ports and authorizations with same error. No other error messages are produced (that I can find).

try 
    {
        //Server settings
        $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
        $mail->SMTPAuth = true; // authentication enabled
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
        
        //Use GOOGLE SMTP Server
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 587;          
        $mail->Username   = 'me@gmail.com';                     //SMTP username
        $mail->Password   = '***app password***';              //SMTP password

        //Recipients
        $mail->setFrom('me@gmail.com', 'Mailer');
        $mail->addAddress('myFriend@gmail.com', 'Good Person');     //Add a recipient

        //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';
        exit;
    } 
    catch (Exception $e) 
    {
        echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
        exit;
    }    

Solution

  • Thank you Synchro! After getting the ports unblocked, I found an error in my use statements. Now sending emails through Gmail.