2012-06-26 47 views
5

我使用SMTP在我的CAKEPHP項目中發送電子郵件。 我的電子郵件地址的配置如下電子郵件不起作用的CakePHP 2.0 SMTP設置

class EmailConfig { 

    public $Smtp = array(
     'transport' => 'Smtp', 
     'from' => array('[email protected]' => 'domainname.com'), 
     'host' => 'myhostingserver', 
     'port' => 2525, 
     'timeout' => 60, 
     'username' => '[email protected]', 
     'password' => 'secret', 
     'client' => null, 
     'log' => false 
    ); 

和我的郵件功能的代碼如下

$email = new CakeEmail('Smtp'); 
    $result = $email->template('welcome_mail','default') 
         ->emailFormat('html') 
         ->to($to_email) 
         ->from('[email protected]') 
         ->subject('Welcome to my domain name') 
         ->viewVars($contents); 

    if($email ->send('Smtp')) 
    { 
     echo ('success'); 

    } 

雖然我發送郵件的投擲以下錯誤SMTP超時。我的SMTP服務器的詳細信息是正確的,它在現有服務器中工作正常。我不知道我錯在哪裏

回答

6

檢查加密類型(如適用),例如SSL或TLS

您的主機URL應該是這個樣子在這種情況下

'host' => 'ssl://myhostingserver' 

'host' => 'tls://myhostingserver' 
1

如果您的SMTP服務器有SSL,您必須在php.ini中啓用php_openssl才能使用此服務。 您可以使用此代碼測試

if(!in_array('openssl',get_loaded_extensions())){ 
    die('you have to enable php_openssl in php.ini to use this service');  
} 
0

旁邊已經在這裏發生了什麼這裏sugested該模塊必須加載。我發現有些服務器有一些端口被阻塞。我用這個腳本來測試一些服務器:

<?php 

if(!in_array('openssl',get_loaded_extensions())){ 
    die('you have to enable php_openssl in php.ini to use this service');  
} else { 
    echo "php_openssl in php.ini is enabled <br />"; 
} 

// fill out here the smpt server that you want to use 
$host = 'ssl://smtp.gmail.com'; 
// add here the port that you use for for the smpt server 
$ports = array(80, 465); 

foreach ($ports as $port) 
{ 
    $connection = @fsockopen($host, $port); 
    if (is_resource($connection)) 
    { 
     echo $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.<br />' . "\n"; 
     fclose($connection); 
    } else { 
     echo $host . ':' . $port . ' is not responding.<br />' . "\n"; 
    } 
} 

?>