PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/atk4/lib/TMail/Basic.php

https://github.com/mahimarathore/mahi
PHP | 260 lines | 207 code | 26 blank | 27 comment | 17 complexity | 423c9783c422a764430bdf5c2773e929 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /*
  3. * Undocumented
  4. *
  5. * @link http://agiletoolkit.org/
  6. *//*
  7. ==ATK4===================================================
  8. This file is part of Agile Toolkit 4
  9. http://agiletoolkit.org/
  10. (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
  11. Distributed under Affero General Public License v3 and
  12. commercial license.
  13. See LICENSE or LICENSE_COM for more information
  14. =====================================================ATK4=*/
  15. class TMail_Basic extends AbstractModel {
  16. public $mail_template=null;
  17. public $template_class='TMail_Template';
  18. public $master_template='shared';
  19. public $boundary;
  20. public $args=array();
  21. public $version='2.0';
  22. function init(){
  23. parent::init();
  24. $master_template = $this->add($this->template_class)->loadTemplate('shared','.mail');
  25. $this->template=$master_template->cloneRegion('body');
  26. $this->headers=$master_template->cloneRegion('headers');
  27. $this->boundary=str_replace('.','',uniqid('atk4tmail',true));
  28. if($t=$this->api->getConfig('tmail/transport',false)){
  29. $this->addTransport($t);
  30. }
  31. }
  32. function extractEmail($fuzzy_email){
  33. preg_match('/^(?:"?([^@"]+)"?\s)?<?([^>]+@[^>]+)>?$/',$fuzzy_email,$m);
  34. return $m;
  35. }
  36. function defaultTemplate(){
  37. return array('shared');
  38. }
  39. function addTransport($t){
  40. return $this->add('TMail_Transport_'.$t);
  41. }
  42. function addPart($p){
  43. return $this->add('TMail_Part_'.$p);
  44. }
  45. /* Setting Content Separatelly */
  46. function setText($text){
  47. $this->addPart('Text')->set($text);
  48. }
  49. function setHTML($html){
  50. $this->addPart('HTML')->set($html);
  51. }
  52. function loadTemplate($template,$junk=null){
  53. return $this->setTemplate($template);
  54. }
  55. function setTemplate($template){
  56. $t=$this->add($this->template_class)->loadTemplate($template,'.mail');
  57. if($t->is_set('subject')){
  58. $s=trim($t->cloneRegion('subject')->render());
  59. $this->set('subject',$s);
  60. $t->del('subject');
  61. }
  62. if($t->is_set('html')){
  63. $this->setText($t->cloneRegion('text'));
  64. $this->setHtml($t->cloneRegion('html'));
  65. }elseif($t->is_set('body')){
  66. $this->set($t->cloneRegion('body'));
  67. }else{
  68. $this->set($t);
  69. }
  70. }
  71. function setTag($arg,$val=null){
  72. return $this->set($arg,$val);
  73. }
  74. function set($arg,$val=null){
  75. if(is_array($arg)){
  76. $this->args=array_merge($this->args,$arg);
  77. }else{
  78. if($val===false){
  79. unset($this->args[$arg]);
  80. }elseif(is_null($val)){
  81. $this->addPart('Both')->set($arg);
  82. }else{
  83. $this->args[$arg]=$val;
  84. }
  85. }
  86. return $this;
  87. }
  88. function get($arg){
  89. return $this->args[$arg];
  90. }
  91. function render(){
  92. $this->template->set('body_parts','');
  93. foreach($this->elements as $el){
  94. if($el instanceof TMail_Part){
  95. $this->template->appendHTML('body_parts',$el->render());
  96. }
  97. }
  98. $this->template->set('boundary',$this->boundary);
  99. $this->headers
  100. ->set('boundary',$this->boundary)
  101. ->setHTML($this->args);
  102. }
  103. function send($to,$from=null){
  104. if(is_null($from) && isset($this->args['from']))$from=$this->args['from'];
  105. if(is_null($from))$from=$this->api->getConfig('tmail/from');
  106. if(!isset($this->args['from_formatted']))$this->args['from_formatted']=$from;
  107. if(!isset($this->args['to_formatted']))$this->args['to_formatted']=$to;
  108. $from=$this->extractEmail($from);$from=$from[2];
  109. $to=$this->extractEmail($to);$to=$to[2];
  110. $this->render();
  111. $body = $this->template->render();
  112. $headers = trim($this->headers->render());
  113. $subject = $this->args['subject'];
  114. // TODO: should we use mb_encode_mimeheader ?
  115. if(!($res=$this->hook('send',array($to,$from,$subject,$body,$headers)))){
  116. return mail($to,$subject,$body,$headers,'-f '.$from);
  117. }
  118. return $res;
  119. }
  120. }
  121. class TMail_Part extends AbstractModel {
  122. public $template=null;
  123. public $content;
  124. public $auto_track_element=true;
  125. function init(){
  126. parent::init();
  127. // Initialize template of this part
  128. $t=$this->defaultTemplate();
  129. $this->template=$this->add($this->owner->template_class)
  130. ->loadTemplate($t[0],'.mail');
  131. if($t[1])$this->template=$this->template->cloneRegion($t[1]);
  132. }
  133. function set($content){
  134. $this->content=$content;
  135. }
  136. function render(){
  137. $c=$this->content;
  138. if($c instanceof SMLite){
  139. $c->set($this->owner->args);
  140. $c=$c->render();
  141. }
  142. $this->template->setHTML($this->owner->args);
  143. $this->template->setHTML('Content',$c);
  144. $this->template->set('boundary',$this->owner->boundary);
  145. return $this->template->render();
  146. }
  147. function defaultTemplate(){
  148. return array('shared','body_part');
  149. }
  150. }
  151. class TMail_Part_HTML extends TMail_Part {
  152. }
  153. class TMail_Part_Text extends TMail_Part {
  154. function init(){
  155. parent::init();
  156. $this->template->set('contenttype','text/plain');
  157. }
  158. }
  159. class TMail_Part_Both extends TMail_Part {
  160. function render(){
  161. $html=parent::render();
  162. $this->template->set('contenttype','text/plain');
  163. $c=$this->content;
  164. if($this->content instanceof SMLite)$this->content=$this->content->render();
  165. $this->content=strip_tags($this->content);
  166. $plain=parent::render();
  167. $this->content=$c;
  168. return $plain.$html;
  169. }
  170. }
  171. class TMail_Part_Attachment extends TMail_Part {
  172. }
  173. /**
  174. * Generic implementation of TMail transport.
  175. */
  176. class TMail_Transport extends AbstractController {
  177. function init(){
  178. parent::init();
  179. $this->owner->addHook('send',array($this,'send'));
  180. }
  181. }
  182. /**
  183. * Uses default sending routine
  184. */
  185. class TMail_Transport_Fallback extends TMail_Transport {
  186. function send($tm,$to,$from,$subject,$body,$headers){
  187. $this->breakHook(false);
  188. }
  189. }
  190. /**
  191. * Discards email as it's being sent out
  192. */
  193. class TMail_Transport_Discard extends TMail_Transport {
  194. function send($tm,$to,$from,$subject,$body,$headers){
  195. $this->breakHook(true);
  196. }
  197. }
  198. class TMail_Transport_Echo extends TMail_Transport {
  199. function send($tm,$to,$from,$subject,$body,$headers){
  200. echo "to: $to<br/>";
  201. echo "from: $from<br/>";
  202. echo "subject: $subject<br/>";
  203. echo "<textarea cols=100 rows=30>$body</textarea><hr/>";
  204. echo "<textarea cols=100 rows=10>$headers</textarea><hr/>";
  205. }
  206. }
  207. class TMail_Transport_DBStore extends TMail_Transport {
  208. public $model=null;
  209. function setModel($m){
  210. if(is_string($m))$m='Model_'.$m;
  211. $this->model=$this->add($m);
  212. return $this->model;
  213. }
  214. function send($tm,$to,$from,$subject,$body,$headers){
  215. if(!$this->model)throw $this->exception('Must use setModel() on DBStore Transport');
  216. $data=array(
  217. 'to'=>$to,
  218. 'from'=>$from,
  219. 'subject'=>$subject,
  220. 'body'=>$body,
  221. 'headers'=>$headers
  222. );
  223. $this->model->unloadData()->set($data)->update();
  224. return $this;
  225. }
  226. }
  227. class TMail_Template extends SMLite {
  228. public $template_type='mail';
  229. function init(){
  230. parent::init();
  231. }
  232. }