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