XOOPS  2.6.0
xoopsmailer.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
34 {
42  protected $multimailer;
43 
49  private $fromEmail;
50 
56  private $fromName;
57 
63  private $fromUser;
64 
70  private $toUsers;
71 
77  private $toEmails;
78 
79  // private
85  private $headers;
86 
92  private $subject;
93 
99  private $body;
100 
106  private $errors;
107 
113  private $success;
114 
118  private $isMail;
119 
123  private $isPM;
124 
128  private $assignedTags;
129 
133  private $template;
134 
138  private $templatedir;
139 
143  protected $charSet = 'iso-8859-1';
144 
148  protected $encoding = '8bit';
149 
153  private $priority;
154 
158  private $LE;
159 
165  public function __construct()
166  {
167  $this->multimailer = new XoopsMultiMailer();
168  $this->reset();
169  }
170 
177  public function setHTML($value = true)
178  {
179  $this->multimailer->isHTML($value);
180  }
181 
187  public function reset()
188  {
189  $this->fromEmail = "";
190  $this->fromName = "";
191  $this->fromUser = null; // RMV-NOTIFY
192  $this->priority = '';
193  $this->toUsers = array();
194  $this->toEmails = array();
195  $this->headers = array();
196  $this->subject = "";
197  $this->body = "";
198  $this->errors = array();
199  $this->success = array();
200  $this->isMail = false;
201  $this->isPM = false;
202  $this->assignedTags = array();
203  $this->template = "";
204  $this->templatedir = "";
205  // Change below to \r\n if you have problem sending mail
206  $this->LE = "\n";
207  }
208 
213  public function setTemplateDir($value = null)
214  {
216  if ($value === null && $xoops->isModule()) {
217  $value = $xoops->module->getVar('dirname', 'n');
218  } else {
219  $value = str_replace(DIRECTORY_SEPARATOR, "/", $value);
220  }
221  $this->templatedir = $value;
222  }
223 
227  private function getTemplatePath()
228  {
230  if (!$path = $this->templatedir) {
231  $path = \XoopsBaseConfig::get('root-path') . "/locale/";
232  } elseif (false === strpos($path, '/')) {
233  $path = \XoopsBaseConfig::get('root-path') . "/modules/" . $path . "/locale/";
234  } elseif (substr($path, -1, 1) != "/") {
235  $path .= "/";
236  }
237  if (XoopsLoad::fileExists($path . $xoops->getConfig('locale') . "/templates/" . $this->template)) {
238  return $path . $xoops->getConfig('locale') . "/templates/" . $this->template;
239  } elseif (XoopsLoad::fileExists($path . "en_US/templates/" . $this->template)) {
240  return $path . "en_US/templates/" . $this->template;
241  } elseif (XoopsLoad::fileExists($path . $this->template)) {
242  return $path . $this->template;
243  } else {
244  return false;
245  }
246  }
247 
252  public function setTemplate($value)
253  {
254  $this->template = $value;
255  }
256 
261  public function setFromEmail($value)
262  {
263  $this->fromEmail = trim($value);
264  }
265 
270  public function setFromName($value)
271  {
272  $this->fromName = trim($value);
273  }
274 
279  public function setFromUser(XoopsUser $user)
280  {
281  $this->fromUser = $user;
282  }
283 
288  public function setPriority($value)
289  {
290  $this->priority = trim($value);
291  }
292 
297  public function setSubject($value)
298  {
299  $this->subject = trim($value);
300  }
301 
306  public function setBody($value)
307  {
308  $this->body = trim($value);
309  }
310 
314  public function useMail()
315  {
316  $this->isMail = true;
317  }
318 
322  public function usePM()
323  {
324  $this->isPM = true;
325  }
326 
331  public function send($debug = false)
332  {
334  if ($this->body == "" && $this->template == "") {
335  if ($debug) {
336  $this->errors[] = XoopsLocale::E_MESSAGE_BODY_NOT_SET;
337  }
338  return false;
339  } elseif ($this->template != "") {
340  $path = $this->getTemplatePath();
341  if (!($fd = @fopen($path, 'r'))) {
342  if ($debug) {
343  $this->errors[] = XoopsLocale::E_TEMPLATE_FILE_NOT_OPENED;
344  }
345  return false;
346  }
347  $this->setBody(fread($fd, filesize($path)));
348  }
349  $headers = '';
350  // for sending mail only
351  if ($this->isMail || !empty($this->toEmails)) {
352  if (!empty($this->priority)) {
353  $this->headers[] = "X-Priority: " . $this->priority;
354  }
355  // $this->headers[] = "X-Mailer: PHP/".phpversion();
356  // $this->headers[] = "Return-Path: ".$this->fromEmail;
357  $headers = join($this->LE, $this->headers);
358  }
359  // TODO: we should have an option of no-reply for private messages and emails
360  // to which we do not accept replies. e.g. the site admin doesn't want a
361  // a lot of message from people trying to unsubscribe. Just make sure to
362  // give good instructions in the message.
363  // add some standard tags (user-dependent tags are included later)
364 
365  $this->assign('X_ADMINMAIL', $xoops->getConfig('adminmail'));
366  $this->assign('X_SITENAME', $xoops->getConfig('sitename'));
367  $this->assign('X_SITEURL', \XoopsBaseConfig::get('url') . "/");
368  // TODO: also X_ADMINNAME??
369  // TODO: X_SIGNATURE, X_DISCLAIMER ?? - these are probably best
370  // done as includes if mail templates ever get this sophisticated
371  // replace tags with actual values
372  foreach ($this->assignedTags as $k => $v) {
373  $this->body = str_replace("{" . $k . "}", $v, $this->body);
374  $this->subject = str_replace("{" . $k . "}", $v, $this->subject);
375  }
376  $this->body = str_replace("\r\n", "\n", $this->body);
377  $this->body = str_replace("\r", "\n", $this->body);
378  $this->body = str_replace("\n", $this->LE, $this->body);
379  // send mail to specified mail addresses, if any
380  foreach ($this->toEmails as $mailaddr) {
381  if (!$this->sendMail($mailaddr, $this->subject, $this->body, $headers)) {
382  if ($debug) {
383  $this->errors[] = sprintf(XoopsLocale::EF_EMAIL_NOT_SENT_TO, $mailaddr);
384  }
385  } else {
386  if ($debug) {
387  $this->success[] = sprintf(XoopsLocale::SF_EMAIL_SENT_TO, $mailaddr);
388  }
389  }
390  }
391  // send message to specified users, if any
392  // NOTE: we don't send to LIST of recipients, because the tags
393  // below are dependent on the user identity; i.e. each user
394  // receives (potentially) a different message
395  foreach ($this->toUsers as $user) {
396  /* @var $user XoopsUser */
397  // set some user specific variables
398  $subject = str_replace("{X_UNAME}", $user->getVar("uname"), $this->subject);
399  $text = str_replace("{X_UID}", $user->getVar("uid"), $this->body);
400  $text = str_replace("{X_UEMAIL}", $user->getVar("email"), $text);
401  $text = str_replace("{X_UNAME}", $user->getVar("uname"), $text);
402  $text = str_replace("{X_UACTLINK}", \XoopsBaseConfig::get('url') . "/register.php?op=actv&id=" . $user->getVar("uid") . "&actkey=" . $user->getVar('actkey'), $text);
403  // send mail
404  if ($this->isMail) {
405  if (!$this->sendMail($user->getVar("email"), $subject, $text, $headers)) {
406  if ($debug) {
407  $this->errors[] = sprintf(XoopsLocale::EF_EMAIL_NOT_SENT_TO, $user->getVar("uname"));
408  }
409  } else {
410  if ($debug) {
411  $this->success[] = sprintf(XoopsLocale::SF_EMAIL_SENT_TO, $user->getVar("uname"));
412  }
413  }
414  }
415  // send private message
416  if ($this->isPM) {
417  if (!$this->sendPM($user->getVar("uid"), $subject, $text)) {
418  if ($debug) {
419  $this->errors[] = sprintf(XoopsLocale::EF_PRIVATE_MESSAGE_NOT_SENT_TO, $user->getVar("uname"));
420  }
421  } else {
422  if ($debug) {
423  $this->success[] = sprintf(XoopsLocale::SF_PRIVATE_MESSAGE_SENT_TO, $user->getVar("uname"));
424  }
425  }
426  }
427  flush();
428  }
429  if (count($this->errors) > 0) {
430  return false;
431  }
432  return true;
433  }
434 
441  private function sendPM($uid, $subject, $body)
442  {
444  $pm_handler = $xoops->getHandlerPrivmessage();
445  $pm = $pm_handler->create();
446  $pm->setVar("subject", $subject);
447  // RMV-NOTIFY
448  $pm->setVar('from_userid', !empty($this->fromUser) ? $this->fromUser->getVar('uid') : (!$xoops->isUser() ? 1
449  : $xoops->user->getVar('uid')));
450  $pm->setVar("msg_text", $body);
451  $pm->setVar("to_userid", $uid);
452  $pm->setVar('msg_time', time());
453  if (!$pm_handler->insert($pm)) {
454  return false;
455  }
456  return true;
457  }
458 
470  private function sendMail($email, $subject, $body, $headers)
471  {
472  $subject = $this->encodeSubject($subject);
473  $this->encodeBody($body);
474  $this->multimailer->ClearAllRecipients();
475  $this->multimailer->AddAddress($email);
476  $this->multimailer->Subject = $subject;
477  $this->multimailer->Body = $body;
478  $this->multimailer->CharSet = $this->charSet;
479  $this->multimailer->Encoding = $this->encoding;
480  if (!empty($this->fromName)) {
481  $this->multimailer->FromName = $this->encodeFromName($this->fromName);
482  }
483  if (!empty($this->fromEmail)) {
484  $this->multimailer->Sender = $this->multimailer->From = $this->fromEmail;
485  }
486 
487  $this->multimailer->ClearCustomHeaders();
488  foreach ($this->headers as $header) {
489  $this->multimailer->AddCustomHeader($header);
490  }
491  if (!$this->multimailer->Send()) {
492  $this->errors[] = $this->multimailer->ErrorInfo;
493  return false;
494  }
495  return true;
496  }
497 
502  public function getErrors($ashtml = true)
503  {
504  if (!$ashtml) {
505  return $this->errors;
506  } else {
507  $ret = "";
508  if (!empty($this->errors)) {
509  $ret = "<h4>" . XoopsLocale::ERRORS . "</h4>";
510  foreach ($this->errors as $error) {
511  $ret .= $error . "<br />";
512  }
513  }
514  return $ret;
515  }
516  }
517 
518  // public
519  function getSuccess($ashtml = true)
520  {
521  if (!$ashtml) {
522  return $this->success;
523  } else {
524  $ret = "";
525  if (!empty($this->success)) {
526  foreach ($this->success as $suc) {
527  $ret .= $suc . "<br />";
528  }
529  }
530  return $ret;
531  }
532  }
533 
539  public function assign($tag, $value = null)
540  {
541  if (is_array($tag)) {
542  foreach ($tag as $k => $v) {
543  $this->assign($k, $v);
544  }
545  } else {
546  if (!empty($tag) && isset($value)) {
547  $tag = strtoupper(trim($tag));
548  // RMV-NOTIFY
549  // TEMPORARY FIXME: until the X_tags are all in here
550  // if ( substr($tag, 0, 2) != "X_" ) {
551  $this->assignedTags[$tag] = $value;
552  // }
553  }
554  }
555  }
556 
561  public function addHeaders($value)
562  {
563  $this->headers[] = trim($value) . $this->LE;
564  }
565 
570  public function setToEmails($email)
571  {
572  if (!is_array($email)) {
574  if ($xoops->checkEmail($email)) {
575  array_push($this->toEmails, $email);
576  }
577  } else {
578  foreach ($email as $e) {
579  $this->setToEmails($e);
580  }
581  }
582  }
583 
588  public function setToUsers($users)
589  {
590  if (is_a($users,'XoopsUser')) {
591  array_push($this->toUsers, $users);
592  } elseif (is_array($users)) {
593  foreach ($users as $u) {
594  $this->setToUsers($u);
595  }
596  }
597  }
598 
603  public function setToGroups($groups)
604  {
605  if (is_a($groups,'XoopsGroup')) {
607  ->getHandlerMember()
608  ->getUsersByGroup($groups->getVar('groupid'), true));
609 
610  } elseif (is_array($groups)) {
611  foreach ($groups as $g) {
612  $this->setToGroups($g);
613  }
614  }
615  }
616 
623  public function encodeFromName($text)
624  {
625  return $text;
626  }
627 
634  public function encodeSubject($text)
635  {
636  return $text;
637  }
638 
645  public function encodeBody(&$text)
646  {
647  }
648 }
getSuccess($ashtml=true)
$path
Definition: execute.php:31
if(empty($settings['ROOT_PATH'])) elseif(empty($settings['DB_PARAMETERS'])) $error
send($debug=false)
sendMail($email, $subject, $body, $headers)
const ERRORS
Definition: en_US.php:302
setTemplateDir($value=null)
setTemplate($value)
setFromUser(XoopsUser $user)
encodeSubject($text)
const SF_EMAIL_SENT_TO
Definition: en_US.php:983
setSubject($value)
static getInstance()
Definition: Xoops.php:160
setFromEmail($value)
$user
Definition: checklogin.php:47
$text
Definition: qrrender.php:27
encodeFromName($text)
setBody($value)
addHeaders($value)
setHTML($value=true)
setToUsers($users)
setPriority($value)
defined('DS') or define('DS' DIRECTORY_SEPARATOR
Definition: common.php:41
const EF_EMAIL_NOT_SENT_TO
Definition: en_US.php:236
setFromName($value)
$header
$xoops
Definition: admin.php:25
assign($tag, $value=null)
setToEmails($email)
encodeBody(&$text)
static fileExists($file)
Definition: xoopsload.php:506
const EF_PRIVATE_MESSAGE_NOT_SENT_TO
Definition: en_US.php:267
const E_TEMPLATE_FILE_NOT_OPENED
Definition: en_US.php:369
static get($name)
if(!$xoops->isUser()) $uid
Definition: index.php:31
$groups
sendPM($uid, $subject, $body)
const E_MESSAGE_BODY_NOT_SET
Definition: en_US.php:345
$pm_handler
Definition: readpmsg.php:37
const SF_PRIVATE_MESSAGE_SENT_TO
Definition: en_US.php:992
setToGroups($groups)
$email
Definition: lostpass.php:32
getErrors($ashtml=true)