0

I have the following PHP code that is supposed to send email (running it on a apache server):

<?php
require_once "Mail.php";

$from = '<myemail@gmail.com>';
$to = '<destination@gmail.com>';
$subject = 'PHP test';
$body = "Hi,\n\n How are you? This was sent by a PHP program. ";

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

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

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

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

?>        

I get the following error message:

Failed to connect to smtp.gmail.com:465 [SMTP: Failed to connect socket: Connection refused (code: -1, response: )]

I tried to send the email using the same SMTP server and with same emails through matlab and it worked.

Julio Sca
  • 21
  • 1
  • 4
  • https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting#smtp-error-could-not-connect-to-smtp-host – Lambasoft Mar 13 '17 at 02:03
  • Either your ISP/server provider blocks the SMTP port or your Gmail account does not have imap enabled (its in "Settings") – Gab Mar 13 '17 at 02:07

0 Answers0