2012-08-17 102 views
0

我正在使用swiftmailer在symfony中創建一個郵件類。我寫郵件類如下:symfony 2沒有找到錯誤類

<?php 
namespace Ens\NewBundle\Controller\Services; 

class MyMailers 
{ 

public $setTovar=''; 


public function setToloc($name) 
{ 
    $this->setTovar=$name; 
} 

public function mail() 
{ 
     date_default_timezone_set('Asia/Kolkata'); 

     $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl') 
     ->setUsername('[email protected]') 
     ->setPassword('scientech'); 
     $mailer = \Swift_Mailer::newInstance($transport); 
     $message = \Swift_Message::newInstance('Wonderful Subject'); 
     $message->setFrom('[email protected]'); 
     $message->setTo($this->setTovar); 
     $message->setBody('Here is the message itself'); 
     $numSent = $mailer->send($message); 
     return $numSent; 
    } 
} 

Notification.php我寫的代碼如下:

//require_once('MyMailers.php'); 
use Ens\NewBundle\contrller\Services\MyMailers; 
    function NotificationOnSignUp($z) 
    { 
    // echo $z;die; 
     // $y=unserialize($z); 
     echo $z; 
     echo "<pre>"; 
     print_r($z); 
     echo "</pre>"; 
      $x=new MyMailers; 
     $x->setToloc($z['to']); 

      $z=$x->mail(); 
     if($z==1) 
    $name='success'; 
    else 
    $name='failed'; 

當我運行此代碼,然後我得到的錯誤:

Fatal error: Class 'Ens\NewBundle\contrller\Services\MyMailers' not found in /var/www/Symfony/src/Ens/NewBundle/Controller/Services/Notification.php on line 13我該怎麼辦 ??

+0

錯別字? Ens \ NewBundle \ << < contrller > >> \ Services \ MyMailers – madflow 2012-08-17 09:21:04

回答

1

$x=new \MyMailers;將在全局命名空間中找到。

你應該做的是:

use Ens\NewBundle\Controller\Services\MyMailers as MyMailers; 

$x = new MyMailers; 
+0

使用此命令後也沒有任何效果 – 2012-08-17 10:26:24

+0

出現錯誤致命錯誤:在/ var/www/Symfony中找不到類'Ens \ NewBundle \ controller \ Services \ MyMailers' /src/Ens/NewBundle/Controller/Services/Notification.php on line 14' – 2012-08-17 10:27:32

+1

你應該使用'Ens \ NewBundle \ Controller \ Services \ MyMailers'而不是'Ens \ NewBundle \ controller \ Services \ MyMailers' Unix系統是大小寫敏感的 – l3l0 2012-08-17 17:20:31