Mail.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\User;
  4. use Symfony\Component\Mailer\MailerInterface;
  5. use Symfony\Component\Mime\Address;
  6. use Symfony\Component\Mime\Email;
  7. use Twig\Environment;
  8. /**
  9. * Envoi de mails
  10. */
  11. class Mail
  12. {
  13. /**
  14. * Mail Manager
  15. * @param $mailer
  16. * @param $templating
  17. * @param $from
  18. * @param $reply
  19. * @param $name
  20. */
  21. public function __construct(
  22. protected MailerInterface $mailer,
  23. protected Environment $templating,
  24. protected string $from,
  25. protected string $reply,
  26. protected string $name
  27. )
  28. {
  29. }
  30. protected function sendMessage(string $subject, string $to, string $body): void
  31. {
  32. $mail = (new Email())
  33. ->from(new Address($this->from, $this->name))
  34. ->to($to)
  35. ->subject($subject)
  36. ->text($body)
  37. ->replyTo(new Address($this->reply, $this->name));
  38. $this->mailer->send($mail);
  39. }
  40. protected function setBody(string $template, User $user, string $lien): string
  41. {
  42. return $this->templating->render($template, ['user' => $user, 'lien' => $lien]);
  43. }
  44. public function sendMailActivation(User $user, string $lien): void
  45. {
  46. $subject = "Activation de votre compte";
  47. $template = 'security/mail_activate.html.twig';
  48. $to = $user->getMail();
  49. $body = $this->setBody($template, $user, $lien);
  50. $this->sendMessage($subject, $to, $body);
  51. }
  52. public function sendMailDemandeActivation(User $user, string $lien): void
  53. {
  54. $subject = "Demande d'activation";
  55. $template = 'security/mail_demande_activation.html.twig';
  56. $to = $this->from;
  57. $body = $this->setBody($template, $user, $lien);
  58. $this->sendMessage($subject, $to, $body);
  59. }
  60. public function sendMailTokenMp(User $user, string $lien): void
  61. {
  62. $subject = "Mot de passe perdu";
  63. $template = 'security/mail_tokenmdp.html.twig';
  64. $to = $user->getMail();
  65. $body = $this->setBody($template, $user, $lien);
  66. $this->sendMessage($subject, $to, $body);
  67. }
  68. }