PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / libraries / adapter / mail / mail.php

mail.php in VikBooking Hotel Booking Engine & PMS trunk, at libraries/adapter/mail/mail.php

470 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.mail
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * Email Class.
16 * Provides a common interface to send emails.
17 *
18 * @since 10.0
19 */
20 class JMail
21 {
22 /**
23 * A list of mail instances.
24 *
25 * @var array
26 */
27 protected static $instances = array();
28
29 /**
30 * The e-mail from address/name.
31 *
32 * @var string
33 */
34 protected $from = null;
35
36 /**
37 * The e-mail subject.
38 *
39 * @var string
40 */
41 protected $subject = null;
42
43 /**
44 * The e-mail body message.
45 *
46 * @var string
47 */
48 protected $body = null;
49
50 /**
51 * The e-mail recipients list.
52 *
53 * @var array
54 */
55 protected $recipients = array();
56
57 /**
58 * The e-mail carbon copy list.
59 *
60 * @var array
61 */
62 protected $cc = array();
63
64 /**
65 * The e-mail blind carbon copy list.
66 *
67 * @var array
68 */
69 protected $bcc = array();
70
71 /**
72 * The e-mail reply-to list.
73 *
74 * @var array
75 */
76 protected $reply_to = array();
77
78 /**
79 * A list of attachments file.
80 *
81 * @var array
82 */
83 protected $attachments = array();
84
85 /**
86 * Flag to define if the content should be Plain Text or HTML.
87 *
88 * @var boolean
89 */
90 protected $isHtml = false;
91
92 /**
93 * Class constructor.
94 */
95 public function __construct()
96 {
97
98 }
99
100 /**
101 * Magic method set to define certain object properties.
102 *
103 * @param string $name The property name.
104 * @param mixed $value The value for the property.
105 *
106 * @return void
107 *
108 * @since 10.1.6
109 */
110 public function __set($name, $value)
111 {
112 // hook to the PHPMailer init action
113 add_action('phpmailer_init', function(&$phpmailer) use ($name, $value)
114 {
115 /**
116 * Make sure the provided argument is actually an object.
117 *
118 * @since 10.1.61 Ignore the PHPMailer class check, since it might be prefixed by a namespace.
119 * Receiving an object is enough for us.
120 */
121 if (!is_object($phpmailer))
122 {
123 // safety check
124 return;
125 }
126
127 /**
128 * Check whether the specified property is supported by PHPMailer.
129 *
130 * @since 10.1.47
131 */
132 if (property_exists($phpmailer, $name))
133 {
134 $phpmailer->{$name} = $value;
135 }
136 });
137 }
138
139 /**
140 * Returns the global email object, only creating it if it doesn't already exist.
141 *
142 * @param string $id The id string for the Mail instance.
143 *
144 * @return JMail The global Mail object.
145 */
146 public static function getInstance($id = 'wordpress')
147 {
148 if (empty(static::$instances[$id]))
149 {
150 static::$instances[$id] = new static();
151 }
152
153 return static::$instances[$id];
154 }
155
156 /**
157 * Send the mail.
158 *
159 * @return boolean True on success, otherwise false.
160 */
161 public function Send()
162 {
163 $to = implode(',', $this->recipients);
164
165 $headers = array();
166
167 if (!empty($this->from))
168 {
169 $headers[] = "From: {$this->from}";
170 }
171
172 if (!empty($this->reply_to))
173 {
174 foreach ($this->reply_to as $replyto)
175 {
176 $headers[] = "Reply-to: {$replyto}";
177 }
178 }
179
180 if (!empty($this->cc))
181 {
182 foreach ($this->cc as $cc)
183 {
184 $headers[] = "Cc: {$cc}";
185 }
186 }
187
188 if (!empty($this->bcc))
189 {
190 foreach ($this->bcc as $bcc)
191 {
192 $headers[] = "Bcc: {$bcc}";
193 }
194 }
195
196 if ($this->isHtml)
197 {
198 $headers[] = "Content-Type: text/html; charset=UTF-8";
199 }
200
201 return wp_mail($to, $this->subject, $this->body, $headers, $this->attachments);
202 }
203
204 /**
205 * Set the From and FromName properties.
206 *
207 * @param string $address The sender email address.
208 * @param string $name The sender name.
209 * @param boolean $auto Whether to also set the Sender address (@unused).
210 *
211 * @return boolean
212 */
213 public function setFrom($address, $name = '', $auto = true)
214 {
215 $this->from = $address;
216
217 if ($name)
218 {
219 $this->from = "$name <{$this->from}>";
220 }
221
222 return true;
223 }
224
225 /**
226 * Sets the email sender.
227 *
228 * @param mixed $from The e-mail address or an array containing sender address and name.
229 *
230 * @return self This object to support chaining.
231 *
232 * @uses setFrom()
233 */
234 public function setSender($from)
235 {
236 if (!is_array($from))
237 {
238 $from = array($from);
239 }
240
241 call_user_func_array(array($this, 'setFrom'), $from);
242
243 return $this;
244 }
245
246 /**
247 * Sets the email subject.
248 *
249 * @param string $subject Subject of the email.
250 *
251 * @return self This object to support chaining.
252 */
253 public function setSubject($subject)
254 {
255 $this->subject = $subject;
256
257 return $this;
258 }
259
260 /**
261 * Sets the email body.
262 *
263 * @param string $content Body of the email.
264 *
265 * @return self This object to support chaining.
266 */
267 public function setBody($content)
268 {
269 $this->body = $content;
270
271 return $this;
272 }
273
274 /**
275 * Adds elements to the email.
276 *
277 * @param mixed $recipient Either a string or array of strings [email address(es)].
278 * @param mixed $name Either a string or array of strings [name(s)].
279 * @param string $property The property name to which attach the element.
280 *
281 * @return self This object to support chaining.
282 */
283 protected function add($recipient, $name = '', $prop = 'recipients')
284 {
285 if (is_array($recipient))
286 {
287 for ($i = 0; $i < count($recipient); $i++)
288 {
289 $this->add($recipient[$i], (is_array($name) ? $name[$i] : $name), $prop);
290 }
291 }
292 else
293 {
294 if ($name)
295 {
296 $recipient = "{$name} <{$recipient}>";
297 }
298
299 $this->{$prop}[] = $recipient;
300 }
301
302 return $this;
303 }
304
305 /**
306 * Adds recipients to the email.
307 *
308 * @param mixed $recipient Either a string or array of strings [email address(es)].
309 * @param mixed $name Either a string or array of strings [name(s)].
310 *
311 * @return self This object to support chaining.
312 *
313 * @uses add()
314 */
315 public function addRecipient($recipient, $name = '')
316 {
317 $this->add($recipient, $name, 'recipients');
318 }
319
320 /**
321 * Adds carbon copy recipients to the email
322 *
323 * @param mixed $cc Either a string or array of strings [email address(es)].
324 * @param mixed $name Either a string or array of strings [name(s)].
325 *
326 * @return self This object to support chaining.
327 *
328 * @uses add()
329 */
330 public function addCc($cc, $name = '')
331 {
332 return $this->add($cc, $name, 'cc');
333 }
334
335 /**
336 * Adds blind carbon copy recipients to the email
337 *
338 * @param mixed $bcc Either a string or array of strings [email address(es)]
339 * @param mixed $name Either a string or array of strings [name(s)]
340 *
341 * @return self This object to support chaining.
342 *
343 * @uses add()
344 */
345 public function addBcc($bcc, $name = '')
346 {
347 return $this->add($bcc, $name, 'bcc');
348 }
349
350 /**
351 * Adds file attachment to the email
352 *
353 * @param mixed $path Either a string or array of strings [filenames].
354 *
355 * @return self This object to support chaining.
356 */
357 public function addAttachment($path)
358 {
359 if (!is_array($path))
360 {
361 $path = array($path);
362 }
363
364 foreach ($path as $file)
365 {
366 $this->attachments[] = $file;
367 }
368
369 return $this;
370 }
371
372 /**
373 * Adds Reply to email address(es) to the email.
374 *
375 * @param mixed $replyto Either a string or array of strings [email address(es)].
376 * @param mixed $name Either a string or array of strings [name(s)].
377 *
378 * @return self This object to support chaining.
379 *
380 * @uses add()
381 */
382 public function addReplyTo($replyto, $name = '')
383 {
384 return $this->add($replyto, $name, 'reply_to');
385 }
386
387 /**
388 * Sets message type to HTML.
389 *
390 * @param boolean $ishtml Boolean true or false.
391 *
392 * @return self This object to support chaining.
393 *
394 */
395 public function isHtml($ishtml = true)
396 {
397 $this->isHtml = $ishtml;
398
399 return $this;
400 }
401
402 /**
403 * Use SMTP for sending the email
404 *
405 * @param string $auth SMTP Authentication [optional]
406 * @param string $host SMTP Host [optional]
407 * @param string $user SMTP Username [optional]
408 * @param string $pass SMTP Password [optional]
409 * @param string $secure Use secure methods
410 * @param integer $port The SMTP port
411 *
412 * @return boolean True on success
413 *
414 * @since 10.1.6
415 */
416 public function useSmtp($auth = null, $host = null, $user = null, $pass = null, $secure = null, $port = 25)
417 {
418 // hook to the PHPMailer init action
419 add_action('phpmailer_init', function(&$phpmailer) use ($auth, $host, $user, $pass, $secure, $port)
420 {
421 /**
422 * Make sure the provided argument is actually an object.
423 *
424 * @since 10.1.62 Ignore the PHPMailer class check, since it might be prefixed by a namespace.
425 * Receiving an object is enough for us.
426 */
427 if (!is_object($phpmailer))
428 {
429 // safety check
430 return;
431 }
432
433 if ($auth === null || $host === null || $user === null || $pass === null)
434 {
435 // do not interfere if the plugin parameters are null
436 return;
437 }
438
439 // Tell PHPMailer to use SMTP
440 $phpmailer->isSMTP();
441
442 // Set the hostname of the mail server
443 $phpmailer->Host = $host;
444
445 // Set the SMTP port number
446 $phpmailer->Port = (int)$port;
447
448 // Whether to use SMTP authentication
449 $phpmailer->SMTPAuth = true;
450
451 // Username to use for SMTP authentication
452 $phpmailer->Username = $user;
453
454 //Password to use for SMTP authentication
455 $phpmailer->Password = $pass;
456
457 if ($secure == 'ssl' || $secure == 'tls')
458 {
459 $phpmailer->SMTPSecure = $secure;
460 }
461 else
462 {
463 $phpmailer->SMTPAutoTLS = false;
464 }
465 });
466
467 return true;
468 }
469 }
470