Browse Source

添付ファイル存在しない場合に、メールジョブを終了させるように終了

develop
sosuke.iwabuchi 2 years ago
parent
commit
a514bfe98d
3 changed files with 40 additions and 1 deletions
  1. +11
    -0
      app/Email/BaseEmailer.php
  2. +21
    -0
      app/Exceptions/TempFileNotExistsException.php
  3. +8
    -1
      app/Jobs/Email/SimpleEmail.php

+ 11
- 0
app/Email/BaseEmailer.php View File

@@ -3,6 +3,7 @@
namespace App\Email; namespace App\Email;


use App\Exceptions\AppCommonException; use App\Exceptions\AppCommonException;
use App\Exceptions\TempFileNotExistsException;
use App\Models\Email; use App\Models\Email;
use App\Models\EmailAttachment; use App\Models\EmailAttachment;
use App\Models\User; use App\Models\User;
@@ -82,7 +83,17 @@ abstract class BaseEmailer extends Mailable
if ($this->__attachments !== null) { if ($this->__attachments !== null) {
$count = $this->__attachments->count(); $count = $this->__attachments->count();
foreach ($this->__attachments as $attachment) { foreach ($this->__attachments as $attachment) {



$filepath = $attachment->filepath; $filepath = $attachment->filepath;


if (!file_exists($filepath)) {
$e = new TempFileNotExistsException();
throw $e->setFilepath($filepath);
}

$as = $attachment->send_filename; $as = $attachment->send_filename;
$mime = $attachment->mime; $mime = $attachment->mime;
$this->attach($filepath, [ $this->attach($filepath, [


+ 21
- 0
app/Exceptions/TempFileNotExistsException.php View File

@@ -0,0 +1,21 @@
<?php

namespace App\Exceptions;

use Exception;

class TempFileNotExistsException extends Exception
{
private $filepath = "";

public function setFilepath(string $filepath): static
{
$this->filepath = $filepath;
return $this;
}

public function getFilepath(): string
{
return $this->filepath;
}
}

+ 8
- 1
app/Jobs/Email/SimpleEmail.php View File

@@ -4,8 +4,10 @@ namespace App\Jobs\Email;


use App\Codes\QueueName; use App\Codes\QueueName;
use App\Email\Sender; use App\Email\Sender;
use App\Exceptions\TempFileNotExistsException;
use App\Jobs\BaseJob; use App\Jobs\BaseJob;
use App\Models\Email; use App\Models\Email;
use Illuminate\Support\Facades\Log;


class SimpleEmail extends BaseJob class SimpleEmail extends BaseJob
{ {
@@ -29,6 +31,11 @@ class SimpleEmail extends BaseJob
*/ */
public function handle() public function handle()
{ {
Sender::send($this->emailId);
try {
Sender::send($this->emailId);
} catch (TempFileNotExistsException $e) {
// 一時ファイルが存在しないため、処理終了
Log::warning(sprintf("ファイル存在しないため、メール送信処理スキップ :%s", $e->getFilepath()));
}
} }
} }

Loading…
Cancel
Save