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