php - Unable to connect to SMTP server. in CakePHP

921

I got this error in cakephp using the smtp mail function

cakephp code:

...
     try{
              $emailClass = new CakeEmail();
              $emailClass->config(array(
              'host' => 'ssl://'.$this->smtpHostName,
              'port' => $this->smtpPort,
              'username' => $this->smtpUserName,
              'password' => $this->smtpPassword,
              'transport' => 'Smtp'
          ));
          $emailClass->from(array($this->from => $this->fromName));
          $emailClass->to($user_email);
          $emailClass->subject($subject);
          $emailClass->emailFormat('html');
          $contents = $emailClass->send($message_text);
          if (!empty($contents))
            return true;      
         }
         catch (Exception $e){
              pr($e);
         }
...

error trace:

 ...
    [_messageTemplate:protected] => 
    [message:protected] => Unable to connect to SMTP server.
    [string:Exception:private] => 
    [code:protected] => 500
    [file:protected] => /opt/lampp/htdocs/site/lib/Cake/Network/Email/SmtpTransport.php
    [line:protected] => 96
    [trace:Exception:private] => Array 
...

Verified that the smtp configuration is correct because I was able to send an email using phpmail

...
$headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'port' => 25,
     'debug' => true,
     'username' => $username,
     'password' => $password));

 $mail = $smtp->send($to, $headers, $body);
...

I'm not sure if it has something to do with cakephp's config because the email code is working on another server.

tried:

fsockopen('ssl://hosthere', porthere);

ssl problem?

553

Answer

Solution:

Try after adding timeout, Hope it will solve this problem!

...
$headers = array ('From' => $from,
   'To' => $to,
   'Subject' => $subject);
 $smtp = Mail::factory('smtp',
   array ('host' => $host,
     'auth' => true,
     'port' => 25,
     'debug' => true,
     **'timeout' => 30,**
     'username' => $username,
     'password' => $password));

 $mail = $smtp->send($to, $headers, $body);
...

People are also looking for solutions to the problem: php, this array works in global but failed while putting inside class

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.