1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Da\Mailer\Model; |
4
|
|
|
|
5
|
|
|
use Da\Mailer\Builder\MailJobBuilder; |
6
|
|
|
use Da\Mailer\Builder\QueueBuilder; |
7
|
|
|
use Da\Mailer\Mail\Dto\File; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
class MailMessage extends AbstractMailObject implements JsonSerializable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array the transport options. They are different according to the transport type chosen. For example: |
14
|
|
|
* |
15
|
|
|
* SmtpTransport may contain: |
16
|
|
|
* |
17
|
|
|
* ``` |
18
|
|
|
* [ |
19
|
|
|
* 'username' => 'Obiwoan', |
20
|
|
|
* 'password' => 'Kenobi', |
21
|
|
|
* 'encryption' => 'ssl', |
22
|
|
|
* 'authMode' => 'Plain' |
23
|
|
|
* ] |
24
|
|
|
* ``` |
25
|
|
|
* |
26
|
|
|
* MailTransport may contain: |
27
|
|
|
* ``` |
28
|
|
|
* ['f%s'] |
29
|
|
|
* ``` |
30
|
|
|
* |
31
|
|
|
* SendMailTransport may contain: |
32
|
|
|
* ``` |
33
|
|
|
* ['/usr/sbin/sendmail -bs'] |
34
|
|
|
* ``` |
35
|
|
|
*/ |
36
|
|
|
public $transportOptions = []; |
37
|
|
|
/** |
38
|
|
|
* @var string the transport type. It could be TransportInterface::TYPE_SMTP, TransportInterface::TYPE_MAIL, o |
39
|
|
|
* TransportInterface::TYPE_SEND_MAIL. |
40
|
|
|
*/ |
41
|
|
|
public $transportType; |
42
|
|
|
/** |
43
|
|
|
* @var string the mail server address. |
44
|
|
|
*/ |
45
|
|
|
public $host; |
46
|
|
|
/** |
47
|
|
|
* @var int the mail server port. |
48
|
|
|
*/ |
49
|
|
|
public $port; |
50
|
|
|
/** |
51
|
|
|
* @var array|string the from address/es |
52
|
|
|
*/ |
53
|
|
|
public $from; |
54
|
|
|
/** |
55
|
|
|
* @var array|string the to address/es |
56
|
|
|
*/ |
57
|
|
|
public $to; |
58
|
|
|
/** |
59
|
|
|
* @var array|string the cc address/es |
60
|
|
|
*/ |
61
|
|
|
public $cc; |
62
|
|
|
/** |
63
|
|
|
* @var array|string the bcc address/es |
64
|
|
|
*/ |
65
|
|
|
public $bcc; |
66
|
|
|
/** |
67
|
|
|
* @var string the subject of the mail message |
68
|
|
|
*/ |
69
|
|
|
public $subject; |
70
|
|
|
/** |
71
|
|
|
* @var string the body html of the mail message |
72
|
|
|
*/ |
73
|
|
|
public $bodyHtml; |
74
|
|
|
/** |
75
|
|
|
* @var string the body |
76
|
|
|
*/ |
77
|
|
|
public $bodyText; |
78
|
|
|
/** |
79
|
|
|
* @var array|null the file paths to attach to the Swift_Message instance if `asSwiftMessage()` is called |
80
|
|
|
*/ |
81
|
|
|
protected $attachments; |
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
26 |
|
public function __construct(array $config = []) |
86
|
|
|
{ |
87
|
26 |
|
parent::__construct($config); |
88
|
26 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $config |
92
|
|
|
* @return MailMessage |
93
|
|
|
*/ |
94
|
|
|
public static function make(array $config) |
95
|
3 |
|
{ |
96
|
|
|
return new self($config); |
97
|
3 |
|
} |
98
|
|
|
|
99
|
3 |
|
/** |
100
|
3 |
|
* Specify data which should be serialized to JSON. |
101
|
3 |
|
* |
102
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
103
|
3 |
|
* |
104
|
3 |
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
105
|
3 |
|
* which is a value of any type other than a resource. |
106
|
3 |
|
* |
107
|
3 |
|
* @since 5.4.0 |
108
|
3 |
|
*/ |
109
|
3 |
|
public function jsonSerialize() |
110
|
3 |
|
{ |
111
|
3 |
|
return get_object_vars($this); |
112
|
3 |
|
} |
113
|
|
|
|
114
|
3 |
|
/** |
115
|
|
|
* @return void |
116
|
3 |
|
* @throws \Da\Mailer\Exception\UndefinedMessageBrokerException |
117
|
3 |
|
*/ |
118
|
3 |
|
public function enqueue() |
119
|
3 |
|
{ |
120
|
3 |
|
$job = MailJobBuilder::make(['message' => json_encode($this)]); |
121
|
3 |
|
QueueBuilder::make()->enqueue($job); |
122
|
3 |
|
} |
123
|
3 |
|
|
124
|
3 |
|
/** |
125
|
3 |
|
* @param string $path |
126
|
3 |
|
* @param string|null $name |
127
|
3 |
|
* @return void |
128
|
|
|
*/ |
129
|
3 |
|
public function addAttachment(string $path, ?string $name = null): void |
130
|
|
|
{ |
131
|
|
|
if (is_null($this->attachments)) { |
132
|
|
|
$this->attachments = [File::make($path, $name)]; |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$this->attachments[] = File::make($path, $name); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
21 |
|
public function getAttachments(): array |
143
|
|
|
{ |
144
|
21 |
|
return $this->attachments ?? []; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|