Symfony 4.3 Utilisation de Mailer pour envoyer des mails.
Bonjour,
Description
Simulation de biens immobiliers et j'incorpore un formulaire de contact à chacun des biens.
Donc si je reçois un mail, je connais le nom du bien sollicité ainsi que les informations du potentiel acquéreur.
Problème
Lors de l'envoi, le redirectToRoute() fonctionne mais pas de addFlash() pourtant incorporer et pas d'email reçus. Donc le traitement du formulaire se fait mais pas de feedback(e-mail).
Ce que j'ai fait
Mon Entity
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
| <?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Contact{
/**
* @var string|null
* @Assert\NotBlank()
* @Assert\Length(
* min=2,
* max=75,
* minMessage="Le nombre minimum de caractères est de de {{ limit }}",
* maxMessage="Le nombre maximum de caractères est de de {{ limit }}"
* )
*/
private $name;
/**
* @var string|null
* @Assert\NotBlank()
* @Assert\Length(
* min=2,
* max=75,
* minMessage="Le nombre minimum de caractères est de de {{ limit }}",
* maxMessage="Le nombre maximum de caractères est de de {{ limit }}"
* )
*/
private $lastname;
/**
* @var string|null
* @Assert\NotBlank()
* @Assert\Regex(
* pattern="/[0-9]{10}/",
* message=" {{ value }} est incorrect"
* )
*/
private $phone;
/**
* @var string|null
* @Assert\Email()
*/
private $email;
/**
* @var string|null
* @Assert\NotBlank()
* @Assert\Length(
* min=10,
* max=100,
* minMessage="Le nombre minimum de caractères est de de {{ limit }}",
* maxMessage="Le nombre maximum de caractères est de de {{ limit }}"
* )
*/
private $message;
/**
* @var Ad|null
*/
private $ad;
/**
*
* @return string|null
*/
public function getName(): ?string
{
return $this->name;
}
/**
*
* @param string|null $name
* @return Contact
*/
public function setName(?string $name): Contact
{
$this->name = $name;
return $this;
}
/**
*
* @return string|null
*/
public function getLastName(): ?string
{
return $this->lastname;
}
/**
* Undocumented function
*
* @param string|null $lastName
* @return Contact
*/
public function setLastName(?string $lastName): Contact
{
$this->lastName = $lastName;
return $this;
}
/**
*
* @return string|null
*/
public function getPhone(): ?string
{
return $this->phone;
}
/**
* Undocumented function
*
* @param string|null $phone
* @return Contact
*/
public function setPhone(?string $phone): Contact
{
$this->phone = $phone;
return $this;
}
/**
*
* @return string|null
*/
public function getEmail(): ?string
{
return $this->email;
}
/**
* Undocumented function
*
* @param string|null $email
* @return Contact
*/
public function setEmail(?string $email): Contact
{
$this->email = $email;
return $this;
}
/**
*
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
/**
* Undocumented function
*
* @param string|null $message
* @return Contact
*/
public function setMessage(?string $message): Contact
{
$this->message = $message;
return $this;
}
/**
* Undocumented function
*
* @return Ad|null
*/
public function getAd(): ?Ad
{
return $this->ad;
}
/**
* Undocumented function
*
* @param Ad|null $ad
* @return Contact
*/
public function setAd(Ad $ad): Contact
{
$this->ad = $ad;
return $this;
}
} |
Controller
Je n'ai pas tout mis, seulement la méthode concernée
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?php
namespace App\Controller;
use App\Entity\Ad;
use App\Form\AdType;
use App\Entity\Contact;
use App\Entity\AdSearch;
use App\Form\ContactType;
use App\Form\AdSearchType;
use App\Notification\AdsContact;
use App\Repository\AdRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class AdController extends AbstractController
{
/**
*
* @Route("/blog/{id}", name="show_ad")
*/
public function show(Ad $article, Request $request,AdsContact $notification){
$contact= new Contact;
$contact->setAd($article);
$form=$this->createForm(ContactType::class,$contact);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$notification->sendEmail($contact);
$this->addflash(
"success",
"Votre e-mail à été envoyé"
);
return $this->redirectToRoute("show_ad",[
'id'=>$article->getId()
]);
}
return $this->render('ad/show.html.twig', [
'article'=>$article,
'form'=>$form->createView()
]);
}
} |
Mon .env
Code:
1 2 3
| GMAIL_USERNAME=MonAdresseEmailPersonnel
GMAIL_PASSWORD=MotDePasseApplication
MAILER_DSN=smtp://$GMAIL_USERNAME:$GMAIL_PASSWORD@gmail |
Le mot de passe est un mot de passe d'application en effectuant cette manipulation
https://support.google.com/mail/answer/185833?hl=fr
et aussi désactiver l'accès moins sécurisé des application
Mon AdsContact $notification
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?php
namespace App\Notification;
use App\Entity\Contact;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
class AdsContact{
/**
* @var MailerInterface
*/
private $mail;
public function __construct(MailerInterface $mail){
$this->mail=$mail;
}
public function sendEmail(Contact $contact){
$email = (new TemplatedEmail())
->from($contact->getEmail())
->to('monAdresse@gmail.com')
->subject('Contact '.$contact->getAd()->getTitle())
// path of the Twig template to render
->htmlTemplate('emails/ad_contact.html.twig')
// pass variables (name => value) to the template
->context([
'contact' => $contact
]);
$this->mail->send($email);
}
} |
Le emails/ad_contact.html.twig
Code:
1 2 3
| <h2>Mail concernant le {{contact.ad.title}}</h2>
<p>{{contact.message}}</p> |
Est-ce un problème de version de symfony? En SF4.3 rester sur SwiftMailer pour plus de stabilité?
.env mal configuré?
Merci