Search code examples
smtpgmailphpmailerswiftmailer

PHPMailer/SwiftMailer Send Email - Connection could not be established with host smtp.gmail.com


I am trying to send emails via PHP Mailer (upon it not working I also installed and tried Swift Mailer, and am getting the same results).

I can send them using localhost but it ALWAYS goes to spam, which is doing my head in. So I am trying to send it through SMTP through my gmail account, in the hope that this will help authenticate the email.

Here is my code:

function SendMail( $ToEmail, $MessageHTML, $MessageTEXT ) {
  require_once ( '../phpmailer/PHPMailerAutoload.php' ); // Add the path as appropriate
  $Mail = new PHPMailer();
  $Mail->IsSMTP(); // Use SMTP
  $Mail->Host        = "smtp.gmail.com"; // Sets SMTP server
  $Mail->SMTPDebug   = 2; // 2 to enable SMTP debug information
  $Mail->SMTPAuth    = TRUE; // enable SMTP authentication
  $Mail->SMTPSecure  = "tls"; //Secure conection
  $Mail->Port        = 587; // set the SMTP port
  $Mail->Username    = '[email protected]'; // SMTP account username
  $Mail->Password    = 'workingpass'; // SMTP account password (IVE TRIED BOTH NORMAL AND APP PASSWORDS)
  $Mail->Priority    = 1; // Highest priority - Email priority (1 = High, 3 = Normal, 5 = low)
  $Mail->CharSet     = 'UTF-8';
  $Mail->Encoding    = '8bit';
  $Mail->Subject     = 'Test Email Using Gmail';
  $Mail->ContentType = 'text/html; charset=utf-8\r\n';
  $Mail->From        = '[email protected]';
  $Mail->FromName    = 'GMail Test';
  $Mail->WordWrap    = 900; // RFC 2822 Compliant for Max 998 characters per line

  $Mail->AddAddress( $ToEmail ); // To:
  $Mail->isHTML( TRUE );
  $Mail->Body    = "Test";
  $Mail->AltBody = "Test";
  $Mail->Send();
  $Mail->SmtpClose();

  if ( $Mail->IsError() ) { // ADDED - This error checking was missing
    return FALSE;
  }
  else {
    return TRUE;
  }
}

$ToEmail = '[email protected]';
$ToName  = 'Name';

$Send = SendMail( $ToEmail, $MessageHTML, $MessageTEXT );
if ( $Send ) {
  echo "<h2> Sent OK</h2>";
}
else {
  echo "<h2> ERROR</h2>";
}
die;

This returns the following error:

2014-12-16 13:17:14 SMTP ERROR: Failed to connect to server: Network is unreachable (101) 2014-12-16 13:17:14   SMTP connect() failed.
ERROR

I have contacted my web provider to check the following: OpenSSL enabled fopen enabled ports 465 and 587 open

I have gone through numerous example codes from both PHP Mailer and Swift Mailer (which all claim to be working code!) and all give the same result. Normally the error 101 cannot connect to the gmail server!

I currently have a support ticket open with my web provider and have people looking into it there and they can't see an issue either.

Help please :(


Solution

  • Right at this moment, gmail smtp servers appear to be having widespread outages this morning. I don't know code:) but was searching for others having issues. Re-try later.