0

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.

Evgeny
  • 153
  • 16

2 Answers2

1

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.

Andy
  • 36
  • 1
  • 4
  • How do I check it? (My server is not port forwarded) – Evgeny Dec 22 '17 at 11:40
  • You need access via cPanel and/or WHM to look at the characteristics of your setup, if not, then you must go through technical support. – Andy Dec 23 '17 at 17:00
0

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.

Ajowi
  • 449
  • 3
  • 12