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?

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

报告相同问题?

悬赏问题

  • ¥15 C++扑克牌游戏的编程
  • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
  • ¥15 cs loadimage运行不了,easyx也下了,没有用
  • ¥15 r包runway详细安装教程
  • ¥15 Html中读取Json文件中数据并制作表格
  • ¥15 谁有RH342练习环境
  • ¥15 STM32F407 DMA中断问题
  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。