PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/core/Mail_2.php

https://gitlab.com/fiesta-framework/Documentation
PHP | 410 lines | 260 code | 43 blank | 107 comment | 64 complexity | bad87762cab8cb057cb13245ddd510b5 MD5 | raw file
  1. <?php
  2. /**
  3. * Mail class
  4. */
  5. class Mail
  6. {
  7. private $transport=null;
  8. private $too=null;
  9. private $attachmnt=null;
  10. private $subject=null;
  11. private $froms=null;
  12. private $replay=null;
  13. private $cc=null;
  14. private $cci=null;
  15. //private $type="html";
  16. private static $error="";
  17. //
  18. // SMTP Config
  19. private $host="";
  20. private $port="";
  21. private $secure="";
  22. private $type="html";
  23. private $username="";
  24. private $password="";
  25. private $from=array();
  26. static function configured($self)
  27. {
  28. if(!empty($self->host) || !empty($self->port) || !empty($self->secure) || !empty($self->username) || !empty($self->password) || !empty($self->from))
  29. {
  30. return true;
  31. }
  32. else return false;
  33. }
  34. static function check($selfmail)
  35. {
  36. if(empty($selfmail->host)) throw new InvalidArgumentException("Missing smtp host");
  37. if(empty($selfmail->port)) throw new InvalidArgumentException("Missing smtp port");
  38. if(empty($selfmail->secure)) throw new InvalidArgumentException("Missing smtp secure");
  39. if(empty($selfmail->username)) throw new InvalidArgumentException("Missing smtp username");
  40. if(empty($selfmail->password)) throw new InvalidArgumentException("Missing smtp password");
  41. if(empty($selfmail->from['adresse'])) throw new InvalidArgumentException("Missing smtp adresse from");
  42. if(empty($selfmail->from['name'])) throw new InvalidArgumentException("Missing smtp name from");
  43. }
  44. public static function send($view,$array,$callback)
  45. {
  46. //include "../core/Associates/PHPMailer/class.phpmailer.php";
  47. include "../core/Associates/SwiftMailer/vendor/autoload.php";
  48. //
  49. $selfmail=new self();
  50. $callback($selfmail);
  51. //
  52. //get The View
  53. if($selfmail->type=="text")
  54. {
  55. $body=$view;
  56. $type="text/plain";
  57. }
  58. else if($selfmail->type=="html")
  59. {
  60. $body=View::get($view,$array);
  61. $type="text/html";
  62. }
  63. //
  64. if(!self::configured($selfmail))
  65. {
  66. $selfmail->host=Config::get("mail.host");
  67. $selfmail->port=Config::get("mail.port");
  68. $selfmail->secure=Config::get("mail.encryption");
  69. $selfmail->type="html";
  70. $selfmail->username=Config::get("mail.username");
  71. $selfmail->password=Config::get("mail.password");
  72. $selfmail->from['adresse']=Config::get("mail.from")['adresse'];
  73. $selfmail->from['name']=Config::get("mail.from")['name'];
  74. }
  75. //
  76. self::check($selfmail);
  77. //
  78. $selfmail->transport = Swift_SmtpTransport::newInstance($selfmail->host, $selfmail->port, $selfmail->secure)
  79. ->setUsername($selfmail->username)
  80. ->setPassword($selfmail->password);
  81. // $message = Swift_Message::newInstance('Activation de compte Touhfat Al Arouss')
  82. // ->setFrom(array('job@touhfatalarouss.com' => 'Équipe Touhfat Al Arouss'))
  83. // ->setTo(array('youssefhad2@gmail.com'))
  84. // ->setBody($body , "text/html");
  85. //var_dump($selfmail->transport);
  86. $mailer = Swift_Mailer::newInstance($selfmail->transport);
  87. $subject=is_null($selfmail->subject)?Config::get('mail.subject'):$selfmail->subject;
  88. //
  89. //The Message
  90. $message = Swift_Message::newInstance($subject);
  91. $message->setBody($body , $type);
  92. $message->setFrom(array($selfmail->from['adresse'] => $selfmail->from['name']));
  93. // Check to
  94. //
  95. if(!is_null($selfmail->too) && !empty($selfmail->too) )
  96. $message->setTo($selfmail->too);
  97. else throw new InvalidArgumentException("Missing mail to", 1);
  98. //
  99. // Attaches
  100. if(!is_null($selfmail->attachmnt) && count($selfmail->attachmnt)>0)
  101. foreach ($selfmail->attachmnt as $key => $value)
  102. {
  103. $name="";
  104. $filee="";
  105. //
  106. foreach ($value as $key2 => $value2) {
  107. if($key2==0) $filee=$value2;
  108. else if($key2==1) $name=$value2;
  109. }
  110. if(empty($name))
  111. {
  112. $message->attach(Swift_Attachment::fromPath($filee));
  113. }
  114. else
  115. {
  116. $message->attach(Swift_Attachment::fromPath($filee)->setFilename($name));
  117. }
  118. }
  119. //
  120. // CC
  121. if(!is_null($selfmail->cc) && count($selfmail->cc)>0)
  122. {
  123. $r=array();
  124. //
  125. foreach ($selfmail->cc as $key => $value)
  126. {
  127. $name="";
  128. $mail="";
  129. //
  130. foreach ($value as $key2 => $value2) {
  131. if($key2=="mail") { $mail=$value2; }
  132. else if($key2=="name") { $name=$value2; }
  133. }
  134. //
  135. if(empty($name)) $r[]=$mail;
  136. else $r[$mail]=$name;
  137. }
  138. //
  139. $message->setCC($r);
  140. }
  141. //
  142. // CCI
  143. if(!is_null($selfmail->cci) && count($selfmail->cci)>0)
  144. {
  145. $r=array();
  146. //
  147. foreach ($selfmail->cci as $key => $value)
  148. {
  149. $name="";
  150. $mail="";
  151. //
  152. foreach ($value as $key2 => $value2) {
  153. if($key2=="mail") { $mail=$value2; }
  154. else if($key2=="name") { $name=$value2; }
  155. }
  156. //
  157. if(empty($name)) $r[]=$mail;
  158. else $r[$mail]=$name;
  159. }
  160. //
  161. $message->setBcc($r);
  162. }
  163. //
  164. // Send
  165. $result = $mailer->send($message);
  166. return $result;
  167. // $mail->IsSMTP();
  168. // $mail->SMTPDebug=1;
  169. // $mail->SMTPAuth=true;
  170. // $mail->SMTPSecure=Config::get("mail.encryption");
  171. // $mail->Host=Config::get("mail.host");
  172. // $mail->Port=Config::get("mail.port");
  173. // $mail->Username=Config::get("mail.username");
  174. // $mail->Password=Config::get("mail.password");
  175. // if(is_null($selfmail->froms)) $selfmail->froms=Config::get("mail.from.adresse");
  176. // $mail->SetFrom($selfmail->froms);
  177. // $mail->Subject=$selfmail->subject;
  178. // $mail->Body=$view2;
  179. // //
  180. // foreach ($selfmail->too as $key => $value)
  181. // {
  182. // $name="";
  183. // $maile="";
  184. // //
  185. // foreach ($value as $key2 => $value2) {
  186. // if($key2=="name") $name=$value2;
  187. // else if($key2=="mail") $maile=$value2;
  188. // }
  189. // //
  190. // if(empty($name)) { $mail->AddAddress($maile); }
  191. // else { $mail->AddAddress($maile,$name); }
  192. // }
  193. // //
  194. // // atachement
  195. // if(!is_null($selfmail->atachmnt) && count($selfmail->atachmnt)>0)
  196. // foreach ($selfmail->atachmnt as $key => $value)
  197. // {
  198. // $name="";
  199. // $filee="";
  200. // //
  201. // foreach ($value as $key2 => $value2) {
  202. // if($key2==1) $name=$value2;
  203. // else if($key2==0) $filee=$value2;
  204. // }
  205. // //
  206. // if(empty($name)) { $mail->addAttachment($filee); }
  207. // else { $mail->addAttachment($filee,$name); }
  208. // }
  209. // //
  210. // // CC
  211. // if(!is_null($selfmail->cc))
  212. // foreach ($selfmail->cc as $key => $value)
  213. // {
  214. // $mail->addCC($value);
  215. // }
  216. // //
  217. // // CCB
  218. // if(!is_null($selfmail->cci))
  219. // foreach ($selfmail->cci as $key => $value)
  220. // {
  221. // $mail->addBCC($value);
  222. // }
  223. // if(!$mail->Send())
  224. // {
  225. // self::$error=$mail->ErrorInfo;
  226. // return false;
  227. // }
  228. // else return true;
  229. }
  230. public function to()
  231. {
  232. //
  233. $r=func_get_args();
  234. $r2=array();
  235. //
  236. if(count($r)==1)
  237. {
  238. if(is_array($r))
  239. {
  240. foreach ($r as $key => $value) {
  241. if(is_array($value))
  242. {
  243. $i=0;
  244. $ry=array();
  245. foreach ($value as $key => $value) {
  246. $r2[]= $value;
  247. }
  248. }
  249. else if(is_string($value))
  250. {
  251. $r2[]= array(
  252. 'mail' => $value
  253. );
  254. }
  255. }
  256. }
  257. else if(is_string($r))
  258. {
  259. $r2[]= array(
  260. 'mail' => $r
  261. );
  262. }
  263. }
  264. else if(count($r)==2)
  265. {
  266. $r2[]= array(
  267. 'mail' => $r[0],
  268. 'name' => $r[1]
  269. );
  270. }
  271. //
  272. $this->too=$r2;
  273. return $this;
  274. }
  275. public function attach()
  276. {
  277. //
  278. $r=func_get_args();
  279. $r2=array();
  280. //
  281. if(count($r)==1)
  282. {
  283. if(is_array($r))
  284. {
  285. foreach ($r as $key => $value) {
  286. if(is_array($value))
  287. {
  288. $i=0;
  289. $ry=array();
  290. foreach ($value as $key => $value) {
  291. $r2[]= $value;
  292. }
  293. }
  294. else if(is_string($value))
  295. {
  296. $r2[]= array(
  297. 'file' => $value
  298. );
  299. }
  300. }
  301. }
  302. else if(is_string($r))
  303. {
  304. $r2[]= array(
  305. 'file' => $r
  306. );
  307. }
  308. }
  309. else if(count($r)==2)
  310. {
  311. $r2[]= array(
  312. 'file' => $r[0],
  313. 'name' => $r[1]
  314. );
  315. }
  316. //
  317. $this->attachmnt=$r2;
  318. return $this;
  319. }
  320. public function subject($subject)
  321. {
  322. $this->subject=$subject;
  323. return $this;
  324. }
  325. public function from($from)
  326. {
  327. $this->froms=$from;
  328. return $this;
  329. }
  330. public function cc()
  331. {
  332. $r=func_get_args();
  333. $mail="";
  334. $name="";
  335. $r2=array();
  336. foreach ($r as $key => $value)
  337. foreach ($value as $key2 => $value2)
  338. {
  339. if(is_numeric($key2)) { $mail= $value2; $name=""; }
  340. else { $mail= $key2; $name=$value2; }
  341. $r2[]=array("name"=>$name,"mail"=>$mail);
  342. }
  343. $this->cc=$r2;
  344. return $this;
  345. }
  346. public function cci($from)
  347. {
  348. $r=func_get_args();
  349. $mail="";
  350. $name="";
  351. $r2=array();
  352. foreach ($r as $key => $value)
  353. foreach ($value as $key2 => $value2)
  354. {
  355. if(is_numeric($key2)) { $mail= $value2; $name=""; }
  356. else { $mail= $key2; $name=$value2; }
  357. $r2[]=array("name"=>$name,"mail"=>$mail);
  358. }
  359. $this->cci=$r2;
  360. return $this;
  361. }
  362. public function type($type)
  363. {
  364. $this->type=$type;
  365. return $this;
  366. }
  367. }