发送用户和管理员确认电子邮件

时间:2017-10-22 10:24:41

标签: php symfony email

我创建了一个服务,用于在发送表单时发送电子邮件。我收到它评价很高的管理员,但我想将其发送给已通过电子邮件完成表单的用户,前提是电子邮件字段已填写。这是我的服务和我的控制器:

我的服务

public function sendMailInscriptionMjml(Invite $invite, $mailTo)
{
    $subject = 'Inscription invité';
    $template = 'AppBundle:Mail:Invite/inscription.html.twig';
    $templateP = 'AppBundle:Mail:Invite/inscription.txt.twig';

    $this->sendMessage($mailTo, $subject, $template, $templateP, array('invite' => $invite));
}

我的控制器:

/**
 * Creates a new invite entity.
 *
 * @Route("/", name="invite_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request)
{
    $invite = new Invite();

    $form = $this->createForm(InviteType::class, $invite);

    if ($request->isMethod('POST')) {
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($invite);
            $em->flush();

            $this->get('app_mailer')->sendMailInscriptionMjml(
                $invite, $this->getParameter('client_mail_to')
            );

            $this->get('session')->getFlashBag()
                ->add('success', 'Votre inscription à été pris en compte.');

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

    return $this->render('@App/invite/new.html.twig', array(
        'invite' => $invite,
        'form' => $form->createView(),
    ));
}

谢谢。

1 个答案:

答案 0 :(得分:0)

我找到了怎么做,这是我控制器中的解决方案。

/**
 * Creates a new invite entity.
 *
 * @Route("/", name="invite_new")
 * @Method({"GET", "POST"})
 */
public function newAction(Request $request)
{
    $invite = new Invite();

    $form = $this->createForm(InviteType::class, $invite);

    if ($request->isMethod('POST')) {
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $mail = $invite->getEmail();
            $em = $this->getDoctrine()->getManager();
            $em->persist($invite);
            $em->flush();

            $this->get('app_mailer')->sendMailInscriptionMjml(
                $invite, $this->getParameter('client_mail_to')
            );


            $this->get('app_mailer')->sendEmailInvite(
                $invite, $mail
            );


            $this->get('session')->getFlashBag()
                ->add('success', 'Votre inscription à été pris en compte.');

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

    return $this->render('@App/invite/new.html.twig', array(
        'invite' => $invite,
        'form' => $form->createView(),
    ));
}