PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/mm/mail/core.php

https://github.com/mediamedics/ko3_modules
PHP | 374 lines | 271 code | 95 blank | 8 comment | 52 complexity | 913d9e64a44387f951bdacdadbc3c11e MD5 | raw file
  1. <?php
  2. abstract class MM_Mail_Core extends Model{
  3. public $tpl_data;
  4. public $recipient;
  5. public $template;
  6. public $subject;
  7. public $replyto;
  8. public $text_body;
  9. public $html_body;
  10. public $attachment;
  11. private $swift;
  12. private $config;
  13. public function __construct($template = NULL, $tpl_data = NULL){
  14. parent::__construct();
  15. $this->config = Kohana::config('email');
  16. if($template !== NULL){
  17. $this->template = $template;
  18. }
  19. if($tpl_data !== NULL){
  20. $this->tpl_data = $tpl_data;
  21. }
  22. return $this;
  23. }
  24. public static function template($template = NULL, $tpl_data = NULL){
  25. $obj = new MM_Mail($template, $tpl_data);
  26. return $obj;
  27. }
  28. public function connect(){
  29. try{
  30. if (!class_exists('Swift_Mailer', FALSE)){
  31. require Kohana::find_file('vendor', 'swift/lib/classes/Swift');
  32. }
  33. if(!in_array('Swift_ClassLoader', spl_autoload_functions())){
  34. Swift::registerAutoload();
  35. }
  36. require_once Kohana::find_file('vendor', 'swift/lib/swift_init');
  37. $port = $this->config['options']['port'];
  38. $transport = Swift_SmtpTransport::newInstance($this->config['options']['hostname'], $port);
  39. $transport->setTimeout($this->config['options']['timeout']);
  40. $this->swift = Swift_Mailer::newInstance($transport);
  41. return true;
  42. }catch(Swift_Exception $e){
  43. echo $e;
  44. return false;
  45. }
  46. }
  47. public function disconnect(){
  48. spl_autoload_unregister(array('Swift_ClassLoader', 'load'));
  49. return true;
  50. }
  51. public function get_template(){
  52. if(isset($this->template) AND !empty($this->template)){
  53. if(is_string($this->template)){
  54. if(isset($this->tpl_data) AND !empty($this->tpl_data)){
  55. if(is_array($this->tpl_data) OR is_object($this->tpl_data)){
  56. $tpl_data = $this->tpl_data;
  57. }else{
  58. throw new Kohana_Exception('Invalid type for tpl_data');
  59. }
  60. }else{
  61. $tpl_data = array();
  62. }
  63. $template_class = 'MM_Mail_Tpl_'.Text::ucwords($this->template, '_');
  64. if(class_exists($template_class)){
  65. $this->template = new $template_class($tpl_data);
  66. $recipient = $this->template->get_recipient();
  67. // echo Kohana::debug($recipient);
  68. if(!empty($recipient) AND (!isset($this->recipient) OR empty($this->recipient))){
  69. $this->recipient = $recipient;
  70. }
  71. // echo Kohana::debug($this->recipient);
  72. $sender = $this->template->get_sender();
  73. if(!empty($sender) AND (!isset($this->sender) OR empty($this->sender))){
  74. $this->sender = $sender;
  75. }
  76. $subject = $this->template->get_subject();
  77. if(!empty($subject) AND (!isset($this->subject) OR empty($this->subject))){
  78. $this->subject = $subject;
  79. }
  80. $text_body = $this->template->get_text();
  81. if(!empty($text_body) AND (!isset($this->text_body) OR empty($this->text_body))){
  82. $this->text_body = $text_body;
  83. }
  84. $html_body = $this->template->get_html();
  85. if(!empty($html_body) AND (!isset($this->html_body) OR empty($this->html_body))){
  86. $this->html_body = $html_body;
  87. }
  88. $attachment = $this->template->get_attachment();
  89. if(!empty($attachment) AND (!isset($this->attachment) OR empty($this->attachment))){
  90. $this->attachment = $attachment;
  91. }
  92. return true;
  93. }else{
  94. throw new Kohana_Exception('Class '.$template_class.' does not exist');
  95. }
  96. }else{
  97. throw new Kohana_Exception('Invalid type for template property');
  98. }
  99. }
  100. }
  101. public function send(){
  102. $this->connect();
  103. $this->get_template();
  104. $message = Swift_Message::newInstance();
  105. $message->setCharset('utf-8');
  106. if(isset($this->recipient) AND !empty($this->recipient)){
  107. if (is_string($this->recipient)){
  108. if(Validate::email($this->recipient)){
  109. // Single recipient
  110. $message->setTo($this->recipient);
  111. }else{
  112. throw new Kohana_Exception('Invalid e-mail address for recipient');
  113. }
  114. }elseif (is_array($this->recipient)){
  115. if (isset($this->recipient[0]) AND isset($this->recipient[1])){
  116. if(Validate::email($this->recipient[1])){
  117. // Create To: address set
  118. $this->recipient = array('to' => $this->recipient);
  119. }else{
  120. throw new Kohana_Exception('Invalid e-mail address for recipient');
  121. }
  122. }
  123. foreach ($this->recipient as $method => $set){
  124. if ( ! in_array($method, array('to', 'cc', 'bcc'))){
  125. // Use To: by default
  126. $method = 'to';
  127. }
  128. // Create method name
  129. $method = 'add'.ucfirst($method);
  130. if (is_array($set)){
  131. if(Validate::email($set[1])){
  132. // Add a recipient with name
  133. $message->$method($set[0], $set[1]);
  134. }else{
  135. throw new Kohana_Exception('Invalid e-mail address for recipient');
  136. }
  137. }else{
  138. if(Validate::email($set)){
  139. // Add a recipient without name
  140. $message->$method($set);
  141. }else{
  142. throw new Kohana_Exception('Invalid e-mail address for recipient');
  143. }
  144. }
  145. }
  146. }else{
  147. throw new Kohana_Exception('Invalid recipient type');
  148. }
  149. }else{
  150. throw new Kohana_Exception('Recipient is required');
  151. }
  152. if(isset($this->sender) AND !empty($this->sender)){
  153. if (is_string($this->sender)){
  154. if(Validate::email($this->sender)){
  155. $message->setFrom($this->sender);
  156. $message->setSender($this->sender);
  157. }else{
  158. throw new Kohana_Exception('Invalid e-mail address for sender');
  159. }
  160. }elseif (is_array($this->sender)){
  161. $senders = array();
  162. foreach($this->sender as $email_address => $name){
  163. if(Validate::email($email_address)){
  164. $senders[$email] = $name;
  165. if(!isset($primary_sender)){
  166. $primary_sender = $email;
  167. }
  168. }else{
  169. throw new Kohana_Exception('Invalid e-mail address for sender');
  170. }
  171. }
  172. if(count($senders) > 0 && isset($primary_sender)){
  173. $message->setFrom($senders);
  174. $message->setSender($primary_sender);
  175. }else{
  176. $message->setFrom(array($this->config['default']['sender'] => $this->config['default']['sender_name']));
  177. $message->setSender($this->config['default']['sender']);
  178. }
  179. }else{
  180. throw new Kohana_Exception('Invalid sender type');
  181. }
  182. }else{
  183. $message->setFrom(array($this->config['default']['sender'] => $this->config['default']['sender_name']));
  184. $message->setSender($this->config['default']['sender']);
  185. }
  186. if(isset($this->sender) AND !empty($this->sender)){
  187. if (is_string($this->sender)){
  188. if(Validate::email($this->sender)){
  189. $message->setFrom($this->sender);
  190. }else{
  191. throw new Kohana_Exception('Invalid e-mail address for sender');
  192. }
  193. }elseif (is_array($this->sender))
  194. {
  195. if(Validate::email($this->sender[1])){
  196. $message->setFrom($this->sender[0], $this->sender[1]);
  197. }else{
  198. throw new Kohana_Exception('Invalid e-mail address for sender');
  199. }
  200. }else{
  201. throw new Kohana_Exception('Invalid sender type');
  202. }
  203. }else{
  204. $message->setFrom($this->config['default']['sender']);
  205. }
  206. if(isset($this->subject) AND !empty($this->subject)){
  207. if(is_string($this->subject)){
  208. $message->setSubject($this->subject);
  209. }else{
  210. throw new Kohana_Exception('Invalid type for subject');
  211. }
  212. }else{
  213. $message->setSubject = '';
  214. }
  215. if((isset($this->text_body) AND !empty($this->text_body)) OR (isset($this->html_body) AND !empty($this->html_body))){
  216. if(isset($this->text_body) AND !empty($this->text_body)){
  217. if(is_string($this->text_body)){
  218. $message->addPart($this->text_body, 'text/plain');
  219. }else{
  220. throw new Kohana_Exception('Invalidy type for text_body');
  221. }
  222. }
  223. if(isset($this->html_body) AND !empty($this->html_body)){
  224. if(is_string($this->html_body)){
  225. $message->addPart($this->html_body, 'text/html');
  226. }else{
  227. throw new Kohana_Exception('Invalidy type for html_body');
  228. }
  229. }
  230. }else{
  231. throw new Kohana_Exception('Either text_body or html_body must be set');
  232. }
  233. if(isset($this->attachment) AND !empty($this->attachment)){
  234. if(is_string($this->attachment)){
  235. if(file_exists($this->attachment)){
  236. $message->attach(Swift_Attachment::fromPath($this->attachment));
  237. }elseif(Validate::url($this->attachment)){
  238. $message->attach(Swift_Attachment::fromPath($this->attachment));
  239. }else{
  240. throw new Kohana_Exception('Could not find file attachment');
  241. }
  242. }elseif(is_array($this->attachment)){
  243. foreach($this->attachment as $file){
  244. if(is_string($file)){
  245. if(file_exists($file)){
  246. $message->attach(Swift_Attachment::fromPath($file));
  247. }elseif(Validate::url($file)){
  248. $message->attach(Swift_Attachment::fromPath($file));
  249. }else{
  250. throw new Kohana_Exception('Could not find file attachment');
  251. }
  252. }else{
  253. throw new Kohana_Exception('Invalid type for file attachment');
  254. }
  255. }
  256. }else{
  257. throw new Kohana_Exception('Invalid type for file attachment');
  258. }
  259. }
  260. if(isset($this->replyto) && !empty($this->replyto)){
  261. if(is_string($this->replyto)){
  262. if(Validate::email($this->replyto)){
  263. $message->setReplyTo($this->replyto);
  264. }else{
  265. throw new Kohana_Exception('Invalid email address for reply to');
  266. }
  267. }else{
  268. throw new Kohana_Exception('Invalid type for reply to');
  269. }
  270. }
  271. if($this->swift->send($message)){
  272. $this->disconnect();
  273. return true;
  274. }else{
  275. $this->disconnect();
  276. return false;
  277. }
  278. }
  279. }