[英]Send user and admin confirmation 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(),
));
}
謝謝。
我找到了解決方法,這是控制器中的解決方案。
/**
* 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(),
));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.