1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: use Xoops\Core\Kernel\Handlers\XoopsGroup;
13: use Xoops\Core\Kernel\Handlers\XoopsUser;
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33: 34: 35:
36: class XoopsMailer
37: {
38: 39: 40: 41: 42: 43: 44:
45: protected $multimailer;
46:
47: 48: 49: 50: 51:
52: private $fromEmail;
53:
54: 55: 56: 57: 58:
59: private $fromName;
60:
61: 62: 63: 64: 65:
66: private $fromUser;
67:
68: 69: 70: 71: 72:
73: private $toUsers;
74:
75: 76: 77: 78: 79:
80: private $toEmails;
81:
82:
83: 84: 85: 86: 87:
88: private $headers;
89:
90: 91: 92: 93: 94:
95: private $subject;
96:
97: 98: 99: 100: 101:
102: private $body;
103:
104: 105: 106: 107: 108:
109: private $errors;
110:
111: 112: 113: 114: 115:
116: private $success;
117:
118: 119: 120:
121: private $isMail;
122:
123: 124: 125:
126: private $isPM;
127:
128: 129: 130:
131: private $assignedTags;
132:
133: 134: 135:
136: private $template;
137:
138: 139: 140:
141: private $templatedir;
142:
143: 144: 145:
146: protected $charSet = 'iso-8859-1';
147:
148: 149: 150:
151: protected $encoding = '8bit';
152:
153: 154: 155:
156: private $priority;
157:
158: 159: 160:
161: private $LE;
162:
163: 164: 165: 166: 167:
168: public function __construct()
169: {
170: $this->multimailer = new XoopsMultiMailer();
171: $this->reset();
172: }
173:
174: 175: 176: 177: 178: 179:
180: public function setHTML($value = true)
181: {
182: $this->multimailer->isHTML($value);
183: }
184:
185: 186: 187: 188: 189:
190: public function reset()
191: {
192: $this->fromEmail = "";
193: $this->fromName = "";
194: $this->fromUser = null;
195: $this->priority = '';
196: $this->toUsers = array();
197: $this->toEmails = array();
198: $this->headers = array();
199: $this->subject = "";
200: $this->body = "";
201: $this->errors = array();
202: $this->success = array();
203: $this->isMail = false;
204: $this->isPM = false;
205: $this->assignedTags = array();
206: $this->template = "";
207: $this->templatedir = "";
208:
209: $this->LE = "\n";
210: }
211:
212: 213: 214: 215:
216: public function setTemplateDir($value = null)
217: {
218: $xoops = Xoops::getInstance();
219: if ($value === null && $xoops->isModule()) {
220: $value = $xoops->module->getVar('dirname', 'n');
221: } else {
222: $value = str_replace(DIRECTORY_SEPARATOR, "/", $value);
223: }
224: $this->templatedir = $value;
225: }
226:
227: 228: 229:
230: private function getTemplatePath()
231: {
232: $xoops = Xoops::getInstance();
233: if (!$path = $this->templatedir) {
234: $path = \XoopsBaseConfig::get('root-path') . "/locale/";
235: } elseif (false === strpos($path, '/')) {
236: $path = \XoopsBaseConfig::get('root-path') . "/modules/" . $path . "/locale/";
237: } elseif (substr($path, -1, 1) !== "/") {
238: $path .= "/";
239: }
240: if (XoopsLoad::fileExists($path . $xoops->getConfig('locale') . "/templates/" . $this->template)) {
241: return $path . $xoops->getConfig('locale') . "/templates/" . $this->template;
242: } elseif (XoopsLoad::fileExists($path . "en_US/templates/" . $this->template)) {
243: return $path . "en_US/templates/" . $this->template;
244: } elseif (XoopsLoad::fileExists($path . $this->template)) {
245: return $path . $this->template;
246: } else {
247: return false;
248: }
249: }
250:
251: 252: 253: 254:
255: public function setTemplate($value)
256: {
257: $this->template = $value;
258: }
259:
260: 261: 262: 263:
264: public function setFromEmail($value)
265: {
266: $this->fromEmail = trim($value);
267: }
268:
269: 270: 271: 272:
273: public function setFromName($value)
274: {
275: $this->fromName = trim($value);
276: }
277:
278: 279: 280: 281:
282: public function setFromUser(XoopsUser $user)
283: {
284: $this->fromUser = $user;
285: }
286:
287: 288: 289: 290:
291: public function setPriority($value)
292: {
293: $this->priority = trim($value);
294: }
295:
296: 297: 298: 299:
300: public function setSubject($value)
301: {
302: $this->subject = trim($value);
303: }
304:
305: 306: 307: 308:
309: public function setBody($value)
310: {
311: $this->body = trim($value);
312: }
313:
314: 315: 316:
317: public function useMail()
318: {
319: $this->isMail = true;
320: }
321:
322: 323: 324:
325: public function usePM()
326: {
327: $this->isPM = true;
328: }
329:
330: 331: 332: 333:
334: public function send($debug = false)
335: {
336: $xoops = Xoops::getInstance();
337: if ($this->body == "" && $this->template == "") {
338: if ($debug) {
339: $this->errors[] = XoopsLocale::E_MESSAGE_BODY_NOT_SET;
340: }
341: return false;
342: } elseif ($this->template != "") {
343: $path = $this->getTemplatePath();
344: if (!($fd = @fopen($path, 'r'))) {
345: if ($debug) {
346: $this->errors[] = XoopsLocale::E_TEMPLATE_FILE_NOT_OPENED;
347: }
348: return false;
349: }
350: $this->setBody(fread($fd, filesize($path)));
351: }
352: $headers = '';
353:
354: if ($this->isMail || !empty($this->toEmails)) {
355: if (!empty($this->priority)) {
356: $this->headers[] = "X-Priority: " . $this->priority;
357: }
358:
359:
360: $headers = join($this->LE, $this->headers);
361: }
362:
363:
364:
365:
366:
367:
368: $this->assign('X_ADMINMAIL', $xoops->getConfig('adminmail'));
369: $this->assign('X_SITENAME', $xoops->getConfig('sitename'));
370: $this->assign('X_SITEURL', \XoopsBaseConfig::get('url') . "/");
371:
372:
373:
374:
375: foreach ($this->assignedTags as $k => $v) {
376: $this->body = str_replace("{" . $k . "}", $v, $this->body);
377: $this->subject = str_replace("{" . $k . "}", $v, $this->subject);
378: }
379: $this->body = str_replace("\r\n", "\n", $this->body);
380: $this->body = str_replace("\r", "\n", $this->body);
381: $this->body = str_replace("\n", $this->LE, $this->body);
382:
383: foreach ($this->toEmails as $mailaddr) {
384: if (!$this->sendMail($mailaddr, $this->subject, $this->body, $headers)) {
385: if ($debug) {
386: $this->errors[] = sprintf(XoopsLocale::EF_EMAIL_NOT_SENT_TO, $mailaddr);
387: }
388: } else {
389: if ($debug) {
390: $this->success[] = sprintf(XoopsLocale::SF_EMAIL_SENT_TO, $mailaddr);
391: }
392: }
393: }
394:
395:
396:
397:
398: foreach ($this->toUsers as $user) {
399:
400:
401: $subject = str_replace("{X_UNAME}", $user->getVar("uname"), $this->subject);
402: $text = str_replace("{X_UID}", $user->getVar("uid"), $this->body);
403: $text = str_replace("{X_UEMAIL}", $user->getVar("email"), $text);
404: $text = str_replace("{X_UNAME}", $user->getVar("uname"), $text);
405: $text = str_replace("{X_UACTLINK}", \XoopsBaseConfig::get('url') . "/register.php?op=actv&id=" . $user->getVar("uid") . "&actkey=" . $user->getVar('actkey'), $text);
406:
407: if ($this->isMail) {
408: if (!$this->sendMail($user->getVar("email"), $subject, $text, $headers)) {
409: if ($debug) {
410: $this->errors[] = sprintf(XoopsLocale::EF_EMAIL_NOT_SENT_TO, $user->getVar("uname"));
411: }
412: } else {
413: if ($debug) {
414: $this->success[] = sprintf(XoopsLocale::SF_EMAIL_SENT_TO, $user->getVar("uname"));
415: }
416: }
417: }
418:
419: if ($this->isPM) {
420: if (!$this->sendPM($user->getVar("uid"), $subject, $text)) {
421: if ($debug) {
422: $this->errors[] = sprintf(XoopsLocale::EF_PRIVATE_MESSAGE_NOT_SENT_TO, $user->getVar("uname"));
423: }
424: } else {
425: if ($debug) {
426: $this->success[] = sprintf(XoopsLocale::SF_PRIVATE_MESSAGE_SENT_TO, $user->getVar("uname"));
427: }
428: }
429: }
430: flush();
431: }
432: if (count($this->errors) > 0) {
433: return false;
434: }
435: return true;
436: }
437:
438: 439: 440: 441: 442: 443:
444: private function sendPM($uid, $subject, $body)
445: {
446: $xoops = Xoops::getInstance();
447: $pm_handler = $xoops->getHandlerPrivateMessage();
448: $pm = $pm_handler->create();
449: $pm->setVar("subject", $subject);
450:
451: $pm->setVar('from_userid', !empty($this->fromUser) ? $this->fromUser->getVar('uid') : (!$xoops->isUser() ? 1
452: : $xoops->user->getVar('uid')));
453: $pm->setVar("msg_text", $body);
454: $pm->setVar("to_userid", $uid);
455: $pm->setVar('msg_time', time());
456: if (!$pm_handler->insert($pm)) {
457: return false;
458: }
459: return true;
460: }
461:
462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472:
473: private function sendMail($email, $subject, $body, $headers)
474: {
475: $subject = $this->encodeSubject($subject);
476: $this->encodeBody($body);
477: $this->multimailer->ClearAllRecipients();
478: $this->multimailer->AddAddress($email);
479: $this->multimailer->Subject = $subject;
480: $this->multimailer->Body = $body;
481: $this->multimailer->CharSet = $this->charSet;
482: $this->multimailer->Encoding = $this->encoding;
483: if (!empty($this->fromName)) {
484: $this->multimailer->FromName = $this->encodeFromName($this->fromName);
485: }
486: if (!empty($this->fromEmail)) {
487: $this->multimailer->Sender = $this->multimailer->From = $this->fromEmail;
488: }
489:
490: $this->multimailer->ClearCustomHeaders();
491: foreach ($this->headers as $header) {
492: $this->multimailer->AddCustomHeader($header);
493: }
494: if (!$this->multimailer->Send()) {
495: $this->errors[] = $this->multimailer->ErrorInfo;
496: return false;
497: }
498: return true;
499: }
500:
501: 502: 503: 504:
505: public function getErrors($ashtml = true)
506: {
507: if (!$ashtml) {
508: return $this->errors;
509: } else {
510: $ret = "";
511: if (!empty($this->errors)) {
512: $ret = "<h4>" . XoopsLocale::ERRORS . "</h4>";
513: foreach ($this->errors as $error) {
514: $ret .= $error . "<br />";
515: }
516: }
517: return $ret;
518: }
519: }
520:
521:
522: function getSuccess($ashtml = true)
523: {
524: if (!$ashtml) {
525: return $this->success;
526: } else {
527: $ret = "";
528: if (!empty($this->success)) {
529: foreach ($this->success as $suc) {
530: $ret .= $suc . "<br />";
531: }
532: }
533: return $ret;
534: }
535: }
536:
537: 538: 539: 540: 541:
542: public function assign($tag, $value = null)
543: {
544: if (is_array($tag)) {
545: foreach ($tag as $k => $v) {
546: $this->assign($k, $v);
547: }
548: } else {
549: if (!empty($tag) && isset($value)) {
550: $tag = strtoupper(trim($tag));
551:
552:
553:
554: $this->assignedTags[$tag] = $value;
555:
556: }
557: }
558: }
559:
560: 561: 562: 563:
564: public function addHeaders($value)
565: {
566: $this->headers[] = trim($value) . $this->LE;
567: }
568:
569: 570: 571: 572:
573: public function setToEmails($email)
574: {
575: if (!is_array($email)) {
576: $xoops = Xoops::getInstance();
577: if ($xoops->checkEmail($email)) {
578: array_push($this->toEmails, $email);
579: }
580: } else {
581: foreach ($email as $e) {
582: $this->setToEmails($e);
583: }
584: }
585: }
586:
587: 588: 589: 590:
591: public function setToUsers($users)
592: {
593: if ($users instanceof XoopsUser) {
594: array_push($this->toUsers, $users);
595: } elseif (is_array($users)) {
596: foreach ($users as $u) {
597: $this->setToUsers($u);
598: }
599: }
600: }
601:
602: 603: 604: 605:
606: public function setToGroups($groups)
607: {
608: if ($groups instanceof XoopsGroup) {
609: $this->setToUsers(Xoops::getInstance()
610: ->getHandlerMember()
611: ->getUsersByGroup($groups->getVar('groupid'), true));
612:
613: } elseif (is_array($groups)) {
614: foreach ($groups as $g) {
615: $this->setToGroups($g);
616: }
617: }
618: }
619:
620: 621: 622: 623: 624: 625:
626: public function encodeFromName($text)
627: {
628: return $text;
629: }
630:
631: 632: 633: 634: 635: 636:
637: public function encodeSubject($text)
638: {
639: return $text;
640: }
641:
642: 643: 644: 645: 646: 647:
648: public function encodeBody(&$text)
649: {
650: }
651: }
652: