我正在使用swiftmailer在symfony中创建一个邮件类。我写过邮件程序如下:
<?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('smtptest.hi@gmail.com')
->setPassword('scientech');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance('Wonderful Subject');
$message->setFrom('ucerturohit@gmai.com');
$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 :(得分:1)
$x=new \MyMailers;
将在全局命名空间中找到。
你应该做的是:
use Ens\NewBundle\Controller\Services\MyMailers as MyMailers;
$x = new MyMailers;