Solving PHPMailer and GMail connection failed issue

Strange behaviour – it just stopped working 10 days ago and after a long search found a solution in specifying additional parameters in SSL connection, so working example should be like this:

try 
{
  $mail = new PHPMailer();
  $mail->IsSMTP();
  $mail->SMTPDebug = 2;
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'ssl';
  $mail->SMTPOptions = array
  (
    'ssl' => array
    (
      'verify_peer' => false,
      'verify_peer_name' => false,
      'allow_self_signed' => true
    )
  );
  $mail->Host = "smtp.gmail.com";
  $mail->Port = 465;
  $mail->IsHTML(true);
  $mail->Username = GOOGLE_SMTP_USERNAME;
  $mail->Password = GOOGLE_SMTP_PASSWORD;
  $mail->SetFrom(SMTP_USERNAME);
  $mail->Subject = "Test";
  $mail->Body = "hello";
  $mail->AddAddress(YOUR_RECEIVING_EMAIL);
 
  if(!$mail->Send()) 
  {
    echo "Mailer Error: " . $mail->ErrorInfo;
  } 
  else 
  {
    echo "Message has been sent";
  }
} 
catch (phpmailerException $e) 
{
  $errors[] = $e->errorMessage();
}
catch (Exception $e) 
{
  $errors[] = $e->getMessage(); 
}

 

Follow

Get every new post delivered to your Inbox

Join other followers