XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
mailer.php
Go to the documentation of this file.
1 <?php
2 // $Id: mailer.php 1019 2012-09-01 06:26:52Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 include_once RMCPATH.'/class/swift/swift_required.php';
12 
21 class RMMailer
22 {
26  private $swTransport;
30  private $swMessage;
34  private $swMailer;
38  private $errors = array();
39 
40  private $vars = array();
41  private $template = '';
42  private $tpl_type = '';
43 
44  private $xusers = array();
45 
46 
55  public function __construct($content_type = 'text/plain'){
56 
57  $config = RMFunctions::configs();
58  $config_handler =& xoops_gethandler('config');
59  $xconfig = $config_handler->getConfigsByCat(XOOPS_CONF_MAILER);
60 
61  // Instantiate the Swit Transport according to our preferences
62  // We can change this preferences later
63  switch($config['transport']){
64  case 'mail':
65  $this->swTransport = Swift_MailTransport::newInstance();
66  break;
67  case 'smtp':
68  $this->swTransport = Swift_SmtpTransport::newInstance($config['smtp_server'], $config['smtp_port'], $config['smtp_crypt']!='none' ? $config['smtp_crypt'] : '');
69  $this->swTransport->setUsername($config['smtp_user']);
70  $this->swTransport->setPassword($config['smtp_pass']);
71  break;
72  case 'sendmail':
73  $this->swTransport = Swift_SendmailTransport::newInstance($config['sendmail_path']);
74  break;
75  }
76 
77  // Create the message object
78  // Also this object could be change later with message() method
79  $this->swMessage = Swift_Message::newInstance();
80  $this->swMessage->setReplyTo($xconfig['from']);
81  $this->swMessage->setFrom(array($xconfig['from'] => $xconfig['fromname']));
82  $this->swMessage->setContentType($content_type);
83 
84  }
85 
86  public function set_from($mail, $name){
87  $this->swMessage->setFrom($mail, $name);
88  }
89 
90  public function set_from_xuser($user){
91  if (strtolower(get_class($user))=='xoopsuser')
92  $this->fromuser = $user;
93  elseif($user>0)
94  $this->fromuser = new XoopsUser($user);
95  }
96 
100  public function &transport(){
101  return $this->swTransport;
102  }
103 
114  public function &message(){
115  return $this->swMessage;
116  }
117 
123  public function get_subject(){
124  return $this->swMessage->getSubject();
125  }
126 
132  public function set_subject($subject){
133  $this->swMessage->setSubject($subject);
134  }
135 
140  public function get_body(){
141  return $this->swMessage->getBody();
142  }
143 
151  public function set_body($body, $content_type=''){
152  $this->swMessage->setBody($body, $content_type!='' ? $content_type : $this->swMessage->getContentType());
153  }
154 
161  public function add_part($content, $type='text/html'){
162  $this->swMessage->addPart($content, $type);
163  }
164 
176  public function attach_content($type='path', $path = '', $content_type = '', $name='', $content=null, $disposition=''){
177 
178  switch($type){
179  case 'path':
180 
181  if (trim($path)=='' || !is_file($path)) return;
183 
184  break;
185  case 'dynamic':
186 
187  if ($content==null || $content=='') return;
189 
190  break;
191  }
192 
193  if (trim($name)!='') $att->setFilename($name);
194  if (trim($content_type)!='') $att->setContentType($content_type);
195  if (trim($disposition)!='') $att->setDisposition($disposition);
196 
197  $this->swMessage->attach($att);
198 
199  }
200 
211  public function embed($type, $file='', $name=null, $content_type=null){
212 
213  switch($type){
214  case 'path':
215  if ($file=='') return;
216  return $this->swMessage->embed(Swift_Image::fromPath($file));
217  break;
218  case 'dynamic':
219  if ($file==null || $file=='') return;
220  return $this->swMessage->embed(Swift_Image::newInstance($file, $name, $content_type));
221  }
222 
223  }
224 
230  public function set_to($recipients = array()){
231  if (empty($recipients)) return;
232  return $this->swMessage->setTo($recipients);
233  }
234 
239  public function get_to(){
240  return $this->swMessage->getTo();
241  }
242 
248  public function add_to($mail, $name=''){
249  if ($mail=='') return;
250  return $this->swMessage->addTo($mail, $name);
251  }
252 
257  public function set_cc($recipients = array()){
258  if (empty($recipients)) return;
259  return $this->swMessage->setCc($recipients);
260  }
261 
266  public function get_cc(){
267  return $this->swMessage->getCc();
268  }
269 
275  public function add_cc($mail, $name=''){
276  if ($mail=='') return;
277 
278  return $this->swMessage->addCc($mail, $name);
279  }
280 
281  public function set_bcc($recipients = array()){
282  if (empty($recipients)) return;
283  return $this->swMessage->seBcc($recipients);
284  }
285 
286  public function get_bcc(){
287  return $this->swMessage->getBcc();
288  }
289 
290  public function add_bcc($mail, $name=''){
291  if ($mail=='') return;
292 
293  return $this->swMessage->addBcc($mail, $name);
294  }
295 
302  public function add_users($users, $field='to'){
303  if (!is_array($users)) return;
304 
305  foreach ($users as $user){
306  if (is_a($user, "XoopsUser")){
307  $this->add_user($user->getVar('email'), $user->getVar('name')!='' ? $user->getVar('name') : $user->getVar('uname'), $field);
308  } else {
309  $user = new XoopsUser($user);
310  if ($user->isNew()) continue;
311  $this->add_user($user->getVar('email'), $user->getVar('name')!='' ? $user->getVar('name') : $user->getVar('uname'), $field);
312  }
313  $this->xusers[] = $user;
314  }
315 
316  }
317 
318  public function add_user($mail, $name='', $field='to'){
319  switch($field){
320  case 'to':
321  $this->add_to($mail, $name);
322  break;
323  case 'cc':
324  $this->add_cc($mail, $name);
325  break;
326  case 'bcc':
327  $this->add_bcc($mail, $name);
328  break;
329  }
330  }
331 
337  public function add_xoops_users($users, $field='to'){
338 
339  if (is_array($users)){
340 
341  foreach($users as $uid){
342  $user = new RMUser($uid);
343  if (strtolower(get_class($user))=='rmuser'){
344  $this->xusers[] = $user;
345  $this->add_user($user->getVar('email'), $user->getVar('name')!='' ? $user->getVar('name') : $user->getVar('uname'), $field);
346  }
347  }
348 
349  } else {
350  $user = new RMUser($users);
351 
352  if (strtolower(get_class($users))=='xoopsuser'){
353  $this->xusers[] = $user;
354  $this->add_user($users->getVar('email'), $users->getVar('name')!='' ? $users->getVar('name') : $users->getVar('uname'), $field);
355  }
356 
357  }
358 
359  }
360 
365  public function set_return_path($mail){
366  if ($mail=='') return;
367  $this->swMessage->setReturnPath($mail);
368  }
369  public function get_return_path(){
370  return $this->swMessage->getReturnPath();
371  }
372 
373  public function assign($var, $value){
374  $this->vars[$var] = $value;
375  }
376 
383  public function template($file, $type=''){
384  if (file_exists($file))
385  $this->template = $file;
386  }
387 
392  public function errors(){
393  return $this->errors;
394  }
395 
396  public function create_body(){
397 
398  if ($this->get_body()!='') return;
399 
400  if($this->template=='') return;
401 
402  if ($this->tpl_type=='old'){
403  global $xoopsTpl;
404 
405  $ret = file_get_contents($this->template);
406 
407  foreach($this->vars as $name => $value){
408  $ret = str_replace('{'.$name.'}', $value, $ret);
409  }
410 
411  $this->set_body($ret);
412  return;
413 
414  }
415 
416  extract($this->vars);
417  ob_start();
418 
419  include $this->template;
420 
421  $ret = ob_get_clean();
422 
423  $this->set_body($ret);
424 
425  }
426 
427  function send_pm(){
428 
429  if(empty($this->xusers))
430  return false;
431 
432  if (!$this->fromuser)
433  return false;
434 
435  $this->create_body();
436 
437  $pm_handler = &xoops_gethandler('privmessage');
438  $pm = &$pm_handler->create();
439  $pm->setVar("subject", $this->get_subject());
440  // RMV-NOTIFY
441  $pm->setVar('from_userid', $this->fromuser->uid());
442  $pm->setVar("msg_text", $this->get_body());
443 
444  foreach($this->xusers as $user){
445  $pm->setVar("to_userid", $user->uid());
446  $pm_handler->insert($pm);
447  }
448 
449  }
450 
451  function batchSend(){
452  $this->create_body();
453  $this->swMailer = Swift_Mailer::newInstance($this->swTransport);
454  return $this->swMailer->batchSend($this->swMessage, $this->errors);
455  }
456 
457  function send(){
458  $this->create_body();
459  $this->swMailer = Swift_Mailer::newInstance($this->swTransport);
460  return $this->swMailer->send($this->swMessage, $this->errors);
461  }
462 
463 }