|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\PostOffice\Mailables; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
|
6
|
|
|
use Illuminate\Queue\SerializesModels; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Mailable extends \Illuminate\Mail\Mailable |
|
9
|
|
|
{ |
|
10
|
|
|
// todo: refactor to allow interfaces to set properties without passing to constructor |
|
11
|
|
|
// todo: refactor to use basic mailable building instead of views (new class?) |
|
12
|
|
|
use Queueable, SerializesModels; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string View blade to use for mailable content |
|
16
|
|
|
*/ |
|
17
|
|
|
public $view; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string First line of the email body |
|
21
|
|
|
*/ |
|
22
|
|
|
public $greeting; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string Recipient email address |
|
26
|
|
|
*/ |
|
27
|
|
|
public $email; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string Subject of the email |
|
31
|
|
|
*/ |
|
32
|
|
|
public $title; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array|string Array of strings to be displayed as paragraphs |
|
36
|
|
|
*/ |
|
37
|
|
|
public $messages; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array Action button url and text |
|
41
|
|
|
*/ |
|
42
|
|
|
public $call_to_action; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Mailable constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string|null $greeting |
|
48
|
|
|
* @param string|null $email |
|
49
|
|
|
* @param string|null $title |
|
50
|
|
|
* @param null $messages |
|
|
|
|
|
|
51
|
|
|
* @param array|null $call_to_action |
|
52
|
|
|
* @param string|null $view |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(string $greeting = null, |
|
55
|
|
|
string $email = null, |
|
56
|
|
|
string $title = null, |
|
57
|
|
|
$messages = null, |
|
58
|
|
|
array $call_to_action = null, |
|
59
|
|
|
string $view = null) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->greeting = $greeting ?? $this->greeting; |
|
62
|
|
|
$this->email = $email ?? $this->email; |
|
63
|
|
|
$this->title = $title ?? $this->title; |
|
64
|
|
|
$this->messages = $messages ?? $this->messages; |
|
65
|
|
|
$this->call_to_action = $call_to_action ?? $this->call_to_action; |
|
66
|
|
|
|
|
67
|
|
|
$this->view = $this->setView($view); |
|
68
|
|
|
$this->onQueue(config('post-office.queue')); |
|
69
|
|
|
$this->onConnection(config('post-office.driver')); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Build the message. |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function build(): self |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->subject($this->title) |
|
80
|
|
|
->view($this->view, [ |
|
81
|
|
|
'greeting' => $this->greeting, |
|
82
|
|
|
'email' => $this->email, |
|
83
|
|
|
'title' => $this->title, |
|
84
|
|
|
'messages' => $this->messages, |
|
85
|
|
|
'call_to_action' => $this->call_to_action, |
|
86
|
|
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Set the Mailable's $view property. |
|
91
|
|
|
* |
|
92
|
|
|
* 1. $view parameter passed to the constructor |
|
93
|
|
|
* 2. $view property declared in child `AbstractMailable` extension |
|
94
|
|
|
* 3. 'post-office.mailable.view' config value (defaults to post-office::email) |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|null $view |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
|
|
private function setView(string $view = null): string |
|
100
|
|
|
{ |
|
101
|
|
|
return $view ?? $this->view ?? config('post-office.mailables.view'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|