Sonata Admin - 访问邮件程序

时间:2013-10-28 10:11:42

标签: symfony sonata-admin

我正在尝试使用发送电子邮件的Sonata Admin Bundle创建自定义批处理操作。

问题是,我不确定如何访问swiftmailer。我有以下内容:

public function batchActionSend(ProxyQueryInterface $selectedModelQuery)
{
   if($this->admin->isGranted('EDIT')=== false) {
       throw new AccessDeniedException();
   }

   $request = $this->get('request');
   $modelManager = $this->admin->getModelManager();

   $selectedModels = $selectedModelQuery->execute();

   try {
       foreach ($selectedModels as $selectedModel) {
           // send the email here?
           $selectedModel->send();
           $modelManager->update($selectedModel);
       }
   } catch (\Exception $e) {
       $this->get('session')->getFlashBag()->add('sonata_flash_error', $e->getMessage());

       return new RedirectResponse($this->admin->generateUrl('list',$this->admin->getFilterParameters()));
   }

   $this->get('session')->getFlashBag()->add('sonata_flash_success',  sprintf('The selected requests have been sent'));

   return new RedirectResponse($this->admin->generateUrl('list',$this->admin->getFilterParameters()));

在我的实体中,我有以下内容:

public function send()
{
   // send email here?
}

访问邮件的最佳方式是什么?通过管理类或通过实体或通过服务?

由于

1 个答案:

答案 0 :(得分:0)

使用Swift_Message::newInstance()

public function send()
{
    $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('recipient@example.com')
        ->setBody(
            $this->renderView(
                'HelloBundle:Hello:email.txt.twig',
                array('name' => $name)
            )
        )
    ;
    $this->get('mailer')->send($message);

}