Mailer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * The email is sent by calling the `mail` method.
  20. */
  21. class Minz_Mailer {
  22. /**
  23. * The view attached to the mailer.
  24. * You should set its file with `$this->view->_path($path)`
  25. *
  26. * @var Minz_View
  27. */
  28. protected $view;
  29. private string $mailer;
  30. /** @var array{'hostname':string,'host':string,'auth':bool,'username':string,'password':string,'secure':string,'port':int,'from':string} */
  31. private array $smtp_config;
  32. private int $debug_level;
  33. /**
  34. * @phpstan-param class-string|'' $viewType
  35. * @param string $viewType Name of the class (inheriting from Minz_View) to use for the view model
  36. * @throws Minz_ConfigurationException
  37. */
  38. public function __construct(string $viewType = '') {
  39. $view = null;
  40. if ($viewType !== '' && class_exists($viewType)) {
  41. $view = new $viewType();
  42. if (!($view instanceof Minz_View)) {
  43. $view = null;
  44. }
  45. }
  46. $this->view = $view ?? new Minz_View();
  47. $this->view->_layout(null);
  48. $this->view->attributeParams();
  49. $conf = Minz_Configuration::get('system');
  50. $this->mailer = $conf->mailer;
  51. $this->smtp_config = $conf->smtp;
  52. // According to https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging#debug-levels
  53. // we should not use debug level above 2 unless if we have big trouble
  54. // to connect.
  55. if ($conf->environment === 'development') {
  56. $this->debug_level = 2;
  57. } else {
  58. $this->debug_level = 0;
  59. }
  60. }
  61. /**
  62. * Send an email.
  63. *
  64. * @param string $to The recipient of the email
  65. * @param string $subject The subject of the email
  66. * @return bool true on success, false if a SMTP error happens
  67. */
  68. public function mail(string $to, string $subject): bool {
  69. ob_start();
  70. $this->view->render();
  71. $body = ob_get_contents() ?: '';
  72. ob_end_clean();
  73. PHPMailer::$validator = 'html5';
  74. $mail = new PHPMailer(true);
  75. try {
  76. // Server settings
  77. $mail->SMTPDebug = $this->debug_level;
  78. $mail->Debugoutput = 'error_log';
  79. if ($this->mailer === 'smtp') {
  80. $mail->isSMTP();
  81. $mail->Hostname = $this->smtp_config['hostname'];
  82. $mail->Host = $this->smtp_config['host'];
  83. $mail->SMTPAuth = $this->smtp_config['auth'];
  84. $mail->Username = $this->smtp_config['username'];
  85. $mail->Password = $this->smtp_config['password'];
  86. $mail->SMTPSecure = $this->smtp_config['secure'];
  87. $mail->Port = $this->smtp_config['port'];
  88. } else {
  89. $mail->isMail();
  90. }
  91. // Recipients
  92. $mail->setFrom($this->smtp_config['from']);
  93. $mail->addAddress($to);
  94. // Content
  95. $mail->isHTML(false);
  96. $mail->CharSet = 'utf-8';
  97. $mail->Subject = $subject;
  98. $mail->Body = $body;
  99. $mail->send();
  100. return true;
  101. } catch (Exception $e) {
  102. Minz_Log::error('Minz_Mailer cannot send a message: ' . $mail->ErrorInfo);
  103. return false;
  104. }
  105. }
  106. }