Mail.php 1.8 KB

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