I have a form which is only for admins. Admins can add users with a default password 1234. I also would like to send an email to the added user after the successful creation. Everything works fine in the controller except the swift mailer part. After sending the form debug bar doesn't show any emails as sent. If I put that part out of the if statement, it works fine and I can see the email in the debug bar. However all the other parts that are in if statement works just fine.
Here is my method;
/**
* @Route("/add-user", name="adduser")
*/
public function addUserAction(Request $request){
$user = new User();
$addform = $this->createForm(NewUserType::class,$user);
$addform->handleRequest($request);
if ($addform->isSubmitted() && $addform->isValid()) {
$user->setRoles(array('ROLE_USER'));
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$cellphone = $addform->get('cellphone')->getData();
$ccode = $addform->get('ccode')->getData();
$cellphone = preg_replace("/[^0-9A-Za-z]/", "", $cellphone);
$user->setCcode($ccode);
$user->setCellphone($cellphone);
$user->setPlainPassword(1234);
$user->setPassword(md5(1234));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$this->addFlash(
'addeduser',
'You successfully added a user to the database'
);
$emailMessage = \Swift_Message::newInstance()
->setSubject('You have successfully signed up')
->setFrom('info@kampapp.com')
->setTo($user->getUsername())
->setBody($this->renderView(':emails:addeduser.html.twig'));
$this->get('mailer')->send($emailMessage);
return $this->redirect($this->generateUrl('adduser'));
}
else{
$this->addFlash(
'addusererror',
'Oops! There was an error!'
);
}
return $this->render(':user:adduser.html.twig', array('addform'=>$addform->createView()));
}
答案 0 :(得分:1)
如果您发送电子邮件然后立即重定向到其他页面(您的情况),则网络调试工具栏将不会在下一页上显示电子邮件图标或报告
相反,您可以在intercept_redirects
文件中将true
选项设置为config_dev.yml
,这将导致重定向停止并允许您打开包含已发送电子邮件详细信息的报告
# app/config/config_dev.yml
web_profiler:
intercept_redirects: true
请参阅documentation。
中的详情