|
- <?php
-
- namespace App\Email;
-
- use App\Exceptions\AppCommonException;
- use App\Models\Email;
- use App\Models\EmailAttachment;
- use App\Models\User;
- use App\Util\DateUtil;
- use Illuminate\Bus\Queueable;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Mail\Mailable;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Mail;
- use Illuminate\Support\Str;
-
- abstract class BaseEmailer extends Mailable
- {
- use Queueable, SerializesModels;
-
- protected $configRoot = 'mail.mailers.smtp_ht.';
-
- protected $casts = [];
-
- protected User|null $__user = null;
- protected string $__email = "";
- protected string|null $__userId = null;
- protected string|null $__contractId = null;
- protected string|null $__receiptIssuingOrderId = null;
-
- /**
- * 添付ファイル
- * @var Collection<int, EmailAttachment>|null
- */
- protected ?Collection $__attachments = null;
-
- public function sendEmail(string $email)
- {
- $this->__email = $email;
- Mail::to($email)
- ->send($this);
- }
-
- public function setUser(User $user)
- {
- $this->__userId = $user->id;
- $this->__email = $user->email;
- return $this;
- }
-
- public function setEmail(string $target)
- {
- $this->__email = $target;
- return $this;
- }
-
- public function setUserId(string $userId)
- {
- $this->__userId = $userId;
- return $this;
- }
-
- public function setContractId(string $contractId)
- {
- $this->__contractId = $contractId;
- return $this;
- }
-
- public function setReceiptIssuingOrderId(string $receiptIssuingOrderId)
- {
- $this->__receiptIssuingOrderId = $receiptIssuingOrderId;
- return $this;
- }
-
- public function build()
- {
- $this->text($this->getTemplateName())
- ->subject($this->getSubject())
- ->with($this->getParams());
-
- // 添付ファイル処理
- if ($this->__attachments !== null) {
- $count = $this->__attachments->count();
- foreach ($this->__attachments as $attachment) {
- $filepath = $attachment->filepath;
- $as = $attachment->send_filename;
- $mime = $attachment->mime;
- $this->attach($filepath, [
- 'as' => $as,
- 'mime' => $mime,
- ]);
- }
- }
-
- return $this;
- }
-
- public function setConfigDefault()
- {
- $root = $this->configRoot;
- config([
- $root . "transport" => "smtp",
- $root . "host" => env('MAIL_HOST', 'smtp.mailgun.org'),
- $root . 'port' => env('MAIL_PORT', 587),
- $root . 'encryption' => env('MAIL_ENCRYPTION', 'tls'),
- $root . 'username' => env('MAIL_USERNAME'),
- $root . 'password' => env('MAIL_PASSWORD'),
- $root . 'timeout' => null,
- $root . 'auth_mode' => null,
- $root . 'verify_peer' => false,
- ]);
- }
-
- public function setConfig(array $config)
- {
- $root = $this->configRoot;
- $ret = [];
- foreach ($config as $key => $val) {
- $ret[$root . $key] = $val;
- }
- config($ret);
- }
-
- protected function setValues(\stdClass|array $data)
- {
- foreach ($data as $key => $val) {
- $this->setValue($key, $val);
- }
- }
-
- protected function setValue(string $key, $value)
- {
- $val = $value;
- $camel = Str::camel($key);
- $snake = Str::snake($key);
-
- $property = "";
-
- if (property_exists($this, $camel)) {
- $property = $camel;
- } else if (property_exists($this, $snake)) {
- $property = $snake;
- } else {
- return;
- }
-
- if (data_get($this->casts, $property) === 'datetime') {
- $this->$property = DateUtil::parse($val);
- } else {
- $this->$property = $val;
- }
- }
-
- public function makeModel(): Email
- {
- if ($this->__email === "") {
- throw new AppCommonException("Email宛先不明");
- }
-
- $model = new Email();
- $model->subject = $this->getSubject();
- $model->content = $this->render();
- $model->type = get_class($this);
- $model->email = $this->__email;
-
- $model->contract_id = $this->__contractId;
- $model->user_id = $this->__userId;
- $model->receipt_issuing_order_id = $this->__receiptIssuingOrderId;
-
- return $model;
- }
-
- abstract public function getTemplateName(): string;
-
- abstract public function getSubject(): string;
-
- abstract public function getParams(): array;
-
-
- /**
- * 画面のURLを生成する
- *
- * @param array|string $path
- * @return string
- */
- protected function getAppUrl(array|string $path): string
- {
- $elements = [config("app.url")];
- if (is_array($path)) {
- $elements = array_merge($elements, $path);
- } else {
- $elements[] = $path;
- }
-
- return implode(
- "/",
- $elements,
- );
- }
- }
|