Gmail SMTP connection fails with PEAR Mail - socket connection error

Setup Details

I’m trying to send emails using PEAR Mail with Gmail’s SMTP server but running into connection issues. My environment includes Apache/2.4.27 (Win64) with PHP/7.2.0beta3, PEAR 1.10.15, Mail 1.4.1, Net_SMTP 1.8.0, and Net_Socket 1.2.2.

I’ve already enabled OpenSSL by adding extension = php_openssl.dll to my php.ini file. No SSL errors appear in the error log.

My Code

require_once "Mail.php";

$sender = '<[email protected]>';
$recipient = '<[email protected]>';
$email_subject = 'Test Message';
$email_content = "Hello,\n\nThis is a test email.";

$mail_headers = array(
    'From' => $sender,
    'To' => $recipient,
    'Subject' => $email_subject
);

$smtp_config = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => '[email protected]',
        'password' => 'mypassword123'
    ));

$result = $smtp_config->send($recipient, $mail_headers, $email_content);

if (PEAR::isError($result)) {
    echo('<p>' . $result->getMessage() . '</p>');
} else {
    echo('<p>Email sent successfully!</p>');
}

Error Message

I keep getting this error: Failed to connect to ssl://smtp.gmail.com:465 [SMTP: Failed to connect socket: fsockopen(): unable to connect to ssl://smtp.gmail.com:465 (Unknown error) (code: -1, response: )]

I’ve searched online but the solutions I found made things more confusing. Can someone help me fix this connection problem?

Additional Testing Results

After running some diagnostic tests, I discovered the issue was related to SSL certificate verification. The fsockopen function was failing with SSL certificate errors.

Solution Found

The problem was resolved by:

  1. Fixing SSL certificate configuration issues
  2. Enabling “Less secure app access” in Gmail settings
  3. Allowing access to the Google account through the security settings

After making these changes, the email functionality works perfectly.

I encountered a similar issue last year and the SSL certificate verification was indeed the culprit. However, I would strongly recommend against enabling “Less secure app access” as Google has deprecated this feature and it poses security risks. Instead, you should generate an App Password specifically for your application. Go to your Google Account settings, enable 2-factor authentication if not already done, then create an App Password under Security settings. Use this 16-character password in your code instead of your regular Gmail password. This approach maintains security while resolving the SMTP connection issues. Also verify that your server’s SSL certificate bundle is up to date, as outdated certificates can cause fsockopen failures even with correct authentication.

glad you got it working! just a heads up tho - if you’re still using that old PEAR setup, might run into issues again since gmail keeps tightening security. i switched to phpmailer recently and its way more stable with oauth2 authentication. also that ‘less secure apps’ thing is getting killed by google soon so definitly look into app passwords like alexlee mentioned.

Had this exact same error on my Windows development setup a few months back. The fsockopen SSL issue you encountered is quite common with PEAR Mail on Windows environments. Beyond the authentication changes you made, I found that adding stream context options helped stabilize the connection. You might want to consider upgrading from PEAR Mail since it’s becoming outdated - PHPMailer or SwiftMailer handle SSL connections more reliably in my experience. Also worth checking if your local firewall or antivirus is blocking outbound SSL connections on port 465, as Windows Defender sometimes interferes with SMTP SSL handshakes. For future reference, testing with telnet on port 587 using STARTTLS can help diagnose whether the issue is authentication-related or purely connection-based before diving into code changes.