21 defined(
'XOOPS_ROOT_PATH') or die('Restricted access');
29 include_once
$GLOBALS['xoops']->path('class/mail/xoopsmultimailer.php');
92 var $charSet =
'iso-8859-1';
94 var $encoding =
'8bit';
101 function XoopsMailer()
108 function setHTML($value =
true)
110 $this->multimailer->isHTML($value);
117 $this->fromEmail =
"";
118 $this->fromName =
"";
119 $this->fromUser = null;
120 $this->priority =
'';
121 $this->toUsers = array();
122 $this->toEmails = array();
123 $this->headers = array();
126 $this->errors = array();
127 $this->success = array();
128 $this->isMail =
false;
130 $this->assignedTags = array();
131 $this->
template =
"";
132 $this->templatedir =
"";
138 function setTemplateDir($value = null)
140 if ($value === null && is_object(
$GLOBALS[
'xoopsModule'])) {
141 $value =
$GLOBALS[
'xoopsModule']->getVar(
'dirname',
'n');
145 $this->templatedir = $value;
149 function getTemplatePath()
151 if (!
$path = $this->templatedir) {
152 $path = XOOPS_ROOT_PATH .
"/language/";
153 } elseif (
false === strpos(
$path,
'/')) {
154 $path = XOOPS_ROOT_PATH .
"/modules/" .
$path .
"/language/";
155 } elseif (substr(
$path, - 1, 1) !=
"/") {
158 if (file_exists(
$path .
$GLOBALS[
'xoopsConfig'][
'language'] .
"/mail_template/" . $this->
template)) {
159 return $path . $GLOBALS[
'xoopsConfig'][
'language'] .
"/mail_template/" . $this->template;
160 } elseif (file_exists(
$path .
"english/mail_template/" . $this->
template)) {
161 return $path .
"english/mail_template/" . $this->template;
162 } elseif (file_exists(
$path . $this->
template)) {
163 return $path . $this->template;
170 function setTemplate($value)
172 $this->
template = $value;
176 function setFromEmail($value)
178 $this->fromEmail = trim($value);
182 function setFromName($value)
184 $this->fromName = trim($value);
191 if (strtolower(get_class(
$user)) ==
"xoopsuser") {
192 $this->fromUser = &
$user;
197 function setPriority($value)
199 $this->priority = trim($value);
203 function setSubject($value)
205 $this->subject = trim($value);
209 function setBody($value)
211 $this->body = trim($value);
217 $this->isMail =
true;
227 function send($debug =
false)
230 if ($this->body ==
"" && $this->
template ==
"") {
235 } elseif ($this->
template !=
"") {
236 $path = $this->getTemplatePath();
237 if (! ($fd = @fopen(
$path,
'r'))) {
243 $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;
252 $headers = join($this->LE, $this->headers);
261 $this->assign(
'X_ADMINMAIL', $xoopsConfig[
'adminmail']);
262 $this->assign(
'X_SITENAME', $xoopsConfig[
'sitename']);
263 $this->assign(
'X_SITEURL', XOOPS_URL .
"/");
268 foreach($this->assignedTags as $k => $v) {
269 $this->body = str_replace(
"{" . $k .
"}", $v, $this->body);
270 $this->subject = str_replace(
"{" . $k .
"}", $v, $this->subject);
272 $this->body = str_replace(
"\r\n",
"\n", $this->body);
273 $this->body = str_replace(
"\r",
"\n", $this->body);
274 $this->body = str_replace(
"\n", $this->LE, $this->body);
276 foreach($this->toEmails as $mailaddr) {
277 if (! $this->sendMail($mailaddr, $this->subject, $this->body, $headers)) {
291 foreach($this->toUsers as
$user) {
293 $subject = str_replace(
"{X_UNAME}", $user->getVar(
"uname"), $this->subject);
294 $text = str_replace(
"{X_UID}", $user->getVar(
"uid"), $this->body);
295 $text = str_replace(
"{X_UEMAIL}", $user->getVar(
"email"), $text);
296 $text = str_replace(
"{X_UNAME}", $user->getVar(
"uname"), $text);
297 $text = str_replace(
"{X_UACTLINK}", XOOPS_URL .
"/register.php?op=actv&id=" . $user->getVar(
"uid") .
"&actkey=" . $user->getVar(
'actkey'), $text);
300 if (! $this->sendMail($user->getVar(
"email"), $subject, $text, $headers)) {
306 $this->success[] = sprintf(
_MAIL_MAILGOOD, $user->getVar(
"uname"));
312 if (! $this->sendPM($user->getVar(
"uid"), $subject, $text)) {
314 $this->errors[] = sprintf(
_MAIL_SENDPMNG, $user->getVar(
"uname"));
318 $this->success[] = sprintf(
_MAIL_PMGOOD, $user->getVar(
"uname"));
324 if (count($this->errors) > 0) {
331 function sendPM(
$uid, $subject, $body)
336 $pm->setVar(
"subject", $subject);
338 $pm->setVar(
'from_userid', ! empty($this->fromUser) ? $this->fromUser->getVar(
'uid') : (empty($xoopsUser) ? 1 : $xoopsUser->getVar(
'uid')));
339 $pm->setVar(
"msg_text", $body);
340 $pm->setVar(
"to_userid",
$uid);
358 function sendMail(
$email, $subject, $body, $headers)
360 $subject = $this->encodeSubject($subject);
361 $this->encodeBody($body);
362 $this->multimailer->ClearAllRecipients();
363 $this->multimailer->AddAddress(
$email);
364 $this->multimailer->Subject = $subject;
365 $this->multimailer->Body = $body;
366 $this->multimailer->CharSet = $this->charSet;
367 $this->multimailer->Encoding = $this->encoding;
368 if (! empty($this->fromName)) {
369 $this->multimailer->FromName = $this->encodeFromName($this->fromName);
371 if (! empty($this->fromEmail)) {
372 $this->multimailer->Sender = $this->multimailer->From = $this->fromEmail;
375 $this->multimailer->ClearCustomHeaders();
376 foreach($this->headers as $header) {
377 $this->multimailer->AddCustomHeader($header);
379 if (! $this->multimailer->Send()) {
380 $this->errors[] = $this->multimailer->ErrorInfo;
387 function getErrors($ashtml =
true)
392 if (! empty($this->errors)) {
394 foreach($this->errors as
$error) {
395 $ret .= $error .
"<br />";
405 function getSuccess($ashtml =
true)
408 return $this->success;
411 if (! empty($this->success)) {
412 foreach($this->success as $suc) {
413 $ret .= $suc .
"<br />";
421 function assign($tag, $value = null)
423 if (is_array($tag)) {
424 foreach($tag as $k => $v) {
425 $this->assign($k, $v);
428 if (! empty($tag) && isset($value)) {
429 $tag = strtoupper(trim($tag));
433 $this->assignedTags[$tag] = $value;
440 function addHeaders($value)
442 $this->headers[] = trim($value) . $this->LE;
449 if (preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+([\.][a-z0-9-]+)+$/i",
$email)) {
450 array_push($this->toEmails,
$email);
454 $this->setToEmails($e);
462 if (! is_array(
$user)) {
463 if (strtolower(get_class(
$user)) ==
"xoopsuser") {
464 array_push($this->toUsers,
$user);
467 foreach(
$user as $u) {
468 $this->setToUsers($u);
474 function setToGroups($group)
476 if (! is_array($group)) {
477 if (strtolower(get_class($group)) ==
"xoopsgroup") {
479 $this->setToUsers(
$member_handler->getUsersByGroup($group->getVar(
'groupid'),
true));
482 foreach($group as $g) {
483 $this->setToGroups($g);
490 function encodeFromName($text)
497 function encodeSubject($text)
504 function encodeBody(&$text)