108 public $charSet =
'UTF-8';
111 public $encoding =
'8bit';
113 private $properties = [
127 'assignedTags' => [],
134 public function __construct()
136 $this->multimailer =
new XoopsMultiMailer();
142 public function reset()
144 foreach ($this->properties as $key => $val) {
150 public function setTemplateDir($value)
152 if (
'/' != substr($value, -1, 1)) {
155 $this->templatedir = $value;
159 public function setTemplate($value)
161 $this->
template = $value;
165 public function setFromEmail($value)
167 $this->fromEmail = trim($value);
171 public function setFromName($value)
173 $this->fromName = trim($value);
178 public function setFromUser(&$user)
180 if (
'xoopsuser' == strtolower(get_class($user))) {
181 $this->fromUser =& $user;
186 public function setPriority($value)
188 $this->priority = trim($value);
193 public function setSubject($value)
195 $this->subject = trim($value);
199 public function setBody($value)
201 $this->body = trim($value);
205 public function useMail()
207 $this->isMail =
true;
211 public function usePM()
216 public function getVar($key)
218 if (isset($this->properties[$key])) {
226 public function send($debug =
false)
229 if (
'' == $this->body &&
'' == $this->
template) {
231 $this->errors[] = _MAIL_MSGBODY;
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'))) {
238 $this->errors[] = _MAIL_FAILOPTPL;
242 $this->setBody(fread($fd, filesize($path)));
246 if ($this->isMail || !empty($this->toEmails)) {
247 if (!empty($this->priority)) {
248 $this->headers[] =
'X-Priority: ' . $this->priority;
250 $this->headers[] =
'X-Mailer: XOOPSCube';
251 $this->headers[] =
'Return-Path: ' . $this->fromEmail;
252 $headers = implode($this->LE, $this->headers);
262 $this->assign(
'X_ADMINMAIL', $xoopsConfig[
'adminmail']);
263 $this->assign(
'X_SITENAME', $xoopsConfig[
'sitename']);
264 $this->assign(
'X_SITEURL', XOOPS_URL);
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);
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);
279 foreach ($this->toEmails as $mailaddr) {
280 if (!$this->
sendMail($mailaddr, $this->subject, $this->body, $headers)) {
282 $this->errors[] = sprintf(_MAIL_SENDMAILNG, $mailaddr);
286 $this->success[] = sprintf(_MAIL_MAILGOOD, $mailaddr);
297 foreach ($this->toUsers as $user) {
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);
307 if (!$this->
sendMail($user->getVar(
'email'), $subject, $text, $headers)) {
309 $this->errors[] = sprintf(_MAIL_SENDMAILNG, $user->getVar(
'uname'));
313 $this->success[] = sprintf(_MAIL_MAILGOOD, $user->getVar(
'uname'));
319 if (!$this->sendPM($user->getVar(
'uid'), $subject, $text)) {
321 $this->errors[] = sprintf(_MAIL_SENDPMNG, $user->getVar(
'uname'));
325 $this->success[] = sprintf(_MAIL_PMGOOD, $user->getVar(
'uname'));
331 if (count($this->errors) > 0) {
338 public function sendPM($uid, $subject, $body)
341 $pm_handler =& xoops_gethandler(
'privmessage');
342 $pm =& $pm_handler->create();
343 $pm->setVar(
'subject', $subject);
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)) {
366 public function sendMail($email, $subject, $body, $headers)
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);
379 if (!empty($this->fromEmail)) {
380 $this->multimailer->From = $this->fromEmail;
382 $this->multimailer->ClearCustomHeaders();
383 foreach ($this->headers as $header) {
384 $this->multimailer->AddCustomHeader($header);
386 if (!$this->multimailer->Send()) {
387 $this->errors[] = $this->multimailer->ErrorInfo;
394 public function getErrors($ashtml =
true)
397 return $this->errors;
399 if (!empty($this->errors)) {
400 $ret =
'<h4>' . _ERRORS .
'</h4>';
401 foreach ($this->errors as $error) {
402 $ret .= $error .
'<br>';
412 public function getSuccess($ashtml =
true)
415 return $this->success;
418 if (!empty($this->success)) {
419 foreach ($this->success as $suc) {
420 $ret .= $suc .
'<br>';
428 public function assign($tag, $value=
null)
430 if (is_array($tag)) {
431 foreach ($tag as $k => $v) {
432 $this->assign($k, $v);
435 if (!empty($tag) && isset($value)) {
436 $tag = strtoupper(trim($tag));
437 $this->assignedTags[$tag] = $value;
443 public function addHeaders($value)
445 $this->headers[] = trim($value).$this->LE;
449 public function setToEmails($email)
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);
456 foreach ($email as $e) {
457 $this->setToEmails($e);
463 public function setToUsers(&$user)
465 if (!is_array($user)) {
467 if (in_array(strtolower(get_class($user)), [
'xoopsuser',
'userusersobject'])) {
468 array_push($this->toUsers, $user);
471 foreach ($user as $u) {
472 $this->setToUsers($u);
478 public function setToGroups($group)
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);
487 foreach ($group as $g) {
488 $this->setToGroups($g);
495 public function encodeFromName($text)
502 public function encodeSubject($text)
509 public function encodeBody(&$text)