I’m a beginner with Symfony, and I have to put a Mailer in my web service. But I have a php exception that I don’t understand.
There is my mailer function :
private function sendMail(MailerInterface $mailer, $p_leTI, $mail) { $pole = $p_leTI['pole']; $type = $pole === 1 ? 'Logiciel' : ($pole === 2 ? 'Matériel' : 'Incident'); $client = $p_leTI['nomCli']; $email = (new Email()) ->from(new Address('random@gmail.com', 'Support Idéation Informatique')) ->to(new Address('random@gmail.com')) ->bcc(new Address('random@gmail.com')) ->subject("Ticket $type Site: $client") ->text("Client: $clientrnObservation: {$p_leTI['OBSERVATION']}") ->html("<html lang='fr'> <body> <p>Demandeur: {$p_leTI['nomCli']}</p> <p>Description: {$p_leTI['OBSERVATION']}</p> <p>Téléphone: {$p_leTI['TEL']}</p> <p>Email: {$p_leTI['EMAILCLIENT']}</p> </body> </html>"); $mailer->send($email); $email = (new TemplatedEmail()) ->from(new Address('random@gmail.com', 'Support Idéation Informatique')) ->bcc(new Address('random@gmail.com')) ->subject("Idéation Informatique - Votre demande a bien été enregistrée (Ticket n°" . $$p_leTI['IDTICKETINCIDENT'] . ")"); if (!empty($mail)) try { $email->to(new Address($mail)); } catch (Exception $e) { } $email->htmlTemplate('emails/mail.html.twig') ->context([ 'client' => $client, 'idticket' => $p_leTI['IDTICKETINCIDENT'], 'observation' => $p_leTI['OBSERVATION'] ]) ->text("Ouverture du ticket incident n°" . $p_leTI['IDTICKETINCIDENT'] . "rn" . $p_leTI['OBSERVATION']); $mailer->send($email); return true; }
And there is the error in my log :
PHP Exception SymfonyComponentDependencyInjectionExceptionEnvNotFoundException: "Environment variable not found: "MAILER_DSN"."dency-injection/EnvVarProcessor.php
EnvVarProcessor.php is a file I never worked with so I don’t understand, like I said I’m a beginner so I might have forgot something.
Thank’s for you’re help.
Advertisement
Answer
You have to set the parameters how your mails should be sent via an environment variable (there are plenty options but there are some common ways).
Most easy one as documented is to update the .env
file with:
MAILER_DSN=smtp://user:pass@smtp.example.com:port
Comment: EnvVarProcessor.php
is just the component to set the env variables in the Symfony container based on the different options one has to set them.