Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

187 lines
4.6KB

  1. <?php
  2. namespace App\Email;
  3. use App\Exceptions\AppCommonException;
  4. use App\Exceptions\TempFileNotExistsException;
  5. use App\Models\Email;
  6. use App\Models\EmailAttachment;
  7. use App\Models\User;
  8. use App\Util\DateUtil;
  9. use App\Util\UrlUtil;
  10. use Illuminate\Bus\Queueable;
  11. use Illuminate\Database\Eloquent\Collection;
  12. use Illuminate\Mail\Mailable;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Support\Facades\Mail;
  15. use Illuminate\Support\Str;
  16. abstract class BaseEmailer extends Mailable
  17. {
  18. use Queueable, SerializesModels;
  19. protected $configRoot = 'mail.mailers.smtp.';
  20. protected $casts = [];
  21. protected User|null $__user = null;
  22. protected string $__email = "";
  23. protected string|null $__userId = null;
  24. /**
  25. * 添付ファイル
  26. * @var Collection<int, EmailAttachment>|null
  27. */
  28. protected ?Collection $__attachments = null;
  29. public function sendEmail(string $email)
  30. {
  31. $this->__email = $email;
  32. Mail::to($email)
  33. ->send($this);
  34. }
  35. public function setUser(User $user)
  36. {
  37. $this->__userId = $user->id;
  38. $this->__email = $user->email;
  39. return $this;
  40. }
  41. public function setEmail(string $target)
  42. {
  43. $this->__email = $target;
  44. return $this;
  45. }
  46. public function setUserId(string $userId)
  47. {
  48. $this->__userId = $userId;
  49. return $this;
  50. }
  51. public function build()
  52. {
  53. $this->text($this->getTemplateName())
  54. ->subject($this->getSubject())
  55. ->with($this->getParams());
  56. // 添付ファイル処理
  57. if ($this->__attachments !== null) {
  58. $count = $this->__attachments->count();
  59. foreach ($this->__attachments as $attachment) {
  60. $filepath = $attachment->filepath;
  61. if (!file_exists($filepath)) {
  62. $e = new TempFileNotExistsException();
  63. throw $e->setFilepath($filepath);
  64. }
  65. $as = $attachment->send_filename;
  66. $mime = $attachment->mime;
  67. $this->attach($filepath, [
  68. 'as' => $as,
  69. 'mime' => $mime,
  70. ]);
  71. }
  72. }
  73. return $this;
  74. }
  75. public function setConfigDefault()
  76. {
  77. $root = $this->configRoot;
  78. config([
  79. $root . "transport" => "smtp",
  80. $root . "host" => env('MAIL_HOST', 'smtp.mailgun.org'),
  81. $root . 'port' => env('MAIL_PORT', 587),
  82. $root . 'encryption' => env('MAIL_ENCRYPTION', 'tls'),
  83. $root . 'username' => env('MAIL_USERNAME'),
  84. $root . 'password' => env('MAIL_PASSWORD'),
  85. $root . 'timeout' => null,
  86. $root . 'auth_mode' => null,
  87. $root . 'verify_peer' => false,
  88. ]);
  89. }
  90. public function setConfig(array $config)
  91. {
  92. $root = $this->configRoot;
  93. $ret = [];
  94. foreach ($config as $key => $val) {
  95. $ret[$root . $key] = $val;
  96. }
  97. config($ret);
  98. }
  99. protected function setValues(\stdClass|array $data)
  100. {
  101. foreach ($data as $key => $val) {
  102. $this->setValue($key, $val);
  103. }
  104. }
  105. protected function setValue(string $key, $value)
  106. {
  107. $val = $value;
  108. $camel = Str::camel($key);
  109. $snake = Str::snake($key);
  110. $property = "";
  111. if (property_exists($this, $camel)) {
  112. $property = $camel;
  113. } else if (property_exists($this, $snake)) {
  114. $property = $snake;
  115. } else {
  116. return;
  117. }
  118. if (data_get($this->casts, $property) === 'datetime') {
  119. $this->$property = DateUtil::parse($val);
  120. } else {
  121. $this->$property = $val;
  122. }
  123. }
  124. public function makeModel(): Email
  125. {
  126. if ($this->__email === "") {
  127. throw new AppCommonException("Email宛先不明");
  128. }
  129. $model = new Email();
  130. $model->subject = $this->getSubject();
  131. $model->content = $this->render();
  132. $model->type = get_class($this);
  133. $model->email = $this->__email;
  134. $model->user_id = $this->__userId;
  135. return $model;
  136. }
  137. abstract public function getTemplateName(): string;
  138. abstract public function getSubject(): string;
  139. abstract public function getParams(): array;
  140. /**
  141. * 画面のURLを生成する
  142. *
  143. * @param array|string $path
  144. * @return string
  145. */
  146. protected function getAppUrl(array|string $path, array $query = []): string
  147. {
  148. return UrlUtil::getAppUrl($path, $query);
  149. }
  150. }