Evgeny
Evgeny

Reputation: 313

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 = '[email protected]';
$to = '[email protected]';
$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' => '[email protected]',
        '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.

Upvotes: 0

Views: 742

Answers (2)

Ajowi
Ajowi

Reputation: 505

The unknown error code of -1 can be confusing but when it shows up on Pear Mail, then most likely you need to install Mail and Mail_Mime

pear install Mail Mail_Mime

SMTP errors, if any, will always be specifically reported. If this also happens, then install Net_SMTP also.

Upvotes: 0

Andy
Andy

Reputation: 36

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.

Upvotes: 1

Related Questions