0
votes

I am getting the error "Connection could not be established with host smtp.gmail.com [#0]" when trying to send a test email using swiftmailer with gmail. My code is as follows

<?php
require_once("./vendor/autoload.php");

$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465,'ssl'))
  ->setUsername('my email')
  ->setPassword('my email password')
  ;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$message = (new Swift_Message('Wonderful Subject'))
  ->setFrom(['email from' => 'from Name'])
  ->setTo(['email to' => 'to Name'])
  ->setBody('Here is the message itself')
  ;

// Send the message
try{
  $result = $mailer->send($message);
}catch(Exception $e){
  echo $e->getMessage();
}
echo $result;

My test environment is Windows Server 2012 (IIS 8 with PHP7).

But when I tried the same code base in an Ubuntu 16.04 (Apache installed with PHP7) environment, it worked. In order to send emails using gmail and SwiftMailer, Should I do any changes to the IIS server or to Windows configuration?

Note : If I use the same configuration with Yahoo account instead of Gmail account. Then it works.

1

1 Answers

0
votes

Is there a firewall running on this server?

Is it allowing outbound connections on port 465?

You may need to poke a hole in the firewall to allow connections to smtp.gmail.com (74.125.21.108 and 74.125.21.109) on port 465.