使用变量从Sonata管理员发送自定义电子邮件

时间:2018-07-06 20:47:26

标签: symfony listview sonata-admin symfony-3.4

我在奏鸣曲管理员中有一个列表视图。我想添加一列,使我可以单击链接来发送电子邮件。链接操作将知道表中该行的变量,以便它可以填写电子邮件。我能够添加该列,并且可以可视化树枝模板。我已将以下函数添加到“管理类”中:

 public function sendEmail( \Swift_Mailer $mailer)
    {
        $message = (new \Swift_Message('some email'))
            ->setFrom('contact@example.com')
            ->setTo('contact@example.com')
            ->setBody(
                $this->renderView(
                    'emails/response.html.twig',
                    array('manufacturer' => $manuvalue, 'modelnum' => $modelnumvalue, 'email' => $email)
                ),
                'text/html');

        $mailer->send($message);

    }

我一直坚持如何将这些部分连接在一起,因此,当我单击链接时,将发送电子邮件并包含该行中的参数。我在网站的其他区域中有处理表单提交的电子邮件,但需要帮助来确定手动执行此操作的方式。

2 个答案:

答案 0 :(得分:0)

正如您在评论中提到的,您通常要做的是Custom Action

为了确保不能通过直接请求访问此操作,并且只能由管理员执行此操作,可以为customAction使用这样的模板:

...
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
...

//AuthorizationCheckerInterface injected as authorizationChecker

public function customAction($id){
    $object = $this->admin->getSubject();
    if (!$object) {
        throw new NotFoundHttpException(sprintf('unable to find the object with id: %s', $id));
    }

    // Check access with role of your choice
    $access = $this->authorizationChecker->isGranted('ROLE_ADMIN');
    if($access){

    //Do your action

    } else {
        throw $this->createAccessDeniedException("You don't have the rights to do that!");
    }
}

答案 1 :(得分:0)

我最终做了一条自定义路由,并使用@dirk提到的安全设置对其进行了保护。