如何从一个重叠的表单收到一封电子邮件到swiftmailer?

时间:2016-07-28 08:31:18

标签: forms symfony email swiftmailer

我想将一个来自imcoicated表单的电子邮件插入到swiftmailer中。 电子邮件是swifmailer的“sendTo”部分。

我尝试过它不起作用。表单已发送,但未收到任何电子邮件。

我怎么能拥有它?你有什么想法吗?

所以控制器,发送表单然后发送电子邮件的动作是:

/**
     * Creates a new Reservations entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new Reservations();
        $emailPool = new Pool();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {	
        	$em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            // Get the sender's email adress
            $sender = $entity->getEmail();

            // Get the recipients' emails adresses (pool address)
            $emailPool = $this->$pool->getEmailPool(); // mal codé >> trouver la bonne méthode

            // Send email
            $message = \Swift_Message::newInstance()
                    ->setSubject('Demande de véhicule')
                    ->setFrom($sender)
                    ->setTo($emailPool) // email à entrer Vehicule.Esplanade@eurelien.fr

                    // Indicate "High" priority
                    ->setPriority(2)
                    ->setBody(
                        $this->renderView(
                            // View in app/Resources/views/emails/demandereservation.html.twig
                            'emails/demandereservation.html.twig', array(
                                'reservations' => $entity)),
                                'text/html'
                        );
            $this->get('mailer')->send($message);
            $this->get('session')->getFlashBag()->Add('notice', 'Votre réservation a bien été envoyée');

            return $this->redirect($this->generateUrl('reservations_show', array('id' => $entity->getId())));
        }

        return $this->render('CDCarsBundle:Reservations:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

表格(带有叠加形式(池))是:

<?php

namespace CD\CarsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use CD\CarsBundle\Entity\Reservations;
use CD\CarsBundle\Entity\Vehicules;
use Application\Sonata\UserBundle\Entity\User;

class ReservationsType extends AbstractType
{
    // Form for the entity "Reservations" which is used to build the car's booking form

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nomAgent', null, array(
                'label' => 'Nom de l\'agent', 
                //'attr' => array(
                    //'readonly' => true,
                    //'disabled' => true
                //)
            ))
            ->add('prenomAgent', null, array(
                'label' => 'Prénom de l\'agent',
                //'attr' => array(
                    //'readonly' => true,
                    //'disabled' => true
                //)
            ))
            ->add('dga', null, array(
                'label' => 'D.G.A',
                //'attr' => array(
                    //'readonly' => true,
                    //'disabled' => true
                //)
            ))
            ->add('direction', null, array(
                'label' => 'Direction',
                //'attr' => array(
                    //'readonly' => true,
                    //'disabled' => true
                //)
            ))
            ->add('email', null, array(
                'label' => 'Email',
                //'attr' => array(
                    //'readonly' => true,
                    //'disabled' => true
                //)
            ))
            ->add('telephone', null, array(
                'label' => 'Téléphone',
                //'attr' => array(
                    //'readonly' => true,
                    //'disabled' => true
                //)
            ))
         
            // ajouter le pool
            ->add('pool',  new PoolType())
         
            
            ->add('heureDebut', null, array(
                'label' => 'Date et heure de début',
                'format' => 'dd-MM-yyyy H:i',
                'years' => range(\date("Y") - 0, \date("Y") + 2),
                )
            )
            ->add('heureFin', null, array(
                'label' => 'Date et heure de fin',
                'format' => 'dd-MM-yyyy H:i',
                'years' => range(\date("Y") - 0, \date("Y") + 2),
                )
            )

            // ajouter type véhicule
             
            ->add('besoin', 'choice', array(
                'label' => 'Type',
                'choices' => array(
                    'V.L' => 'V.L',
                    'V.L.E' => 'V.L.E',
                    'V.U' => 'V.U',
                    'velo' => 'Vélo')
                )
            )

            // ajouter nombre personnes
            ->add('nombrePersonne', 'choice', array(
                'label' => 'Nombre de personne',
                'choices' => array(
                    '1' => '1',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5 +')
                )
            )
           
             
            
            
             // ajouter demande de remisage -> si coché dévoiler champs pour le remisage (dématérialisation) => à faire dans la vue

            ->add('remisage', null, array('required' => false))

            ->add('adresseRemisage', null, array('label' => 'Adresse'))
            ->add('dateDebutRemisage', null, array(
                'label' => 'Du',
                'format' => 'dd-MM-yyyy H:i',
                'years' => range(\date("Y") - 0, \date("Y") + 2),
                )
            )
            ->add('dateFinRemisage', null, array(
                'label' => 'au',
                'format' => 'dd-MM-yyyy H:i',
                'years' => range(\date("Y") - 0, \date("Y") + 2),
                )
            )
            ->add('emailDirecteur', null, array(
                'label' => 'Email du directeur',
                'attr' => array(
                    'placeholder' => 'email@email.fr',
                ))
            )          
            
            ->add('destination', null, array('label' => 'Destination'))
            ->add('motifRdv', null, array('required' => false))
            ->add('motifFormation', null, array('required' => false))
            ->add('motifReunion', null, array('required' => false))
            ->add('motifCollecte', null, array('required' => false))
            ->add('motifInstallation', null, array('required' => false))
            ->add('motifProgrammation', null, array('required' => false))
            ->add('motifDepannage', null, array('required' => false))
            ->add('motifVad', null, array('required' => false))
            ->add('motifAutre', null, array('label' => 'Autre motif'))
            ->add('conducteur', null, array('required' => false))

            // ajouter mandataire -> si coché dévoiler champs pour le mandataire (email...) => à faire dans la vue
            ->add('mandataire', null, array('required' => false))

            ->add('nomMandataire', null, array('label' => 'Votre nom'))
            ->add('prenomMandataire', null, array('label' => 'Votre prénom'))
            ->add('emailMandataire', null, array('label' => 'Votre émail'))
            ->add('honneur', null, array('required' => true))
        ;
    }

Pool表格是:

<?php

namespace CD\CarsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use CD\CarsBundle\Entity\Pool;
use CD\CarsBundle\Entity\Vehicules;

class PoolType extends AbstractType
{
    // Form for the entity "pool"
    
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            /*->add('nom', null, array(
                'label' => 'Nom',
            ))*/
            ->add('emailPool', null, array(
                'label' => 'Email du pool duquel vous dépendez',
            ))
        ;
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'CD\CarsBundle\Entity\Pool'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'cd_carsbundle_pool';
    }
}

池实体是:

<?php

namespace CD\CarsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Pool
 */
class Pool
{
    // Code for the entity "Pool"

    public function __toString()
    {
        return (string) $this->getEmailPool();
    }
    
    //YML GENERATED CODE
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $nom;

    /**
     * @var string
     */
    private $emailPool;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $vehicules;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $user;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $reservations;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->vehicules = new \Doctrine\Common\Collections\ArrayCollection();
        $this->user = new \Doctrine\Common\Collections\ArrayCollection();
        $this->reservations = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nom
     *
     * @param string $nom
     * @return Pool
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string 
     */
    public function getNom()
    {
        return $this->nom;
    }

    /**
     * Set emailPool
     *
     * @param string $emailPool
     * @return Pool
     */
    public function setEmailPool($emailPool)
    {
        $this->emailPool = $emailPool;

        return $this;
    }

    /**
     * Get emailPool
     *
     * @return string 
     */
    public function getEmailPool()
    {
        return $this->emailPool;
    }

    /**
     * Add vehicules
     *
     * @param \CD\CarsBundle\Entity\Vehicules $vehicules
     * @return Pool
     */
    public function addVehicule(\CD\CarsBundle\Entity\Vehicules $vehicules)
    {
        $this->vehicules[] = $vehicules;

        return $this;
    }

    /**
     * Remove vehicules
     *
     * @param \CD\CarsBundle\Entity\Vehicules $vehicules
     */
    public function removeVehicule(\CD\CarsBundle\Entity\Vehicules $vehicules)
    {
        $this->vehicules->removeElement($vehicules);
    }

    /**
     * Get vehicules
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getVehicules()
    {
        return $this->vehicules;
    }

    /**
     * Add user
     *
     * @param \Application\Sonata\UserBundle\Entity\User $user
     * @return Pool
     */
    public function addUser(\Application\Sonata\UserBundle\Entity\User $user)
    {
        $this->user[] = $user;

        return $this;
    }

    /**
     * Remove user
     *
     * @param \Application\Sonata\UserBundle\Entity\User $user
     */
    public function removeUser(\Application\Sonata\UserBundle\Entity\User $user)
    {
        $this->user->removeElement($user);
    }

    /**
     * Get user
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Add reservations
     *
     * @param \CD\CarsBundle\Entity\Reservations $reservations
     * @return Pool
     */
    public function addReservation(\CD\CarsBundle\Entity\Reservations $reservations)
    {
        $this->reservations[] = $reservations;

        return $this;
    }

    /**
     * Remove reservations
     *
     * @param \CD\CarsBundle\Entity\Reservations $reservations
     */
    public function removeReservation(\CD\CarsBundle\Entity\Reservations $reservations)
    {
        $this->reservations->removeElement($reservations);
    }

    /**
     * Get reservations
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getReservations()
    {
        return $this->reservations;
    }
}

预订实体是:

<?php

namespace CD\CarsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints as Asserts;

/**
 * Reservations
 */
class Reservations
{
    // Code for the entity "Reservations"

    public function __toString()
    {
        return (string) $this->getId();
        return (string) $this->getHeureDebut();
    }

    

    // YML GENERATED CODE 
    
    
    /**
     * @var integer
     */
    private $id;

    /**
     * @var \DateTime
     */
    private $heureDebut;

    /**
     * @var \DateTime
     */
    private $heureFin;

    /**
     * @var string
     */
    private $nomAgent;

    /**
     * @var string
     */
    private $prenomAgent;

    /**
     * @var string
     */
    private $dga;

    /**
     * @var string
     */
    private $direction;

    /**
     * @var string
     */
    private $email;

    /**
     * @var string
     */
    private $telephone;

    /**
     * @var string
     */
    private $destination;

    /**
     * @var boolean
     */
    private $reserve;

    /**
     * @var boolean
     */
    private $annulation;

    /**
     * @var boolean
     */
    private $remisage;

    /**
     * @var string
     */
    private $adresseRemisage;

    /**
     * @var \DateTime
     */
    private $dateDebutRemisage;

    /**
     * @var \DateTime
     */
    private $dateFinRemisage;

    /**
     * @var string
     */
    private $emailDirecteur;

    /**
     * @var boolean
     */
    private $conducteur;

    /**
     * @var boolean
     */
    private $mandataire;

    /**
     * @var boolean
     */
    private $motifRdv;

    /**
     * @var boolean
     */
    private $motifFormation;

    /**
     * @var boolean
     */
    private $motifReunion;

    /**
     * @var boolean
     */
    private $motifCollecte;

    /**
     * @var boolean
     */
    private $motifInstallation;

    /**
     * @var boolean
     */
    private $motifProgrammation;

    /**
     * @var boolean
     */
    private $motifDepannage;

    /**
     * @var boolean
     */
    private $motifVad;

    /**
     * @var string
     */
    private $motifAutre;

    /**
     * @var string
     */
    private $commentaires;

    /**
     * @var integer
     */
    private $nombrePersonne;

    /**
     * @var string
     */
    private $besoin;

    /**
     * @var string
     */
    private $nomMandataire;

    /**
     * @var string
     */
    private $prenomMandataire;

    /**
     * @var string
     */
    private $emailMandataire;

    /**
     * @var boolean
     */
    private $honneur;

    /**
     * @var boolean
     */
    private $traite;

    /**
     * @var \CD\CarsBundle\Entity\Vehicules
     */
    private $vehicules;

    /**
     * @var \Application\Sonata\UserBundle\Entity\User
     */
    private $user;

    /**
     * @var \CD\CarsBundle\Entity\Pool
     */
    private $pool;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set heureDebut
     *
     * @param \DateTime $heureDebut
     * @return Reservations
     */
    public function setHeureDebut($heureDebut)
    {
        $this->heureDebut = $heureDebut;

        return $this;
    }

    /**
     * Get heureDebut
     *
     * @return \DateTime 
     */
    public function getHeureDebut()
    {
        return $this->heureDebut;
    }

    /**
     * Set heureFin
     *
     * @param \DateTime $heureFin
     * @return Reservations
     */
    public function setHeureFin($heureFin)
    {
        $this->heureFin = $heureFin;

        return $this;
    }

    /**
     * Get heureFin
     *
     * @return \DateTime 
     */
    public function getHeureFin()
    {
        return $this->heureFin;
    }

    /**
     * Set nomAgent
     *
     * @param string $nomAgent
     * @return Reservations
     */
    public function setNomAgent($nomAgent)
    {
        $this->nomAgent = $nomAgent;

        return $this;
    }

    /**
     * Get nomAgent
     *
     * @return string 
     */
    public function getNomAgent()
    {
        return $this->nomAgent;
    }

    /**
     * Set prenomAgent
     *
     * @param string $prenomAgent
     * @return Reservations
     */
    public function setPrenomAgent($prenomAgent)
    {
        $this->prenomAgent = $prenomAgent;

        return $this;
    }

    /**
     * Get prenomAgent
     *
     * @return string 
     */
    public function getPrenomAgent()
    {
        return $this->prenomAgent;
    }

    /**
     * Set dga
     *
     * @param string $dga
     * @return Reservations
     */
    public function setDga($dga)
    {
        $this->dga = $dga;

        return $this;
    }

    /**
     * Get dga
     *
     * @return string 
     */
    public function getDga()
    {
        return $this->dga;
    }

    /**
     * Set direction
     *
     * @param string $direction
     * @return Reservations
     */
    public function setDirection($direction)
    {
        $this->direction = $direction;

        return $this;
    }

    /**
     * Get direction
     *
     * @return string 
     */
    public function getDirection()
    {
        return $this->direction;
    }

    /**
     * Set email
     *
     * @param string $email
     * @return Reservations
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set telephone
     *
     * @param string $telephone
     * @return Reservations
     */
    public function setTelephone($telephone)
    {
        $this->telephone = $telephone;

        return $this;
    }

    /**
     * Get telephone
     *
     * @return string 
     */
    public function getTelephone()
    {
        return $this->telephone;
    }

    /**
     * Set destination
     *
     * @param string $destination
     * @return Reservations
     */
    public function setDestination($destination)
    {
        $this->destination = $destination;

        return $this;
    }

    /**
     * Get destination
     *
     * @return string 
     */
    public function getDestination()
    {
        return $this->destination;
    }

    /**
     * Set reserve
     *
     * @param boolean $reserve
     * @return Reservations
     */
    public function setReserve($reserve)
    {
        $this->reserve = $reserve;

        return $this;
    }

    /**
     * Get reserve
     *
     * @return boolean 
     */
    public function getReserve()
    {
        return $this->reserve;
    }

    /**
     * Set annulation
     *
     * @param boolean $annulation
     * @return Reservations
     */
    public function setAnnulation($annulation)
    {
        $this->annulation = $annulation;

        return $this;
    }

    /**
     * Get annulation
     *
     * @return boolean 
     */
    public function getAnnulation()
    {
        return $this->annulation;
    }

    /**
     * Set remisage
     *
     * @param boolean $remisage
     * @return Reservations
     */
    public function setRemisage($remisage)
    {
        $this->remisage = $remisage;

        return $this;
    }

    /**
     * Get remisage
     *
     * @return boolean 
     */
    public function getRemisage()
    {
        return $this->remisage;
    }

    /**
     * Set adresseRemisage
     *
     * @param string $adresseRemisage
     * @return Reservations
     */
    public function setAdresseRemisage($adresseRemisage)
    {
        $this->adresseRemisage = $adresseRemisage;

        return $this;
    }

    /**
     * Get adresseRemisage
     *
     * @return string 
     */
    public function getAdresseRemisage()
    {
        return $this->adresseRemisage;
    }

    /**
     * Set dateDebutRemisage
     *
     * @param \DateTime $dateDebutRemisage
     * @return Reservations
     */
    public function setDateDebutRemisage($dateDebutRemisage)
    {
        $this->dateDebutRemisage = $dateDebutRemisage;

        return $this;
    }

    /**
     * Get dateDebutRemisage
     *
     * @return \DateTime 
     */
    public function getDateDebutRemisage()
    {
        return $this->dateDebutRemisage;
    }

    /**
     * Set dateFinRemisage
     *
     * @param \DateTime $dateFinRemisage
     * @return Reservations
     */
    public function setDateFinRemisage($dateFinRemisage)
    {
        $this->dateFinRemisage = $dateFinRemisage;

        return $this;
    }

    /**
     * Get dateFinRemisage
     *
     * @return \DateTime 
     */
    public function getDateFinRemisage()
    {
        return $this->dateFinRemisage;
    }

    /**
     * Set emailDirecteur
     *
     * @param string $emailDirecteur
     * @return Reservations
     */
    public function setEmailDirecteur($emailDirecteur)
    {
        $this->emailDirecteur = $emailDirecteur;

        return $this;
    }

    /**
     * Get emailDirecteur
     *
     * @return string 
     */
    public function getEmailDirecteur()
    {
        return $this->emailDirecteur;
    }

    
    /**
     * Set vehicules
     *
     * @param \CD\CarsBundle\Entity\Vehicules $vehicules
     * @return Reservations
     */
    public function setVehicules(\CD\CarsBundle\Entity\Vehicules $vehicules = null)
    {
        $this->vehicules = $vehicules;

        return $this;
    }

    /**
     * Get vehicules
     *
     * @return \CD\CarsBundle\Entity\Vehicules 
     */
    public function getVehicules()
    {
        return $this->vehicules;
    }

    /**
     * Set user
     *
     * @param \Application\Sonata\UserBundle\Entity\User $user
     * @return Reservations
     */
    public function setUser(\Application\Sonata\UserBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \Application\Sonata\UserBundle\Entity\User 
     */
    public function getUser()
    {
        return $this->user;
    }

    /**
     * Set pool
     *
     * @param \CD\CarsBundle\Entity\Pool $pool
     * @return Reservations
     */
    public function setPool(\CD\CarsBundle\Entity\Pool $pool = null)
    {
        $this->pool = $pool;

        return $this;
    }

    /**
     * Get pool
     *
     * @return \CD\CarsBundle\Entity\Pool 
     */
    public function getPool()
    {
        return $this->pool;
    }
}

谢谢。祝你有愉快的一天。

1 个答案:

答案 0 :(得分:0)

我已经弄清楚了。 在控制器中,在swiftmailer部分,获取收件人电子邮件的行是:

 // Get the recipients' emails adresses (pool address)
            $recipients = $entity->getPool()->getEmailPool(); 

像这样有效。

感谢所有阅读此帖的人,如果您有其他答案,请随时发布。

度过美好的一天!