Mailer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Tools\Mailer;
  3. use Cake\Core\Configure;
  4. use Cake\I18n\I18n;
  5. use Cake\Mailer\Mailer as CakeMailer;
  6. /**
  7. * Allows locale overwrite to send emails in a specific language
  8. *
  9. * @method string addEmbeddedAttachment(string $file, ?string $name = null, array $options = [])
  10. * @method string addEmbeddedBlobAttachment(string $file, ?string $name = null, array $options = [])
  11. * @method string addEmbeddedAttachmentByContentId(string $file, ?string $name = null, array $options = [])
  12. * @method string addEmbeddedBlobAttachmentByContentId(string $file, ?string $name = null, array $options = [])
  13. */
  14. class Mailer extends CakeMailer {
  15. /**
  16. * Message class name.
  17. *
  18. * @var string
  19. * @psalm-var class-string<\Cake\Mailer\Message>
  20. */
  21. protected $messageClass = Message::class;
  22. /**
  23. * @var string|null
  24. */
  25. protected $locale;
  26. /**
  27. * @var string
  28. */
  29. protected $localeBefore;
  30. /**
  31. * @var array
  32. */
  33. protected $debug = [];
  34. /**
  35. * Magic method to forward method class to Message instance.
  36. *
  37. * @param string $method Method name.
  38. * @param array $args Method arguments
  39. * @return \Cake\Mailer\Mailer|mixed
  40. */
  41. public function __call(string $method, array $args) {
  42. $result = $this->message->$method(...$args);
  43. if (strpos($method, 'get') === 0 || strpos($method, 'add') === 0) {
  44. return $result;
  45. }
  46. return $this;
  47. }
  48. /**
  49. * @param string $locale
  50. *
  51. * @return $this
  52. */
  53. public function setLocale(string $locale) {
  54. $this->locale = $locale;
  55. return $this;
  56. }
  57. /**
  58. * @return string|null
  59. */
  60. public function getLocale(): ?string {
  61. return $this->locale;
  62. }
  63. /**
  64. * Validate if the email has the required fields necessary to make send() work.
  65. * Assumes layouting (does not check on content to be present or if view/layout files are missing).
  66. *
  67. * @return bool Success
  68. */
  69. public function validates() {
  70. if ($this->getMessage()->getSubject() && $this->getMessage()->getTo()) {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /**
  76. * @inheritDoc
  77. */
  78. public function send(?string $action = null, array $args = [], array $headers = []): array {
  79. $this->fixateLocale();
  80. $result = parent::send($action, $args, $headers);
  81. $this->restoreLocale();
  82. return $result;
  83. }
  84. /**
  85. * Switch to primary locale if applicable.
  86. *
  87. * @return void
  88. */
  89. protected function fixateLocale() {
  90. $this->localeBefore = I18n::getLocale();
  91. if ($this->locale) {
  92. I18n::setLocale($this->locale);
  93. return;
  94. }
  95. $primaryLocale = $this->detectPrimaryLocale();
  96. if ($primaryLocale) {
  97. I18n::setLocale($primaryLocale);
  98. }
  99. }
  100. /**
  101. * Restore to current locale if applicable.
  102. *
  103. * @return void
  104. */
  105. protected function restoreLocale() {
  106. I18n::setLocale($this->localeBefore);
  107. }
  108. /**
  109. * Returns the configured default locale.
  110. *
  111. * Can be based on the primary language and the allowed languages (whitelist).
  112. *
  113. * @throws \RuntimeException
  114. * @return string|null
  115. */
  116. protected function detectPrimaryLocale(): ?string {
  117. if (Configure::read('Config.defaultLocale')) {
  118. return Configure::read('Config.defaultLocale');
  119. }
  120. $primaryLanguage = Configure::read('Config.defaultLanguage');
  121. $primaryLocale = Configure::read('Config.allowedLanguages.' . $primaryLanguage . '.locale');
  122. return $primaryLocale;
  123. }
  124. /**
  125. * Render content and send email using configured transport.
  126. *
  127. * @psalm-return array{headers: string, message: string}
  128. * @param string $content Content.
  129. * @return array
  130. */
  131. public function deliver(string $content = '') {
  132. $this->debug = parent::deliver($content);
  133. return $this->debug;
  134. }
  135. /**
  136. * @return array
  137. */
  138. public function getDebug(): array{
  139. return $this->debug;
  140. }
  141. /**
  142. * @return $this
  143. */
  144. public function reset() {
  145. $this->locale = null;
  146. $this->debug = [];
  147. return parent::reset();
  148. }
  149. }