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" }