PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.4
VikBooking Hotel Booking Engine & PMS v1.6.4
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.4, at libraries/adapter/mail/mail.php

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