Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
EMailer.php
1 <?php
2 /**
3  * EMailer class file.
4  *
5  * @author MetaYii
6  * @version 2.2
7  * @link http://www.yiiframework.com/
8  * @copyright Copyright &copy; 2009 MetaYii
9  *
10  * Copyright (C) 2009 MetaYii.
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation, either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  *
25  * For third party licenses and copyrights, please see phpmailer/LICENSE
26  *
27  */
28 
29 /**
30  * Include the the PHPMailer class.
31  */
32 require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'phpmailer'.DIRECTORY_SEPARATOR.'class.phpmailer.php');
33 
34 /**
35  * EMailer is a simple wrapper for the PHPMailer library.
36  * @see http://phpmailer.codeworxtech.com/index.php?pg=phpmailer
37  *
38  * @author MetaYii
39  * @package application.extensions.emailer
40  * @since 1.0
41  */
42 class EMailer
43 {
44  //***************************************************************************
45  // Configuration
46  //***************************************************************************
47 
48  /**
49  * The path to the directory where the view for getView is stored. Must not
50  * have ending dot.
51  *
52  * @var string
53  */
54  protected $pathViews = 'application.views.email';
55 
56  /**
57  * The path to the directory where the layout for getView is stored. Must
58  * not have ending dot.
59  *
60  * @var string
61  */
62  protected $pathLayouts = 'application.views.email.layouts';
63 
64  //***************************************************************************
65  // Private properties
66  //***************************************************************************
67 
68  /**
69  * The internal PHPMailer object.
70  *
71  * @var object PHPMailer
72  */
73  private $_myMailer;
74 
75  //***************************************************************************
76  // Initialization
77  //***************************************************************************
78 
79  /**
80  * Init method for the application component mode.
81  */
82  public function init() {}
83 
84  /**
85  * Constructor. Here the instance of PHPMailer is created.
86  */
87  public function __construct()
88  {
89  $this->_myMailer = new PHPMailer(true);
90  }
91 
92  //***************************************************************************
93  // Setters and getters
94  //***************************************************************************
95 
96  /**
97  * Setter
98  *
99  * @param string $value pathLayouts
100  */
101  public function setPathLayouts($value)
102  {
103  if (!is_string($value) && !preg_match("/[a-z0-9\.]/i"))
104  throw new CException(Yii::t('EMailer', 'pathLayouts must be a Yii alias path'));
105  $this->pathLayouts = $value;
106  }
107 
108  /**
109  * Getter
110  *
111  * @return string pathLayouts
112  */
113  public function getPathLayouts()
114  {
115  return $this->pathLayouts;
116  }
117 
118  /**
119  * Setter
120  *
121  * @param string $value pathViews
122  */
123  public function setPathViews($value)
124  {
125  if (!is_string($value) && !preg_match("/[a-z0-9\.]/i"))
126  throw new CException(Yii::t('EMailer', 'pathViews must be a Yii alias path'));
127  $this->pathViews = $value;
128  }
129 
130  /**
131  * Getter
132  *
133  * @return string pathViews
134  */
135  public function getPathViews()
136  {
137  return $this->pathViews;
138  }
139 
140  //***************************************************************************
141  // Magic
142  //***************************************************************************
143 
144  /**
145  * Call a PHPMailer function
146  *
147  * @param string $method the method to call
148  * @param array $params the parameters
149  * @return mixed
150  */
151  public function __call($method, $params)
152  {
153  if (is_object($this->_myMailer) && get_class($this->_myMailer)==='PHPMailer') return call_user_func_array(array($this->_myMailer, $method), $params);
154  else throw new CException(Yii::t('EMailer', 'Can not call a method of a non existent object'));
155  }
156 
157  /**
158  * Setter
159  *
160  * @param string $name the property name
161  * @param string $value the property value
162  */
163  public function __set($name, $value)
164  {
165  if (is_object($this->_myMailer) && get_class($this->_myMailer)==='PHPMailer') $this->_myMailer->$name = $value;
166  else throw new CException(Yii::t('EMailer', 'Can not set a property of a non existent object'));
167  }
168 
169  /**
170  * Getter
171  *
172  * @param string $name
173  * @return mixed
174  */
175  public function __get($name)
176  {
177  if (is_object($this->_myMailer) && get_class($this->_myMailer)==='PHPMailer') return $this->_myMailer->$name;
178  else throw new CException(Yii::t('EMailer', 'Can not access a property of a non existent object'));
179  }
180 
181  /**
182  * Cleanup work before serializing.
183  * This is a PHP defined magic method.
184  * @return array the names of instance-variables to serialize.
185  */
186  public function __sleep()
187  {
188  }
189 
190  /**
191  * This method will be automatically called when unserialization happens.
192  * This is a PHP defined magic method.
193  */
194  public function __wakeup()
195  {
196  }
197 
198  //***************************************************************************
199  // Utilities
200  //***************************************************************************
201 
202  /**
203  * Displays an e-mail in preview mode.
204  *
205  * @param string $view the class
206  * @param array $vars
207  * @param string $layout
208  */
209  public function getView($view, $vars = array(), $layout = null)
210  {
211  $body = Yii::app()->controller->renderPartial($this->pathViews.'.'.$view, array_merge($vars, array('content'=>$this->_myMailer)), true);
212  if ($layout === null) {
213  $this->_myMailer->Body = $body;
214  }
215  else {
216  $this->_myMailer->Body = Yii::app()->controller->renderPartial($this->pathLayouts.'.'.$layout, array('content'=>$body), true);
217  }
218  }
219 }