phpsmtppearpear-mail

PHP pear-mail smtp not working


When I try to run this code, I get a strange error I cannot find an answer to:

// Pear Mail Library
require_once "Mail.php";

$from = 'someone@gmail.com';
$to = 'someoneelse@gmail.com';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'someone@gmail.com',
        'password' => 'password'
    ));

$mail = $smtp->send($to, $headers, $body);

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

The error is:

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 looked for an answer but could not find it anywhere. Please help.


Solution

  • i have the same problem and mine stopped working suddenly. My suspicion is the SSL certificates associated with my name server, or in your case your name server are expired. My hosting site has incompetent help as they have been at it for two weeks and still can't figure it out. So check the name server SSL certificates.