2014-11-05 31 views
2

我想了解如何使用CakeEmail。爲此,我使用Pages控制器創建了一個名爲email-tester.ctp的視圖。我通過直接調用頁面來訪問它(mydomain/mypath/pages/email-tester)。如何使用CakeEmail

視圖本身只是保持從文檔的標準編碼,更多或更少:

<?php 
App::uses('CakeEmail', 'Network/Email'); 

$Email = new CakeEmail(); 
$Email->from(array('[email protected]' => 'My Gmail Address')) 
    ->to('[email protected]') 
    ->subject('About') 
    ->send('My message'); 
?> 
<p>Email sent...</p> 

我也創建一個電子郵件配置文件(email.php)如下:

class EmailConfig { 
     public $gmail = array(
     'host' => 'ssl://smtp.gmail.com', 
     'port' => 465, 
     'username' => '[email protected]', 
     'password' => 'mypassword-for-gmail', 
     'transport' => 'Smtp', 
      ); 
} 

當我運行這個頁面時,我得到一個內部服務器錯誤:

錯誤:發生內部錯誤。 堆棧跟蹤

CORE/Cake/Network/Email/MailTransport.php line 51 → MailTransport->_mail(string, string, string, string, null) 
CORE/Cake/Network/Email/CakeEmail.php line 1158 → MailTransport->send(CakeEmail) 
APP/View/Pages/email-tester.ctp line 8 → CakeEmail->send(string) 
CORE/Cake/View/View.php line 948 → include(string) 
CORE/Cake/View/View.php line 910 → View->_evaluate(string, array) 
CORE/Cake/View/View.php line 471 → View->_render(string) 
CORE/Cake/Controller/Controller.php line 954 → View->render(string, null) 
APP/Controller/PagesController.php line 69 → Controller->render(string) 
[internal function] → PagesController->display(string) 
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(PagesController, array) 
CORE/Cake/Routing/Dispatcher.php line 191 → Controller->invokeAction(CakeRequest) 
CORE/Cake/Routing/Dispatcher.php line 165 → Dispatcher->_invoke(PagesController, CakeRequest) 
APP/webroot/index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse) 

任何人都可以指出我我要去哪裏錯了嗎?我需要在我工作的本地開發機器上運行郵件服務器嗎?還是問題更重要一些?

從其他研究中,我看到一些建議,即php.ini文件應該包含一個擴展名爲php_openssl的行,但是這隻會導致出現一條錯誤消息,提示找不到擴展名:可能是因爲它包含在gnutls我在Ubuntu 14.10上)。

任何幫助將不勝感激。

彼得

回答

1

如果從本地服務器的工作,如wampp,XAMPP等,該功能不會工作,溜溜就需要有一個郵件服務器設置這個工作。一旦你有,或如果你能在一個真正的服務器進行測試,這可能幫助:

在:

Views->Layouts->Emails->html 

我有我的電子郵件被稱爲HTML模板「clientsreports」:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html> 
    <head> 
     <title><?php echo $title_for_layout;?></title> 
    </head> 
    <body> 
     <?php echo $content_for_layout;?>  
    </body> 
</html> 

而且你在EmailConfig文件變種應命名爲SMTP爲它工作,檢查煤礦是如何設置

class EmailConfig { 
    public $smtp = array(
     'transport' => 'Smtp', 
     'from' => array('[email protected]' => 'Senders name'), 
     'host' => 'ssl://smtp.gmail.com', 
     'port' => 465, 
     'timeout' => 30, 
     'username' => '[email protected]', 
     'password' => 'mypassword', 
     'client' => null, 
     'log' => false 
    ); 
} 

網絡最後這是一個簡單的例子,我如何發送電子郵件:

$email = new CakeEmail('smtp'); 
$email->template('clientsreport', 'clientsreport'); 
$email->emailFormat('html'); 
$email->viewVars(array('message' => "This is the body of the message")); 
$email->from(array('[email protected]' => 'Senders name')); 
$email->to('[email protected]'); 
//Only if neccesary this is how to carbon copy someone 
$email->cc(array('[email protected]','[email protected]')); 
$email->subject('Subject for the email');   
$email->send(); 

我希望有幫助,祝你好運!