PageRenderTime 71ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/kohana/mailer.php

https://github.com/jmhobbs/Mailer
PHP | 467 lines | 231 code | 57 blank | 179 comment | 34 complexity | a26c0a8c34f6f04a52972bbbcd9efe08 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * System for sending email using Swift Mailer integrates into any web app written
  4. * in PHP 5, offering a flexible and elegant object-oriented approach to sending
  5. * emails with a multitude of features.
  6. *
  7. * @package Kohana/Mailer
  8. * @author Eduardo Pacheco
  9. * @copyright (c) 2007-2009 Kohana Team
  10. * @fork http://github.com/themusicman/Mailer
  11. * @license http://kohanaphp.com/license
  12. */
  13. class Kohana_Mailer {
  14. /**
  15. * Swift_Mailer
  16. * @var object
  17. */
  18. protected $_mailer = null;
  19. /**
  20. * Mail Type
  21. * @var string
  22. */
  23. protected $type = null;
  24. /**
  25. * Sender mail
  26. * @var string
  27. */
  28. protected $from = null;
  29. /**
  30. * Reply to email
  31. * @var string
  32. */
  33. protected $reply_to = null;
  34. /**
  35. * Receipents mail
  36. * @var string
  37. */
  38. protected $to = null;
  39. /**
  40. * CC
  41. * @var string
  42. */
  43. protected $cc = null;
  44. /**
  45. * BCC
  46. * @var string
  47. */
  48. protected $bcc = null;
  49. /**
  50. * Mail Subject
  51. * @var string
  52. */
  53. protected $subject = null;
  54. /**
  55. * Data binding
  56. * @var array
  57. */
  58. protected $data = null;
  59. /**
  60. * Attachments
  61. * @var array
  62. */
  63. protected $attachments = null;
  64. /**
  65. * Whether in batch send or no
  66. * @var boolean
  67. */
  68. protected $batch_send = false;
  69. /**
  70. * Swift_Message Object
  71. * @var object
  72. */
  73. protected $message = null;
  74. /**
  75. * Mailer Config
  76. * @var object
  77. */
  78. protected $config = "default";
  79. /**
  80. * Mailer DebugMode
  81. * @var bool
  82. */
  83. protected $debug = FALSE;
  84. /**
  85. * Mailer Method
  86. * @var string
  87. */
  88. protected $method = NULL;
  89. /**
  90. * Mailer Instance
  91. * @var object
  92. */
  93. public static $instances = array();
  94. /**
  95. * Mail template
  96. * @var array
  97. */
  98. protected $view = array(
  99. 'html' => null,
  100. 'text' => null,
  101. );
  102. /**
  103. * Automatically executed before the controller action. Can be used to set
  104. * class properties, do authorization checks, and execute other custom code.
  105. *
  106. * @return void
  107. */
  108. public function before()
  109. {
  110. // Nothing by default
  111. }
  112. /**
  113. * Automatically executed after the controller action. Can be used to apply
  114. * transformation to the request response, add extra output, and execute
  115. * other custom code.
  116. *
  117. * @return void
  118. */
  119. public function after()
  120. {
  121. // Nothing by default
  122. }
  123. /**
  124. * Creates a new Mailer object.
  125. *
  126. * @param string configuration
  127. * @return void
  128. */
  129. public function __construct($config = "default")
  130. {
  131. if ( ! class_exists('Swift', FALSE))
  132. {
  133. // Load SwiftMailer Autoloader
  134. require_once Kohana::find_file('vendor', 'swift/swift_required');
  135. };
  136. // Load configuration
  137. $this->before();
  138. }
  139. /**
  140. * Creates a new Mailer object.
  141. *
  142. * @access public
  143. * @param string mailer_name
  144. * @param string method
  145. * @param data array
  146. * @return Mailer
  147. *
  148. **/
  149. public static function factory( $mailer_name = NULL, $method = NULL, $data = array() )
  150. {
  151. $class = ( $mailer_name !== NULL ) ? 'Mailer_'.ucfirst($mailer_name) : 'Mailer';
  152. $class = new $class;
  153. if ( $method === NULL )
  154. {
  155. return $class;
  156. } else {
  157. //see if the method exists
  158. if (method_exists($class, $method))
  159. {
  160. //call the method
  161. call_user_func_array( array( $class, $method ), array( $data ) );
  162. // $class->$method( $data );
  163. //setup the message
  164. $class->setup( $method );
  165. //send the message
  166. return $class->send();
  167. }
  168. else
  169. {
  170. //the method does not exist so throw exception
  171. throw new Exception('Method: '.$method.' does not exist.');
  172. }
  173. };
  174. }
  175. /**
  176. * Singleton pattern
  177. *
  178. * @access public
  179. * @param string mailer_name
  180. * @return Mailer
  181. *
  182. **/
  183. public static function instance( $mailer_name = NULL, $config = "default" )
  184. {
  185. $className = ( $mailer_name !== NULL) ? 'Mailer_'.ucfirst($mailer_name) : "Mailer";
  186. if ( ! isset( self::$instances[$className] ) )
  187. {
  188. self::$instances[$className] = new $className;
  189. };
  190. return self::$instances[$className]->config($config);
  191. }
  192. /**
  193. * Connect to server
  194. *
  195. * @access public
  196. * @param string config
  197. * @return object Swift_Mailer
  198. *
  199. **/
  200. public function connect( $config = "default" )
  201. {
  202. // Load configuration
  203. $config = Kohana::$config->load('mailer.'.$config);
  204. $transport = ( is_null( $config['transport'] ) ) ? 'native' : $config['transport'];
  205. $config = $config['options'];
  206. $klass = 'Mailer_Transport_' . ucfirst($transport);
  207. $factory = new $klass();
  208. //Create the Mailer
  209. return $this->_mailer = Swift_Mailer::newInstance($factory->build($config));
  210. }
  211. /**
  212. * Use the call object to configure the sending
  213. *
  214. * @access public
  215. * @param void
  216. * @return void
  217. *
  218. **/
  219. public function __call($name, $args = array())
  220. {
  221. $pattern = '/^(type|from|to|cc|bcc|subject|data|attachments|batch_send|config|html|text|debug)$/i';
  222. if ( isset($args[0]) && is_array( $args[0] ) )
  223. {
  224. foreach ($args[0] as $key => $value)
  225. {
  226. if (preg_match($pattern, $key))
  227. {
  228. $this->$key = $value;
  229. };
  230. };
  231. };
  232. if ( preg_match($pattern, $name) )
  233. {
  234. $this->$name = $args[0];
  235. return $this;
  236. };
  237. if (preg_match('/^sen(d|t)_/i', $name))
  238. {
  239. $method = substr($name, 5, strlen($name));
  240. //see if the method exists
  241. if (method_exists($this, $method))
  242. {
  243. //call the method
  244. call_user_func_array(array($this, $method), $args);
  245. //setup the message
  246. $this->setup($method);
  247. //send the message
  248. return $this->send();
  249. }
  250. else
  251. {
  252. //the method does not exist so throw exception
  253. throw new Exception('Method: '.$method.' does not exist.');
  254. };
  255. }
  256. }
  257. /**
  258. * Setup the message
  259. *
  260. * @access public
  261. * @param string method
  262. * @return object
  263. *
  264. **/
  265. public function setup($method = NULL)
  266. {
  267. $this->message = Swift_Message::newInstance();
  268. $this->message->setSubject($this->subject);
  269. $is_html = isset( $this->html );
  270. $is_text = isset( $this->text );
  271. if ( $is_html || $is_text )
  272. {
  273. if ( $is_html )
  274. {
  275. $this->view["html"] = $this->html;
  276. $this->message->setBody($this->view['html'], 'text/html');
  277. };
  278. if ( $is_text )
  279. {
  280. $this->view["text"] = $this->text;
  281. if ( $is_html )
  282. {
  283. $this->message->addPart($this->view['text'], 'text/plain');
  284. }
  285. else
  286. {
  287. $this->message->setBody($this->view['text'], 'text/plain');
  288. };
  289. };
  290. }
  291. else
  292. {
  293. // View
  294. $template = strtolower(preg_replace('/_/', '/', get_class($this)) . "/{$method}");
  295. $text = View::factory($template);
  296. $this->set_data($text);
  297. $this->view['text'] = $text->render();
  298. if ($this->type === 'html')
  299. {
  300. $template = View::factory( $template . "_text" );
  301. $this->set_data( $template );
  302. $this->view['html'] = $this->view['text'];
  303. $this->view['text'] = $template->render();
  304. $this->message->setBody($this->view['html'], 'text/html');
  305. $this->message->addPart($this->view['text'], 'text/plain');
  306. } else {
  307. $this->message->setBody($this->view['text'], 'text/plain');
  308. }
  309. };
  310. if ($this->attachments !== null)
  311. {
  312. if (! is_array($this->attachments))
  313. {
  314. $this->attachments = array($this->attachments);
  315. }
  316. foreach ($this->attachments as $file)
  317. {
  318. $this->message->attach(Swift_Attachment::fromPath($file));
  319. }
  320. }
  321. // to
  322. if (! is_array($this->to))
  323. {
  324. $this->to = array($this->to);
  325. }
  326. $this->message->setTo($this->to);
  327. // cc
  328. if ($this->cc !== null)
  329. {
  330. if (! is_array($this->cc))
  331. {
  332. $this->cc = array($this->cc);
  333. }
  334. $this->message->setCc($this->cc);
  335. }
  336. // bcc
  337. if ($this->bcc !== null)
  338. {
  339. if (! is_array($this->bcc))
  340. {
  341. $this->bcc = array($this->bcc);
  342. }
  343. $this->message->setBcc($this->bcc);
  344. }
  345. // from
  346. $this->message->setFrom($this->from);
  347. // reply to
  348. if( $this->reply_to !== null ) {
  349. $this->message->setReplyTo( $this->reply_to );
  350. }
  351. return $this;
  352. }
  353. /**
  354. * Send the mail
  355. *
  356. * @access public
  357. * @param void
  358. * @return bool Mailer_result
  359. *
  360. **/
  361. public function send()
  362. {
  363. if ( $this->message === NULL )
  364. {
  365. $this->setup();
  366. };
  367. $this->connect( $this->config );
  368. try {
  369. //should we batch send or not?
  370. if ( ! $this->batch_send)
  371. {
  372. //Send the message
  373. $this->result = $this->_mailer->send($this->message);
  374. }
  375. else
  376. {
  377. $this->result = $this->_mailer->batchSend($this->message);
  378. }
  379. } catch (Exception $e) {
  380. if ( Kohana::$environment != Kohana::PRODUCTION ) {
  381. throw new Kohana_Exception( $e->getMessage() );
  382. } else {
  383. return false;
  384. };
  385. };
  386. $this->after();
  387. return $this->result;
  388. }
  389. /**
  390. * Set data into the View
  391. *
  392. * @param &View
  393. * @return &View
  394. */
  395. protected function set_data(& $view)
  396. {
  397. if ($this->data != null)
  398. {
  399. if (! is_array($this->data))
  400. {
  401. $this->data = array($this->data);
  402. };
  403. foreach ($this->data as $key => $value)
  404. {
  405. $view->bind($key, $this->data[$key]);
  406. };
  407. };
  408. return $view;
  409. }
  410. }// end of Mailer