You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 line
4.7KB

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