Gmail SMTP connection failure with PEAR Mail - socket connection error

Environment Details

I’m trying to send emails through Gmail’s SMTP server using PEAR Mail library. My setup 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-related errors appear in the error log.

My Code

require_once "Mail.php";

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

$mailHeaders = array(
    'From' => $sender,
    'To' => $recipient,
    'Subject' => $emailSubject
);

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

$result = $smtpConfig->send($recipient, $mailHeaders, $messageBody);

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 only made things more confusing. Can someone help me understand what’s causing this issue and how to resolve it?

Additional Testing Results

After some debugging, I discovered the issue was related to SSL certificate verification. The fsockopen function was failing with SSL operation errors. I also encountered authentication issues (error 534) which required enabling less secure app access in Gmail settings.

The problem was eventually resolved by adjusting firewall settings and enabling less secure app access for the Gmail account being used.

I hit this exact issue with PEAR Mail and Gmail SMTP. That socket connection error is super common with older PEAR Mail versions - Gmail’s security requirements break it constantly. Since you got it working with firewall changes, I’d check if Windows Defender or your corporate firewall is blocking port 465. Try port 587 with TLS instead - set your host to ‘tls://smtp.gmail.com’ and port to ‘587’. Also, Google killed less secure app access in 2022, so regular passwords won’t work anymore. You’ll need App Passwords from your Google Account settings. Honestly though, PEAR Mail is ancient at this point. I’d switch to PHPMailer or SwiftMailer - they play way nicer with Gmail and actually get updates.

Had the same issue with PEAR Mail on Windows. That fsockopen SSL error means your PHP SSL context isn’t set up right for Gmail’s certificate chain. Check your OpenSSL version - older ones can’t handle Gmail’s TLS properly. Test SSL connectivity outside PHP first using telnet or openssl s_client commands. If those work but PHP doesn’t, it’s definitely a PHP SSL config problem. Windows installations often have incomplete CA certificate bundles, so the SSL handshake fails even when the connection works. You’ll probably need to download a proper cacert.pem file and configure it in php.ini.

Yeah, I’ve hit this same issue. SSL verification with PEAR Mail is a nightmare. Add this to your SMTP config array: ‘socket_options’ => array(‘ssl’ => array(‘verify_peer’ => false, ‘verify_peer_name’ => false)). It’ll skip the cert check that’s breaking fsockopen. Not super secure, but it’ll get you unstuck while you figure out the real fix.