This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Message.php
193 lines (155 loc) · 4.42 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
namespace MailThief;
use Exception;
use Illuminate\Support\Arr;
use MailThief\Support\MailThiefCollection;
class Message
{
private $view;
public $data;
public $subject;
public $from;
public $sender;
public $to;
public $cc;
public $bcc;
public $reply_to;
public $priority;
public $attachments;
public $headers;
public $delay = 0;
/**
* Methods that are available in Laravel but not provided by MailThief
* @var array
*/
public $valid_methods = [
'addPart',
'getHeaders',
'setReadReceiptTo',
'setCharset',
'setMaxLineLength',
'attachSigner',
];
public function __construct($view, $data)
{
$this->view = $view;
$this->data = $data;
$this->to = new MailThiefCollection();
$this->cc = new MailThiefCollection();
$this->bcc = new MailThiefCollection();
$this->reply_to = new MailThiefCollection();
$this->attachments = new MailThiefCollection();
$this->headers = new MailThiefCollection();
}
public function __call($name, $arguments)
{
if (in_array(strtolower($name), array_map('strtolower', $this->valid_methods))) {
return $this;
}
throw new Exception("Invalid method ($name) called");
}
public static function fromView($view, $data)
{
return new self($view, $data);
}
public static function fromRaw($body)
{
return new self(['raw' => $body], []);
}
public function getBody($part = 'html')
{
return Arr::get($this->view, $part, reset($this->view));
}
public function subject($subject)
{
$this->subject = $subject;
return $this;
}
public function to($address, $name = null, $override = false)
{
if ($override) {
$this->to = new MailThiefCollection();
}
if (! is_array($address)) {
$address = $name ? [$address => $name] : [$address];
}
$this->to = $this->to->merge($address);
return $this;
}
public function cc($address, $name = null)
{
if (! is_array($address)) {
$address = $name ? [$address => $name] : [$address];
}
$this->cc = $this->cc->merge($address);
return $this;
}
public function bcc($address, $name = null)
{
if (! is_array($address)) {
$address = $name ? [$address => $name] : [$address];
}
$this->bcc = $this->bcc->merge($address);
return $this;
}
public function replyTo($address, $name = null)
{
if (! is_array($address)) {
$address = $name ? [$address => $name] : [$address];
}
$this->reply_to = $this->reply_to->merge($address);
return $this;
}
public function from($address, $name = null)
{
if (! is_array($address)) {
$address = $name ? [$address => $name] : [$address];
}
$this->from = new MailThiefCollection($address);
return $this;
}
public function sender($address, $name = null)
{
if (! is_array($address)) {
$address = $name ? [$address => $name] : [$address];
}
$this->sender = new MailThiefCollection($address);
return $this;
}
public function priority($level)
{
$this->priority = $level;
return $this;
}
public function hasRecipient($email)
{
return $this->recipients()->has($email) || $this->recipients()->contains($email);
}
public function recipients()
{
return $this->to->merge($this->cc)->merge($this->bcc);
}
public function contains($text, $part = 'html')
{
return str_contains($this->getBody($part), $text);
}
public function attach($pathToFile, array $options = [])
{
$this->attachments[] = ['path' => $pathToFile, 'options' => $options];
return $this;
}
public function attachData($data, $name, array $options = [])
{
$this->attachments[] = ['data' => $data, 'name' => $name, 'options' => $options];
return $this;
}
public function addTextHeader($name, $value = null)
{
$this->headers[] = ['name' => $name, 'value' => $value];
return $this;
}
public function getSwiftMessage()
{
throw new Exception("Cannot get Swift message from MailThief message.");
}
}