Search code examples
phpsmtpgmaildebianphpmailer

PHPMailer fails to connect to google smtp server under debian


I am trying to send an e-mail with PHPMailer. Here is my function to send an e-mail. It is working fine on localhost from an Ubuntu 14.04.

function smtpMail($to, $from, $from_name, $subject, $body) {
   $mail = new PHPMailer;
   $mail->IsSMTP();                                      // tells PHPMailer to use SMTP
   $mail->Host       = gethostbyname('smtp.gmail.com');  // sets the hostname of the mail server
   $mail->Port       = 587;                              // sets the SMTP port number
   $mail->SMTPSecure = 'tls';                            // sets the encryption system to use
   $mail->SMTPDebug  = 2;                                   // debug level
   $mail->SMTPAuth   = true;                             // whether to use SMTP authentication
   $mail->Username   = '[email protected]';            // username to use for SMTP authentication
   $mail->Password   = 'qwerty';                     // password to use for SMTP authentication
   $mail->CharSet    = 'UTF-8';
   $mail->IsHTML(true);
   $mail->setFrom($from, $from_name);                    // sets who the message is to be sent from
   $mail->Subject = $subject;
   $mail->Body = $body;
   $mail->AddAddress($to);
   if (!$mail->Send ()) {
           return 'Mail error: ' . $mail->ErrorInfo;
           //return 'KO';
   }
   return 'OK';
}

Now I am trying to deploy it on a server. The code is the same but it is not working anymore. The server is an apache2 running inside a debian 8.2. PHPMailer prints these lines.

2015-12-09 13:35:41 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2015-12-09 13:35:41 SMTP Error: Could not connect to SMTP host.
2015-12-09 13:35:41 CLIENT -> SERVER: QUIT
2015-12-09 13:35:41 SERVER -> CLIENT:
2015-12-09 13:35:41 SMTP ERROR: QUIT command failed:

From the server the following commands are working fine :

dig smtp.gmail.com
ping smtp.gmail.com
telnet smtp.gmail.com

I tried to send an e-mail with telnet and it is also working. I followed this tutorial (sorry it's in french) What am I doing wrong?


Solution

  • Ok I found the answer here

    It appears that my certificates were not matching.