PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.9
VikBooking Hotel Booking Engine & PMS v1.6.9
1.8.16 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 All 37 releases
vikbooking / libraries / adapter / mail / mail.php

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

458 lines 8.9 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 if (!$phpmailer instanceof PHPMailer)
116 {
117 // safety check
118 return $phpmailer;
119 }
120
121 /**
122 * Check whether the specified property is supported by PHPMailer.
123 *
124 * @since 10.1.47
125 */
126 if (property_exists($phpmailer, $name))
127 {
128 $phpmailer->{$name} = $value;
129 }
130 });
131 }
132
133 /**
134 * Returns the global email object, only creating it if it doesn't already exist.
135 *
136 * @param string $id The id string for the Mail instance.
137 *
138 * @return JMail The global Mail object.
139 */
140 public static function getInstance($id = 'wordpress')
141 {
142 if (empty(static::$instances[$id]))
143 {
144 static::$instances[$id] = new static();
145 }
146
147 return static::$instances[$id];
148 }
149
150 /**
151 * Send the mail.
152 *
153 * @return boolean True on success, otherwise false.
154 */
155 public function Send()
156 {
157 $to = implode(',', $this->recipients);
158
159 $headers = array();
160
161 if (!empty($this->from))
162 {
163 $headers[] = "From: {$this->from}";
164 }
165
166 if (!empty($this->reply_to))
167 {
168 foreach ($this->reply_to as $replyto)
169 {
170 $headers[] = "Reply-to: {$replyto}";
171 }
172 }
173
174 if (!empty($this->cc))
175 {
176 foreach ($this->cc as $cc)
177 {
178 $headers[] = "Cc: {$cc}";
179 }
180 }
181
182 if (!empty($this->bcc))
183 {
184 foreach ($this->bcc as $bcc)
185 {
186 $headers[] = "Bcc: {$bcc}";
187 }
188 }
189
190 if ($this->isHtml)
191 {
192 $headers[] = "Content-Type: text/html; charset=UTF-8";
193 }
194
195 return wp_mail($to, $this->subject, $this->body, $headers, $this->attachments);
196 }
197
198 /**
199 * Set the From and FromName properties.
200 *
201 * @param string $address The sender email address.
202 * @param string $name The sender name.
203 * @param boolean $auto Whether to also set the Sender address (@unused).
204 *
205 * @return boolean
206 */
207 public function setFrom($address, $name = '', $auto = true)
208 {
209 $this->from = $address;
210
211 if ($name)
212 {
213 $this->from = "$name <{$this->from}>";
214 }
215
216 return true;
217 }
218
219 /**
220 * Sets the email sender.
221 *
222 * @param mixed $from The e-mail address or an array containing sender address and name.
223 *
224 * @return self This object to support chaining.
225 *
226 * @uses setFrom()
227 */
228 public function setSender($from)
229 {
230 if (!is_array($from))
231 {
232 $from = array($from);
233 }
234
235 call_user_func_array(array($this, 'setFrom'), $from);
236
237 return $this;
238 }
239
240 /**
241 * Sets the email subject.
242 *
243 * @param string $subject Subject of the email.
244 *
245 * @return self This object to support chaining.
246 */
247 public function setSubject($subject)
248 {
249 $this->subject = $subject;
250
251 return $this;
252 }
253
254 /**
255 * Sets the email body.
256 *
257 * @param string $content Body of the email.
258 *
259 * @return self This object to support chaining.
260 */
261 public function setBody($content)
262 {
263 $this->body = $content;
264
265 return $this;
266 }
267
268 /**
269 * Adds elements to the email.
270 *
271 * @param mixed $recipient Either a string or array of strings [email address(es)].
272 * @param mixed $name Either a string or array of strings [name(s)].
273 * @param string $property The property name to which attach the element.
274 *
275 * @return self This object to support chaining.
276 */
277 protected function add($recipient, $name = '', $prop = 'recipients')
278 {
279 if (is_array($recipient))
280 {
281 for ($i = 0; $i < count($recipient); $i++)
282 {
283 $this->add($recipient[$i], (is_array($name) ? $name[$i] : $name), $prop);
284 }
285 }
286 else
287 {
288 if ($name)
289 {
290 $recipient = "{$name} <{$recipient}>";
291 }
292
293 $this->{$prop}[] = $recipient;
294 }
295
296 return $this;
297 }
298
299 /**
300 * Adds recipients to the email.
301 *
302 * @param mixed $recipient Either a string or array of strings [email address(es)].
303 * @param mixed $name Either a string or array of strings [name(s)].
304 *
305 * @return self This object to support chaining.
306 *
307 * @uses add()
308 */
309 public function addRecipient($recipient, $name = '')
310 {
311 $this->add($recipient, $name, 'recipients');
312 }
313
314 /**
315 * Adds carbon copy recipients to the email
316 *
317 * @param mixed $cc Either a string or array of strings [email address(es)].
318 * @param mixed $name Either a string or array of strings [name(s)].
319 *
320 * @return self This object to support chaining.
321 *
322 * @uses add()
323 */
324 public function addCc($cc, $name = '')
325 {
326 return $this->add($cc, $name, 'cc');
327 }
328
329 /**
330 * Adds blind carbon copy recipients to the email
331 *
332 * @param mixed $bcc Either a string or array of strings [email address(es)]
333 * @param mixed $name Either a string or array of strings [name(s)]
334 *
335 * @return self This object to support chaining.
336 *
337 * @uses add()
338 */
339 public function addBcc($bcc, $name = '')
340 {
341 return $this->add($bcc, $name, 'bcc');
342 }
343
344 /**
345 * Adds file attachment to the email
346 *
347 * @param mixed $path Either a string or array of strings [filenames].
348 *
349 * @return self This object to support chaining.
350 */
351 public function addAttachment($path)
352 {
353 if (!is_array($path))
354 {
355 $path = array($path);
356 }
357
358 foreach ($path as $file)
359 {
360 $this->attachments[] = $file;
361 }
362
363 return $this;
364 }
365
366 /**
367 * Adds Reply to email address(es) to the email.
368 *
369 * @param mixed $replyto Either a string or array of strings [email address(es)].
370 * @param mixed $name Either a string or array of strings [name(s)].
371 *
372 * @return self This object to support chaining.
373 *
374 * @uses add()
375 */
376 public function addReplyTo($replyto, $name = '')
377 {
378 return $this->add($replyto, $name, 'reply_to');
379 }
380
381 /**
382 * Sets message type to HTML.
383 *
384 * @param boolean $ishtml Boolean true or false.
385 *
386 * @return self This object to support chaining.
387 *
388 */
389 public function isHtml($ishtml = true)
390 {
391 $this->isHtml = $ishtml;
392
393 return $this;
394 }
395
396 /**
397 * Use SMTP for sending the email
398 *
399 * @param string $auth SMTP Authentication [optional]
400 * @param string $host SMTP Host [optional]
401 * @param string $user SMTP Username [optional]
402 * @param string $pass SMTP Password [optional]
403 * @param string $secure Use secure methods
404 * @param integer $port The SMTP port
405 *
406 * @return boolean True on success
407 *
408 * @since 10.1.6
409 */
410 public function useSmtp($auth = null, $host = null, $user = null, $pass = null, $secure = null, $port = 25)
411 {
412 // hook to the PHPMailer init action
413 add_action('phpmailer_init', function(&$phpmailer) use ($auth, $host, $user, $pass, $secure, $port)
414 {
415 if (!$phpmailer instanceof PHPMailer)
416 {
417 // safety check
418 return $phpmailer;
419 }
420
421 if ($auth === null || $host === null || $user === null || $pass === null)
422 {
423 // do not interfere if the plugin parameters are null
424 return $phpmailer;
425 }
426
427 // Tell PHPMailer to use SMTP
428 $phpmailer->isSMTP();
429
430 // Set the hostname of the mail server
431 $phpmailer->Host = $host;
432
433 // Set the SMTP port number
434 $phpmailer->Port = (int)$port;
435
436 // Whether to use SMTP authentication
437 $phpmailer->SMTPAuth = true;
438
439 // Username to use for SMTP authentication
440 $phpmailer->Username = $user;
441
442 //Password to use for SMTP authentication
443 $phpmailer->Password = $pass;
444
445 if ($secure == 'ssl' || $secure == 'tls')
446 {
447 $phpmailer->SMTPSecure = $secure;
448 } else {
449 $phpmailer->SMTPAutoTLS = false;
450 }
451
452 return $phpmailer;
453 });
454
455 return true;
456 }
457 }
458