Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: root
- * Date: 16/01/18
- * Time: 22:21
- */
- namespace MainBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\Annotation\Route;
- class ContactController extends Controller
- {
- public function sendEmail($data, $subject)
- {
- // $subject = implode("','",$subject);
- $message = (new \Swift_Message($subject))
- ->setFrom(array($data['email'] => $data["name"]))
- ->setTo('contact@conceptsiteweb.com')
- ->setBody(
- $this->renderView(
- '@Main/Contact/Creation/email.html.twig',
- ['message'=>$data['message'], 'email' => $data['email'], 'firstname' => $data['firstname'], 'name' => $data['name']]
- ),
- 'text/html'
- );
- return $this->get('mailer')->send($message);
- }
- /**
- * @Route("/mail-creation.html/", name="mail_creation_de_site")
- */
- public function formMailAction(Request $request)
- {
- $form = $this->createForm('MainBundle\Form\Contact\ContactCreaType',null,array(
- // To set the action use $this->generateUrl('route_identifier')
- 'action' => $this->generateUrl('mail_creation_de_site'),
- 'method' => 'POST'
- ));
- if ($request->isMethod('POST')) {
- // Refill the fields in case the form is not valid.
- $request->getSession()
- ->getFlashBag()
- ->add('success', 'Message envoyé')
- ;
- $form->handleRequest($request);
- if($form->isValid()){
- $url = ($request->headers->get('referer'));
- $routeName = $this->getRouteNameFromUrl($url);
- if($routeName === 'creation_de_site'){
- $subject= 'Création de site';
- }elseif (($routeName==='referencement')){
- $subject= 'Référencement';
- }elseif ('depannage'){
- $subject= 'depannage';
- }
- // Send mail
- if($this->sendEmail($form->getData(), $subject))
- {
- // Everything OK, redirect to wherever you want ! :
- return $this->redirectToRoute($routeName);
- }else{
- // An error ocurred, handle
- var_dump("Errooooor :(");
- }
- }
- }
- return $this->render('@Main/Contact/Creation/sendmail.html.twig', array(
- 'form' => $form->createView(),
- ));
- }
- private function getRouteNameFromUrl($url)
- {
- $parsed = parse_url($url);
- if (false === isset($parsed['path'])) {
- return null;
- }
- $route = $this->get('router')->match($parsed['path']);
- return $route['_route'] ?? null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement