Mailer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. use PHPMailer\PHPMailer\PHPMailer;
  4. use PHPMailer\PHPMailer\Exception;
  5. /**
  6. * Allow to send emails.
  7. *
  8. * The Minz_Mailer class must be inherited by classes under app/Mailers.
  9. * They work similarly to the ActionControllers in the way they have a view to
  10. * which you can pass params (eg. $this->view->foo = 'bar').
  11. *
  12. * The view file is not determined automatically, so you have to select one
  13. * with, for instance:
  14. *
  15. * ```
  16. * $this->view->_path('user_mailer/email_need_validation.txt.php')
  17. * ```
  18. *
  19. * Minz_Mailer uses the PHPMailer library under the hood. The latter requires
  20. * PHP >= 5.5 to work. If you instantiate a Minz_Mailer with PHP < 5.5, a
  21. * warning will be logged.
  22. *
  23. * The email is sent by calling the `mail` method.
  24. */
  25. class Minz_Mailer {
  26. /**
  27. * The view attached to the mailer.
  28. * You should set its file with `$this->view->_path($path)`
  29. *
  30. * @var Minz_View
  31. */
  32. protected $view;
  33. private string $mailer;
  34. /** @var array{'hostname':string,'host':string,'auth':bool,'username':string,'password':string,'secure':string,'port':int,'from':string} */
  35. private array $smtp_config;
  36. private int $debug_level;
  37. /**
  38. * Constructor.
  39. */
  40. public function __construct () {
  41. $this->view = new Minz_View();
  42. $this->view->_layout(null);
  43. $this->view->attributeParams();
  44. $conf = Minz_Configuration::get('system');
  45. $this->mailer = $conf->mailer;
  46. $this->smtp_config = $conf->smtp;
  47. // According to https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging#debug-levels
  48. // we should not use debug level above 2 unless if we have big trouble
  49. // to connect.
  50. if ($conf->environment === 'development') {
  51. $this->debug_level = 2;
  52. } else {
  53. $this->debug_level = 0;
  54. }
  55. }
  56. /**
  57. * Send an email.
  58. *
  59. * @param string $to The recipient of the email
  60. * @param string $subject The subject of the email
  61. * @return bool true on success, false if a SMTP error happens
  62. */
  63. public function mail(string $to, string $subject): bool {
  64. ob_start();
  65. $this->view->render();
  66. $body = ob_get_contents() ?: '';
  67. ob_end_clean();
  68. PHPMailer::$validator = 'html5';
  69. $mail = new PHPMailer(true);
  70. try {
  71. // Server settings
  72. $mail->SMTPDebug = $this->debug_level;
  73. $mail->Debugoutput = 'error_log';
  74. if ($this->mailer === 'smtp') {
  75. $mail->isSMTP();
  76. $mail->Hostname = $this->smtp_config['hostname'];
  77. $mail->Host = $this->smtp_config['host'];
  78. $mail->SMTPAuth = $this->smtp_config['auth'];
  79. $mail->Username = $this->smtp_config['username'];
  80. $mail->Password = $this->smtp_config['password'];
  81. $mail->SMTPSecure = $this->smtp_config['secure'];
  82. $mail->Port = $this->smtp_config['port'];
  83. } else {
  84. $mail->isMail();
  85. }
  86. // Recipients
  87. $mail->setFrom($this->smtp_config['from']);
  88. $mail->addAddress($to);
  89. // Content
  90. $mail->isHTML(false);
  91. $mail->CharSet = 'utf-8';
  92. $mail->Subject = $subject;
  93. $mail->Body = $body;
  94. $mail->send();
  95. return true;
  96. } catch (Exception $e) {
  97. Minz_Log::error('Minz_Mailer cannot send a message: ' . $mail->ErrorInfo);
  98. return false;
  99. }
  100. }
  101. }