0
votes

I am trying to setup SwiftMailer on my server. I am using the example configuration from the SwiftMailer docs for testing. I have a paid G Suite account but haven't finished setting up SSL on the server yet. I am running Cent OS 6.8 and Apache 2.2. I have googled everything I can think of and tried all the proposed solutions with no success.

My script is as follows, obviously my email address and credentials are correct in the real script. I appreciate any advice.

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

require_once('swiftmailer-5.x/lib/swift_required.php');

// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp-relay.gmail.com', 25)
     ->setUsername('[email protected]')
     ->setPassword('*****')
;

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

// Create a message
$message = Swift_Message::newInstance('Swift Mailer Test')
  ->setFrom(array('my from address'))
  ->setBody('Here is the message itself')
  ;

// Send the message
$failedRecipients = array();
$numSent = 0;
$to = array('[email protected]' => 'Joe');

foreach ($to as $address => $name)
{
  if (is_int($address)) {
    $message->setTo($name);
  } else {
    $message->setTo(array($address => $name));
  }

  $numSent += $mailer->send($message, $failedRecipients);
}

printf("Sent %d messages\n", $numSent);


?>

When I run this script I get the following very vague error:

"Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp-relay.gmail.com [Connection timed out #110]' in /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/StreamBuffer.php:269 Stack trace: #0 /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/StreamBuffer.php(62): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/AbstractSmtpTransport.php(113): Swift_Transport_StreamBuffer->initialize(Array) #2 /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start() #3 /var/www/html/appreciate-erp/mailtest.php(42): Swift_Mailer->send(Object(Swift_Message), Array) #4 {main} thrown in /var/www/html/appreciate-erp/swiftmailer-5.x/lib/classes/Swift/Transport/StreamBuffer.php on line 269"

1
Wow, stack overflow. Was it something I said?SuperCatchyUsername

1 Answers

0
votes

As of https://support.google.com/a/answer/176600, it seems like SMTP Relay on G-Suite applies IP address restriction. The "could not connect" error would fit as the result of a missing configuration or the attempt to connect from another IP address as the one configured. Hope this helps!