|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\MailableTest; |
|
4
|
|
|
|
|
5
|
|
|
use Mail; |
|
6
|
|
|
use Validator; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
9
|
|
|
|
|
10
|
|
|
class SendTestMail extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
protected $signature = 'mail:send-test {mailableClass} {recipient} {--values=}'; |
|
13
|
|
|
|
|
14
|
|
|
protected $description = 'Send a test email'; |
|
15
|
|
|
|
|
16
|
|
|
public function handle() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->guardAgainstInvalidArguments(); |
|
19
|
|
|
|
|
20
|
|
|
$mailableClass = $this->getMailableClass(); |
|
21
|
|
|
|
|
22
|
|
|
$mailable = app(MailableFactory::class)->getInstance( |
|
23
|
|
|
$mailableClass, |
|
24
|
|
|
$this->argument('recipient'), |
|
25
|
|
|
$this->getValues() |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
Mail::send($mailable); |
|
29
|
|
|
|
|
30
|
|
|
$this->comment("Mailable `{$mailableClass}` sent to {$this->argument('recipient')}!"); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function guardAgainstInvalidArguments() |
|
34
|
|
|
{ |
|
35
|
|
|
$validator = Validator::make( |
|
36
|
|
|
['email' => $this->argument('recipient')], |
|
37
|
|
|
['email' => 'email'] |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
if (! $validator->passes()) { |
|
41
|
|
|
throw new InvalidArgumentException("`{$this->argument('recipient')}` is not a valid e-mail address"); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function getValues(): array |
|
46
|
|
|
{ |
|
47
|
|
|
if (! $this->option('values')) { |
|
48
|
|
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$values = explode(',', $this->option('values')); |
|
52
|
|
|
|
|
53
|
|
|
return collect($values) |
|
54
|
|
|
->mapWithKeys(function (string $value) { |
|
55
|
|
|
$values = explode(':', $value); |
|
56
|
|
|
|
|
57
|
|
|
if (count($values) != 2) { |
|
58
|
|
|
throw new InvalidArgumentException("The given value for the option 'values' `{$this->option('values')}` is not valid."); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return [$values[0] => $values[1]]; |
|
62
|
|
|
}) |
|
63
|
|
|
->toArray(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function getMailableClass() |
|
67
|
|
|
{ |
|
68
|
|
|
$mailableClass = $this->argument('mailableClass'); |
|
69
|
|
|
|
|
70
|
|
|
if (! class_exists($mailableClass)) { |
|
71
|
|
|
$mailableClass = sprintf('%s\\%s', config('mailable-test.base_namespace'), $mailableClass); |
|
72
|
|
|
|
|
73
|
|
|
if (! class_exists($mailableClass)) { |
|
74
|
|
|
throw new InvalidArgumentException("Mailable `{$mailableClass}` does not exist."); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $mailableClass; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|