3

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: $client\r\nObservation: {$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'] . "\r\n" . $p_leTI['OBSERVATION']);

             $mailer->send($email);
             return true;
          }

And there is the error in my log :

PHP Exception Symfony\Component\DependencyInjection\Exception\EnvNotFoundException: "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.

NeoKerd
  • 189
  • 3
  • 13
  • 3
    Error message is pretty clear, you have to [create an environment variable `MAILER_DSN`](https://symfony.com/doc/current/mailer.html#transport-setup) – Cid Jul 10 '20 at 07:34

1 Answers1

5

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.

LBA
  • 3,859
  • 2
  • 21
  • 60
  • 4
    Note that if you're using some real credentials, you want to write it in `.env.local` instead of `.env` so it is not committed. – Altherius Jul 10 '20 at 07:57
  • 3
    URL might need to be URL encoded first if there are special characters in the password in example (like `@` becomes `%40`) – Cid Jul 10 '20 at 08:02