XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
xoopsmailer.php
1<?php
13
14if (!defined('XOOPS_ROOT_PATH')) {
15 exit();
16}
17
18if (isset($GLOBALS['xoopsConfig']['language']) && file_exists(XOOPS_ROOT_PATH.'/language/'.$GLOBALS['xoopsConfig']['language'].'/mail.php')) {
19 include_once XOOPS_ROOT_PATH.'/language/'.$GLOBALS['xoopsConfig']['language'].'/mail.php';
20} else {
21 include_once XOOPS_ROOT_PATH.'/language/english/mail.php';
22}
23
28include_once(XOOPS_ROOT_PATH . '/class/mail/xoopsmultimailer.php');
29
30
39{
47 public $multimailer;
48
49 // sender email address
50 // private
51 public $fromEmail;
52
53 // sender name
54 // private
55 public $fromName;
56
57 // RMV-NOTIFY
58 // sender UID
59 // private
60 public $fromUser;
61
62 // array of user class objects
63 // private
64 public $toUsers;
65
66 // array of email addresses
67 // private
68 public $toEmails;
69
70 // custom headers
71 // private
72 public $headers;
73
74 // subjet of mail
75 // private
76 public $subject;
77
78 // body of mail
79 // private
80 public $body;
81
82 // error messages
83 // private
84 public $errors;
85
86 // messages upon success
87 // private
88 public $success;
89
90 // private
91 public $isMail;
92
93 // private
94 public $isPM;
95
96 // private
97 public $assignedTags;
98
99 // private
100 public $template;
101
102 // private
103 public $templatedir;
104
105 // protected
106 // replace iso by utf-8
107 // public $charSet = 'iso-8859-1';
108 public $charSet = 'UTF-8';
109
110 // protected
111 public $encoding = '8bit';
112
113 private $properties = [
114 'fromEmail' => '',
115 'fromName' => '',
116 'fromUser' => null, // RMV-NOTIFY
117 'priority' => '',
118 'toUsers' => [],
119 'toEmails' => [],
120 'headers' => [],
121 'subject' => '',
122 'body' => '',
123 'errors' => [],
124 'success' => [],
125 'isMail' => false,
126 'isPM' => false,
127 'assignedTags' => [],
128 'template' => '',
129 'templatedir' => '',
130 // Change below to \r\n if you have problem sending mail
131 'LE' => "\n"
132 ];
133
134 public function __construct()
135 {
136 $this->multimailer = new XoopsMultiMailer();
137 $this->reset();
138 }
139
140 // public
141 // reset all properties to default
142 public function reset()
143 {
144 foreach ($this->properties as $key => $val) {
145 $this->$key = $val;
146 }
147 }
148
149 // public
150 public function setTemplateDir($value)
151 {
152 if ('/' != substr($value, -1, 1)) {
153 $value .= '/';
154 }
155 $this->templatedir = $value;
156 }
157
158 // public
159 public function setTemplate($value)
160 {
161 $this->template = $value;
162 }
163
164 // pupblic
165 public function setFromEmail($value)
166 {
167 $this->fromEmail = trim($value);
168 }
169
170 // public
171 public function setFromName($value)
172 {
173 $this->fromName = trim($value);
174 }
175
176 // RMV-NOTIFY
177 // public
178 public function setFromUser(&$user)
179 {
180 if ('xoopsuser' == strtolower(get_class($user))) {
181 $this->fromUser =& $user;
182 }
183 }
184
185 // public
186 public function setPriority($value)
187 {
188 $this->priority = trim($value);
189 }
190
191
192 // public
193 public function setSubject($value)
194 {
195 $this->subject = trim($value);
196 }
197
198 // public
199 public function setBody($value)
200 {
201 $this->body = trim($value);
202 }
203
204 // public
205 public function useMail()
206 {
207 $this->isMail = true;
208 }
209
210 // public
211 public function usePM()
212 {
213 $this->isPM = true;
214 }
215
216 public function getVar($key)
217 {
218 if (isset($this->properties[$key])) {
219 return $this->$key;
220 } else {
221 return null;
222 }
223 }
224
225 // public
226 public function send($debug = false)
227 {
228 global $xoopsConfig;
229 if ('' == $this->body && '' == $this->template) {
230 if ($debug) {
231 $this->errors[] = _MAIL_MSGBODY;
232 }
233 return false;
234 } elseif ('' != $this->template) {
235 $path = ('' != $this->templatedir) ? $this->templatedir . '' . $this->template : (XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/mail_template/' . $this->template);
236 if (!($fd = @fopen($path, 'r'))) {
237 if ($debug) {
238 $this->errors[] = _MAIL_FAILOPTPL;
239 }
240 return false;
241 }
242 $this->setBody(fread($fd, filesize($path)));
243 }
244
245 // for sending mail only
246 if ($this->isMail || !empty($this->toEmails)) {
247 if (!empty($this->priority)) {
248 $this->headers[] = 'X-Priority: ' . $this->priority;
249 }
250 $this->headers[] = 'X-Mailer: XOOPSCube';
251 $this->headers[] = 'Return-Path: ' . $this->fromEmail;
252 $headers = implode($this->LE, $this->headers);
253 }
254
255// TODO: we should have an option of no-reply for private messages and emails
256// to which we do not accept replies. e.g. the site admin doesn't want a
257// a lot of message from people trying to unsubscribe. Just make sure to
258// give good instructions in the message.
259
260 // add some standard tags (user-dependent tags are included later)
261 global $xoopsConfig;
262 $this->assign('X_ADMINMAIL', $xoopsConfig['adminmail']);
263 $this->assign('X_SITENAME', $xoopsConfig['sitename']);
264 $this->assign('X_SITEURL', XOOPS_URL);
265 // TODO: also X_ADMINNAME??
266 // TODO: X_SIGNATURE, X_DISCLAIMER ?? - these are probably best
267 // done as includes if mail templates ever get this sophisticated
268
269 // replace tags with actual values
270 foreach ($this->assignedTags as $k => $v) {
271 $this->body = str_replace('{' . $k . '}', $v, $this->body);
272 $this->subject = str_replace('{' . $k . '}', $v, $this->subject);
273 }
274 $this->body = str_replace("\r\n", "\n", $this->body);
275 $this->body = str_replace("\r", "\n", $this->body);
276 $this->body = str_replace("\n", $this->LE, $this->body);
277
278 // send mail to specified mail addresses, if any
279 foreach ($this->toEmails as $mailaddr) {
280 if (!$this->sendMail($mailaddr, $this->subject, $this->body, $headers)) {
281 if ($debug) {
282 $this->errors[] = sprintf(_MAIL_SENDMAILNG, $mailaddr);
283 }
284 } else {
285 if ($debug) {
286 $this->success[] = sprintf(_MAIL_MAILGOOD, $mailaddr);
287 }
288 }
289 }
290
291 // send message to specified users, if any
292
293 // NOTE: we don't send to LIST of recipients, because the tags
294 // below are dependent on the user identity; i.e. each user
295 // receives (potentially) a different message
296
297 foreach ($this->toUsers as $user) {
298 // set some user specific variables
299 $subject = str_replace('{X_UNAME}', $user->getVar('uname'), $this->subject);
300 $text = str_replace('{X_UID}', $user->getVar('uid'), $this->body);
301 $text = str_replace('{X_UEMAIL}', $user->getVar('email'), $text);
302 $text = str_replace('{X_UNAME}', $user->getVar('uname'), $text);
303 $text = str_replace('{X_UACTLINK}', XOOPS_URL . '/user.php?op=actv&id=' . $user->getVar('uid') . '&actkey=' . $user->getVar('actkey'), $text);
304
305 // send mail
306 if ($this->isMail) {
307 if (!$this->sendMail($user->getVar('email'), $subject, $text, $headers)) {
308 if ($debug) {
309 $this->errors[] = sprintf(_MAIL_SENDMAILNG, $user->getVar('uname'));
310 }
311 } else {
312 if ($debug) {
313 $this->success[] = sprintf(_MAIL_MAILGOOD, $user->getVar('uname'));
314 }
315 }
316 }
317 // send private message
318 if ($this->isPM) {
319 if (!$this->sendPM($user->getVar('uid'), $subject, $text)) {
320 if ($debug) {
321 $this->errors[] = sprintf(_MAIL_SENDPMNG, $user->getVar('uname'));
322 }
323 } else {
324 if ($debug) {
325 $this->success[] = sprintf(_MAIL_PMGOOD, $user->getVar('uname'));
326 }
327 }
328 }
329
330 }
331 if (count($this->errors) > 0) {
332 return false;
333 }
334 return true;
335 }
336
337 // private
338 public function sendPM($uid, $subject, $body)
339 {
340 global $xoopsUser;
341 $pm_handler =& xoops_gethandler('privmessage');
342 $pm =& $pm_handler->create();
343 $pm->setVar('subject', $subject);
344 // RMV-NOTIFY
345 $pm->setVar('from_userid', !empty($this->fromUser) ? $this->fromUser->getVar('uid') : $xoopsUser->getVar('uid'));
346 $pm->setVar('msg_text', $body);
347 $pm->setVar('to_userid', $uid);
348 if (!$pm_handler->insert($pm)) {
349 return false;
350 }
351 return true;
352 }
353
365
366 public function sendMail($email, $subject, $body, $headers)
367 {
368 $subject = $this->encodeSubject($subject);
369 $this->encodeBody($body);
370 $this->multimailer->ClearAllRecipients();
371 $this->multimailer->AddAddress($email);
372 $this->multimailer->Subject = $subject;
373 $this->multimailer->Body = $body;
374 $this->multimailer->CharSet = $this->charSet;
375 $this->multimailer->Encoding = $this->encoding;
376 if (!empty($this->fromName)) {
377 $this->multimailer->FromName = $this->encodeFromName($this->fromName);
378 }
379 if (!empty($this->fromEmail)) {
380 $this->multimailer->From = $this->fromEmail;
381 }
382 $this->multimailer->ClearCustomHeaders();
383 foreach ($this->headers as $header) {
384 $this->multimailer->AddCustomHeader($header);
385 }
386 if (!$this->multimailer->Send()) {
387 $this->errors[] = $this->multimailer->ErrorInfo;
388 return false;
389 }
390 return true;
391 }
392
393 // public
394 public function getErrors($ashtml = true)
395 {
396 if (!$ashtml) {
397 return $this->errors;
398 } else {
399 if (!empty($this->errors)) {
400 $ret = '<h4>' . _ERRORS . '</h4>';
401 foreach ($this->errors as $error) {
402 $ret .= $error . '<br>';
403 }
404 } else {
405 $ret = '';
406 }
407 return $ret;
408 }
409 }
410
411 // public
412 public function getSuccess($ashtml = true)
413 {
414 if (!$ashtml) {
415 return $this->success;
416 } else {
417 $ret = '';
418 if (!empty($this->success)) {
419 foreach ($this->success as $suc) {
420 $ret .= $suc . '<br>';
421 }
422 }
423 return $ret;
424 }
425 }
426
427 // public
428 public function assign($tag, $value=null)
429 {
430 if (is_array($tag)) {
431 foreach ($tag as $k => $v) {
432 $this->assign($k, $v);
433 }
434 } else {
435 if (!empty($tag) && isset($value)) {
436 $tag = strtoupper(trim($tag));
437 $this->assignedTags[$tag] = $value;
438 }
439 }
440 }
441
442 // public
443 public function addHeaders($value)
444 {
445 $this->headers[] = trim($value).$this->LE;
446 }
447
448 // public
449 public function setToEmails($email)
450 {
451 if (!is_array($email)) {
452 if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i", $email)) {
453 array_push($this->toEmails, $email);
454 }
455 } else {
456 foreach ($email as $e) {
457 $this->setToEmails($e);
458 }
459 }
460 }
461
462 // public
463 public function setToUsers(&$user)
464 {
465 if (!is_array($user)) {
466 //@ToDo $user should be either XoopsUser or UserUsersObject now
467 if (in_array(strtolower(get_class($user)), ['xoopsuser', 'userusersobject'])) {
468 array_push($this->toUsers, $user);
469 }
470 } else {
471 foreach ($user as $u) {
472 $this->setToUsers($u);
473 }
474 }
475 }
476
477 // public
478 public function setToGroups($group)
479 {
480 if (!is_array($group)) {
481 if ('xoopsgroup' == strtolower(get_class($group))) {
482 $member_handler =& xoops_gethandler('member');
483 $groups=&$member_handler->getUsersByGroup($group->getVar('groupid'), true);
484 $this->setToUsers($groups, true);
485 }
486 } else {
487 foreach ($group as $g) {
488 $this->setToGroups($g);
489 }
490 }
491 }
492
493 // abstract
494 // to be overidden by lang specific mail class, if needed
495 public function encodeFromName($text)
496 {
497 return $text;
498 }
499
500 // abstract
501 // to be overidden by lang specific mail class, if needed
502 public function encodeSubject($text)
503 {
504 return $text;
505 }
506
507 // abstract
508 // to be overidden by lang specific mail class, if needed
509 public function encodeBody(&$text)
510 {
511 }
512}
sendMail($email, $subject, $body, $headers)