'Invalid Address: SMTP Error: Could not connect to SMTP host

I am trying to use gmail smtp server to send emails via php mailer from a php form. i have opened the 465 port from windows firewall but the port doesn't appear in the cmd output when i type

netstat -an

command.the error that appears in the php page is: Invalid address: SMTP Error: Could not connect to SMTP host. there is no exact solution for my issue in the result of my searchs. my code is:

<?php
 require_once('PHPMailer_v5.1/class.phpmailer.php');
 define('GUSER', '[email protected]'); // GMail username
define('GPWD', 'password'); // GMail password
function smtpmailer($to, $from, $from_name, $subject, $body) { 
    global $error;
    $mail = new PHPMailer();  // create a new object
    $mail->IsSMTP(); // enable SMTP
    $mail->SMTPDebug = 0;  // debugging: 1 = errors and messages, 2 = messages only
    $mail->SMTPAuth = true;  // authentication enabled
    $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 465; 
    $mail->Username = GUSER;  
    $mail->Password = GPWD;           
    $mail->SetFrom($from, $from_name);
    $mail->Subject = $subject;
    $mail->Body = $body;
    $mail->AddAddress($to);
    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Message sent!';
        return true;
    }}
    if(smtpmailer('[email protected]', '[email protected]', 'name', 'test mail message', 'Hello World!')){
        echo "sent";
    if (!empty($error)) echo $error;}

  ?>

note: i am using localhost for my site



Solution 1:[1]

Try using port 25 or 587, also check your apache log.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ravi D