1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
use PHPMailer\PHPMailer\PHPMailer; |
6
|
|
|
use PHPMailer\PHPMailer\SMTP; |
7
|
|
|
use PHPMailer\PHPMailer\Exception; |
8
|
|
|
|
9
|
|
|
class Email |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Application Object |
13
|
|
|
* |
14
|
|
|
* @var \System\Application |
15
|
|
|
*/ |
16
|
|
|
private $app; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* PHPMailer Object |
20
|
|
|
* |
21
|
|
|
* @var PHPMailer |
22
|
|
|
*/ |
23
|
|
|
private $mail; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor |
27
|
|
|
* |
28
|
|
|
* @param \System\Application $app |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Application $app) |
31
|
|
|
{ |
32
|
|
|
$this->app = $app; |
33
|
|
|
|
34
|
|
|
$this->mail = new PHPMailer(true); |
35
|
|
|
|
36
|
|
|
$this->setUp(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set up the configrations |
41
|
|
|
* |
42
|
|
|
* @property object $error |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
private function setUp() |
46
|
|
|
{ |
47
|
|
|
$this->mail->SMTPDebug = $this->app->error->allowDisplayingError() ? SMTP::DEBUG_SERVER : 0; |
|
|
|
|
48
|
|
|
$this->mail->isSMTP(); |
49
|
|
|
$this->mail->Host = $_ENV['EMAIL_HOST']; |
50
|
|
|
$this->mail->SMTPAuth = $_ENV['EMAIL_SMTPAUTH']; |
51
|
|
|
$this->mail->Username = $_ENV['EMAIL_USERNAME']; |
52
|
|
|
$this->mail->Password = $_ENV['EMAIL_PASSWORD']; |
53
|
|
|
$this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; |
54
|
|
|
$this->mail->Port = $_ENV['EMAIL_PORT']; |
55
|
|
|
$this->mail->setFrom($_ENV['EMAIL_ADMIN'], $_ENV['EMAIL_NAME']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* To add addresses or attachments easily to the object |
60
|
|
|
* |
61
|
|
|
* @param string|array $input |
62
|
|
|
* @param string $method |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
private function add($input, $method) |
66
|
|
|
{ |
67
|
|
|
if (!is_array($input)) { |
68
|
|
|
$input = [$input]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($input as $key => $value) { |
72
|
|
|
if (is_numeric($key)) { |
73
|
|
|
$this->mail->$method($value); |
74
|
|
|
} else { |
75
|
|
|
$this->mail->$method($value, $key); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add addresses |
82
|
|
|
* |
83
|
|
|
* @param string|array $addresses |
84
|
|
|
* @return object $this |
85
|
|
|
*/ |
86
|
|
|
public function address($addresses) |
87
|
|
|
{ |
88
|
|
|
$this->add($addresses, 'addAddress'); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add addresses |
95
|
|
|
* |
96
|
|
|
* @param array $replayTo |
97
|
|
|
* @return object $this |
98
|
|
|
*/ |
99
|
|
|
public function replayTo(array $replayTo = []) |
100
|
|
|
{ |
101
|
|
|
$this->mail->addReplyTo(array_values($replayTo)[0], array_keys($replayTo)[0]); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add attachments to email |
108
|
|
|
* |
109
|
|
|
* @param string|array $attachments |
110
|
|
|
* @return object $this |
111
|
|
|
*/ |
112
|
|
|
public function attachments($attachments) |
113
|
|
|
{ |
114
|
|
|
$this->add($attachments, 'addAttachment'); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Add bcc to email |
121
|
|
|
* |
122
|
|
|
* @param string|array $bcc |
123
|
|
|
* @return object $this |
124
|
|
|
*/ |
125
|
|
|
public function bcc($bcc) |
126
|
|
|
{ |
127
|
|
|
$this->add($bcc, 'addBCC'); |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Add cc to email |
134
|
|
|
* |
135
|
|
|
* @param string|array $cc |
136
|
|
|
* @return object $this |
137
|
|
|
*/ |
138
|
|
|
public function cc($cc) |
139
|
|
|
{ |
140
|
|
|
$this->add($cc, 'addCC'); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add content to email |
147
|
|
|
* |
148
|
|
|
* @param string $html |
149
|
|
|
* @param string $subject |
150
|
|
|
* @param string $body |
151
|
|
|
* @param string $altBody |
152
|
|
|
* @return object $this |
153
|
|
|
*/ |
154
|
|
|
public function content(string $html, string $subject, string $body, string $altBody) |
155
|
|
|
{ |
156
|
|
|
$this->mail->isHTML($html); |
|
|
|
|
157
|
|
|
$this->mail->Subject = $subject; |
158
|
|
|
$this->mail->Body = $body; |
159
|
|
|
$this->mail->AltBody = $altBody; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Send email |
166
|
|
|
* |
167
|
|
|
* @return void |
168
|
|
|
*/ |
169
|
|
|
public function send() |
170
|
|
|
{ |
171
|
|
|
try { |
172
|
|
|
$this->mail->send(); |
173
|
|
|
} catch (Exception $e) { |
174
|
|
|
echo 'Message could not be sent. Mailer Error'; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.