无法连接到SMTP服务器。在CakePHP中

时间:2013-12-12 03:15:22

标签: php email cakephp ssl smtp

我在cakephp中使用smtp mail函数

得到了这个错误

cakephp代码:

...
     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);
         }
...

错误跟踪:

 ...
    [_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 
...

验证了smtp配置是否正确,因为我能够使用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);
...

我不确定它是否与cakephp的配置有关,因为电子邮件代码正在另一台服务器上运行。

尝试:

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

问题?

1 个答案:

答案 0 :(得分:0)

添加超时后尝试,希望它能解决这个问题!

...
$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);
...