PageRenderTime 82ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/CORE.OLD/Email.php

https://github.com/silasrm/MyZendAssets
PHP | 431 lines | 196 code | 53 blank | 182 comment | 28 complexity | fb2a2875aec40b89b11922608bab9d13 MD5 | raw file
  1. <?php
  2. /**
  3. * Class for sent mail with Zend_Mail
  4. *
  5. * @author Silas Ribas <silasrm@gmail.com> github.com/silasrm
  6. * @version 0.2.2
  7. * @package CORE
  8. * @name Email
  9. * @example BOOTSTRAP
  10. $log = null;
  11. if( Zend_Registry::get('config')->core->email->log )
  12. {
  13. $writer = new Zend_Log_Writer_Stream( Zend_Registry::get('config')->core->email->logPath );
  14. $log = new Zend_Log ( $writer );
  15. }
  16. CORE_Email::getInstance()->config( array( 'viewPath' =>Zend_Registry::get('config')->core->email->viewPath
  17. , 'alertTo' =>Zend_Registry::get('config')->core->email->alertTo
  18. , 'transport' => $transport
  19. , 'log' => $log ) );
  20. *
  21. * @example MULTIPLES RECIPIENTS AND WITH TEMPLATE
  22. CORE_Email::getInstance()->setTitle( 'Ola {{nome}}', true, array('nome' => 'Silas Ribas' ) )
  23. ->setData( array( 'xya' => 'LUPA UMPA!' ) )
  24. ->setTemplate( 'teste.phtml' )
  25. ->isHtml() // indicated this message is a html type
  26. ->addTo('mariadasilva999999@email.com.br')
  27. ->addTo('mariadasilva9999992@email.com.br')
  28. ->send();
  29. *
  30. * @example USING TEMPLATE BUT IS NOT A HTML MESSAGE, NOT PASSED A PATH FILE OF THE TEMPLATE CODE
  31. CORE_Email::getInstance()->setTitle( 'Ola {{nome}}', true, array('nome' => 'Silas Ribas' ) )
  32. ->setData( array( 'xya' => 'LUPA UMPA!' ) )
  33. ->setTemplate( '<h2>{{nome}}</h2>', true )
  34. ->isHtml() // indicated this message is a html type
  35. ->send( 'mariadasilva9999992@email.com.br' );
  36. *
  37. * @example USING HTML CODE IN TEMPLATE, NOT PASSED A PATH FILE OF THE TEMPLATE CODE
  38. CORE_Email::getInstance()->setTitle( 'Ola {{nome}}', true, array('nome' => 'Silas Ribas' ) )
  39. ->setData( array( 'xya' => 'LUPA UMPA!' ) )
  40. ->setTemplate( 'Hi {{nome}}.', true )
  41. ->send( 'mariadasilva9999992@email.com.br' );
  42. *
  43. * @example SINGLE RECIPIENT AND WITH TEXT MESSAGE ( NOT HTML MESSAGE )
  44. CORE_Email::getInstance()->setTitle( 'Ola {{nome}}', true, array('nome' => 'Silas Ribas' ) )
  45. ->setMessage('Message for body e-mail')
  46. ->send( 'mariadasilva999999@email.com.br' ); // 1 recipient
  47. */
  48. class CORE_Email
  49. {
  50. /**
  51. * @var CORE_Email
  52. */
  53. protected static $_instance = null;
  54. /**
  55. * @var Zend_Mail
  56. */
  57. protected static $_instanceMail = null;
  58. /**
  59. * @var Zend_View
  60. */
  61. protected $_view = null;
  62. /**
  63. * @var string
  64. */
  65. protected $_alertTo = null;
  66. /**
  67. * @var Zend_Mail_Transport
  68. */
  69. protected $_transport = null;
  70. /**
  71. * @var Zend_Log
  72. */
  73. protected $_log = null;
  74. /**
  75. * @var array
  76. */
  77. protected $_data = null;
  78. /**
  79. * @var string
  80. */
  81. protected $_template = null;
  82. /**
  83. * @var string
  84. */
  85. protected $_message = null;
  86. /**
  87. * @var string
  88. */
  89. protected $_title = null;
  90. /**
  91. * If template value is not a file path and as a html code.
  92. * @var booleand
  93. */
  94. protected $_isCode = false;
  95. /**
  96. * @var int 1 = text, 2 = html
  97. */
  98. protected $_messageType = 1;
  99. public function __construct(){}
  100. public function __clone(){}
  101. /**
  102. * Return a instance of this class
  103. * @return CORE_Email
  104. */
  105. public static function getInstance()
  106. {
  107. if( null === self::$_instance )
  108. {
  109. self::$_instance = new self;
  110. }
  111. if( null === self::$_instanceMail )
  112. {
  113. self::$_instanceMail = new Zend_Mail;
  114. }
  115. return self::$_instance;
  116. }
  117. /**
  118. * Return a Zend_Mail instance
  119. *
  120. * @return Zend_Mail
  121. */
  122. public function getMail()
  123. {
  124. return self::$_instanceMail;
  125. }
  126. /**
  127. * Set a configuration of the class
  128. *
  129. * @param array $config
  130. * @return CORE_Email
  131. */
  132. public function config( array $config )
  133. {
  134. if( !is_array( $config ) )
  135. throw new InvalidArgumentException( 'Configuration parameters is not a valid array.' );
  136. if( !array_key_exists( 'viewPath', $config ) )
  137. throw new InvalidArgumentException( 'Configuration parameter "View Path" not exists.' );
  138. if( empty($config['viewPath']) )
  139. throw new InvalidArgumentException( '"View Path" is not set.' );
  140. if( !is_dir($config['viewPath']) )
  141. throw new InvalidArgumentException( '"View Path" is not set exists.' );
  142. if( !array_key_exists( 'transport', $config ) )
  143. throw new InvalidArgumentException( 'Configuration parameter "Transport" not exists.' );
  144. if( empty($config['transport']) )
  145. throw new InvalidArgumentException( '"Transport" is not set.' );
  146. $this->_view = new Zend_View();
  147. $this->_view->setScriptPath( $config['viewPath'] );
  148. $this->_transport = $config['transport'];
  149. /**
  150. * For alert want a exception occurred
  151. */
  152. if( array_key_exists( 'alertTo', $config )
  153. && !empty($config['alertTo']) )
  154. $this->_alertTo = $config['alertTo'];
  155. /**
  156. * For log want a exception occurred
  157. */
  158. if( array_key_exists( 'log', $config )
  159. && !empty($config['log']) )
  160. $this->_log = $config['log'];
  161. return self::$_instance;
  162. }
  163. /**
  164. * Set data used in template
  165. * @param array $data
  166. * @return CORE_Email
  167. */
  168. public function setData( array $data )
  169. {
  170. $this->_data = $data;
  171. return self::$_instance;
  172. }
  173. /**
  174. * Set a template used in build a message
  175. * @param string $template
  176. * @param boolean $isCode
  177. * @return CORE_Email
  178. */
  179. public function setTemplate( $template, $isCode = false )
  180. {
  181. if( !is_string( $template ))
  182. throw new InvalidArgumentException( 'Template is not a string/path.' );
  183. $this->_template = $template;
  184. $this->_isCode = $isCode;
  185. return self::$_instance;
  186. }
  187. /**
  188. * Build a message
  189. *
  190. * @return CORE_Email
  191. */
  192. public function buildDataInTemplate()
  193. {
  194. if( $this->_isCode )
  195. $codeTemplate = $this->_template;
  196. else
  197. $codeTemplate = $this->_view->partial( $this->_template );
  198. $this->_message = $this->stringf( $codeTemplate, $this->_data );
  199. return self::$_instance;
  200. }
  201. /**
  202. * Indicated a message sent is a HTML
  203. * @param boolean $true
  204. */
  205. public function isHtml( $true = true )
  206. {
  207. $this->_messageType = 2;
  208. return self::$_instance;
  209. }
  210. /**
  211. * Change mark tags on template to value string
  212. * @url https://gist.github.com/822397 idea by @alganet
  213. *
  214. * @param string $template
  215. * @param array $vars
  216. * @return string
  217. */
  218. public function stringf( $template, array $vars = array() )
  219. {
  220. if( substr( PHP_VERSION, 0, 3 ) >= 5.3 )
  221. return preg_replace_callback( '/{{(\w+)}}/'
  222. , function( $match ) use( &$vars ) {
  223. return $vars[ $match[ 1 ] ];
  224. }
  225. , $template );
  226. else
  227. return preg_replace_callback( '/{{(\w+)}}/'
  228. , create_function( '$match', 'return &$vars[$match[1]];' )
  229. , $template );
  230. }
  231. /**
  232. * Return a message in text or html
  233. *
  234. * @return string
  235. */
  236. public function getMessage()
  237. {
  238. if( !empty( $this->_template ) )
  239. $this->buildDataInTemplate();
  240. return $this->_message;
  241. }
  242. /**
  243. * Set a message text for sent a pure text email
  244. *
  245. * @param string $message
  246. * @return CORE_Email
  247. */
  248. public function setMessage( $message )
  249. {
  250. if( !is_string( $message ))
  251. throw new InvalidArgumentException( 'Message is not a string.' );
  252. $this->_messageType = 1;
  253. $this->_message = $message;
  254. return self::$_instance;
  255. }
  256. /**
  257. * Set a Title/Subject of the email
  258. *
  259. * @param string $title
  260. * @param boolean $hasVariables
  261. * @param null|array $data
  262. * @return CORE_Email
  263. */
  264. public function setTitle( $title, $hasVariables = false, $data = null )
  265. {
  266. if( !is_string( $title ))
  267. throw new InvalidArgumentException( 'Title is not a string.' );
  268. // clear subject in Zend_Mail
  269. $this->getMail()->clearSubject();
  270. $this->_title = $title;
  271. if( $hasVariables )
  272. $this->_title = $this->stringf( $this->_title, $data );
  273. return self::$_instance;
  274. }
  275. /**
  276. * Set a name/email in TO mail field
  277. *
  278. * @param string $email
  279. * @param string $name
  280. * @return CORE_Email
  281. */
  282. public function addTo( $email, $name='' )
  283. {
  284. $this->getMail()->addTo( $email, $name );
  285. return self::$_instance;
  286. }
  287. /**
  288. * Set a name/email in CC mail field
  289. *
  290. * @param string $email
  291. * @param string $name
  292. * @return CORE_Email
  293. */
  294. public function addCc( $email, $name='' )
  295. {
  296. $this->getMail()->addCc( $email, $name );
  297. return self::$_instance;
  298. }
  299. /**
  300. * Set a email in BCC mail field
  301. *
  302. * @param string $email
  303. * @param string $name
  304. * @return CORE_Email
  305. */
  306. public function addBcc($email )
  307. {
  308. $this->getMail()->addBcc( $email );
  309. return self::$_instance;
  310. }
  311. /**
  312. * Sent a email
  313. *
  314. * @param string|array $recipient
  315. * @return boolena|Exception
  316. */
  317. public function send( $recipient = null )
  318. {
  319. if( !is_null( $recipient ) )
  320. {
  321. if( is_array( $recipient ) )
  322. {
  323. if( !empty($recipient['nome']) && !empty($recipient['email']) )
  324. $this->getMail()->addTo( $recipient['email'], $recipient['nome']);
  325. else
  326. throw new InvalidArgumentException( 'Recipient is invalid' );
  327. }
  328. else
  329. {
  330. if( !empty($recipient) )
  331. $this->getMail()->addTo( $recipient );
  332. else
  333. throw new InvalidArgumentException( 'Recipient is invalid' );
  334. }
  335. }
  336. $this->getMail()->setSubject( utf8_decode( $this->_title ) );
  337. if( $this->_messageType == 1 )
  338. {
  339. $this->getMail()->setBodyText( $this->getMessage()
  340. , 'UTF-8'
  341. , 'UTF-8' );
  342. }
  343. else
  344. {
  345. $this->getMail()->setBodyHtml( $this->getMessage()
  346. , 'UTF-8'
  347. , 'UTF-8' );
  348. }
  349. try
  350. {
  351. $this->getMail()->send( $this->_transport );
  352. return true;
  353. }
  354. catch( Exception $e )
  355. {
  356. // If alertTo is informed, sent email with alert and exception trace
  357. if( !is_null( $this->_alertTo ) )
  358. {
  359. $mail = CORE_Email::getInstance()->getMail();
  360. $mail->clearRecipients()
  361. ->addTo( $this->_alertTo )
  362. ->clearSubject()
  363. ->setSubject( '[Alert] Email not sent in ' . __FILE__ )
  364. ->setBodyHtml( 'Exception occurred in sent e-mail:<br /><br />' . $e->__toString(), 'UTF-8', 'UTF-8' )
  365. ->setBodyText( "Exception occurred in sent e-mail:\n\n" . $e->__toString(), 'UTF-8', 'UTF-8' )
  366. ->send( $this->_transport );
  367. }
  368. // If log is informed, log a exception trace
  369. if( !is_null( $this->_log ) )
  370. {
  371. $this->_log->log( $e->__toString(), Zend_Log::CRIT );
  372. }
  373. throw new Exception( $e->getMessage() );
  374. }
  375. }
  376. }
  377. ?>