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:
- Fixing SSL certificate configuration issues
- Enabling “Less secure app access” in Gmail settings
- Allowing access to the Google account through the security settings
After making these changes, the email functionality works perfectly.