duanna5749 2016-11-30 15:56
浏览 31
已采纳

如何在按钮功能中午餐Swiftmailer

I'm using swiftmailer bundle, everything works good, but I still cannot integrate swiftmailer with my function. I have to confirm users after registration "using a confirm button" and i want to instantly launch a confirmation mail to my user confirmed.

My Button's code in twig:

{% if r=='USER' %}
    <td>
        <a href="{{ path ('confirm_user',{id:user.id}) }}">
        <button type="button" class="btn btn-success">Confirmer </button>
        </a>
    </td>
{% endif %}

My confirmAction in controller:

public function confirm_userAction($id)
{
    if (False === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
        return $this->render('@Admin/controlDerreur/denied.html.twig');
    }

    $em = $this->getDoctrine()->getManager();
    $user = $em->getRepository("MyAppUserBundle:User")->find($id);
    if ($user != null) {
        $user->addRole('ROLE_LECTEUR');
        $em->persist($user);
        $em->flush();
    }

    return $this->redirectToRoute('users_list');
}

My Mail controller:

class MailController extends Controller{
    public function indexAction(Request $request){
        $mail = new Mail();

        $form = $this->createForm(MailTypeForm::class, $mail);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $message = Swift_Message::newInstance()
                ->setSubject('Accusé de réception')
                ->setFrom('mail@gmail.com')
                ->setTo($mail->getEmail())
                ->setBody(
                    $this->renderView('MyAppUserBundle:Default:mail.html.twig',array(
                        'nom' => $mail->getNom(),
                        'prenom' => $mail->getPrenom()
                    )),'text/html');

            $this->get('mailer')->send($message);

            return $this->redirect($this->generateUrl('my_app_mail_accusm'));
        }

        return $this->render('MyAppUserBundle:Default:new.html.twig', array(
            'form' => $form->createView()
        ));
    }

    public function succesAction()
    {
        return new Response("email envoyé avec succès, Merci de vérifier votre adresse mail.");
    }
}

My routings :

my_app_mail_newm:
    path:     /newm
    defaults: { _controller: "MyAppUserBundle:Mail:index" }


my_app_mail_accusm:
    path: /succesm
    defaults: { _controller: "MyAppUserBundle:Mail:succes" }
users_list:
    path:   /admin/userlist
    defaults: { _controller: "AdminBundle:Admin:list_users" }
confirm_user:
path:   /admin/confirm/{id}
defaults: { _controller: "AdminBundle:Admin:confirm_user" }
  • 写回答

1条回答 默认 最新

  • duancai7568 2016-11-30 17:54
    关注

    What if you use a SubmitType button:

    ->add('sendmail', SubmitType::class, array(
       'label' => 'Send Email',
       'disabled' => true,
    ))
    

    Then you can use disabled option to disable submit using Twig IF {% if r=='USER' %} to enable the submit button in your template.

    EDIT #2:

    So if all you need to do is send an email, modify your confirm_userAction controller:

    if ($user != null) {
        $user->addRole('ROLE_LECTEUR');
        $em->persist($user);
        $em->flush();
    }
    
    $message = Swift_Message::newInstance()
                    ->setSubject('Accusé de réception')
                    ->setFrom('mail@gmail.com')
                    ->setTo($user->getEmail())
                    ->setBody("My message");
    
    $this->get('mailer')->send($message);
    
    return $this->redirectToRoute('users_list');
    

    Is that what you needed?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联