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.