PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/xe/classes/mail/Mail.class.php

https://bitbucket.org/pipoket/hanu_xe
PHP | 353 lines | 292 code | 57 blank | 4 comment | 17 complexity | ab14bbc2061e6cf17db5a501020acf73 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @brief Mailing
  4. * @author NHN (developers@xpressengine.com)
  5. **/
  6. class Mail {
  7. var $sender_name = '';
  8. var $sender_email = '';
  9. var $receiptor_name = '';
  10. var $receiptor_email = '';
  11. var $title = '';
  12. var $content = '';
  13. var $content_type = 'html';
  14. var $messageId = null;
  15. var $replyTo = null;
  16. var $bcc = null;
  17. var $attachments = array();
  18. var $cidAttachments = array();
  19. var $mainMailPart = null;
  20. var $body = '';
  21. var $header = '';
  22. var $eol = '';
  23. var $references = '';
  24. var $additional_params = null;
  25. function Mail() { }
  26. function setAdditionalParams($additional_params)
  27. {
  28. $this->additional_params = $additional_params;
  29. }
  30. function addAttachment($filename, $orgfilename)
  31. {
  32. $this->attachments[$orgfilename] = $filename;
  33. }
  34. function addCidAttachment($filename, $cid)
  35. {
  36. $this->cidAttachments[$cid] = $filename;
  37. }
  38. function setSender($name, $email) {
  39. $this->sender_name = $name;
  40. $this->sender_email = $email;
  41. }
  42. function getSender() {
  43. if(!stristr(PHP_OS, 'win') && $this->sender_name) return sprintf("%s <%s>", '=?utf-8?b?'.base64_encode($this->sender_name).'?=', $this->sender_email);
  44. return $this->sender_email;
  45. }
  46. function setReceiptor($name, $email) {
  47. $this->receiptor_name = $name;
  48. $this->receiptor_email = $email;
  49. }
  50. function getReceiptor() {
  51. if(!stristr(PHP_OS, 'win') && $this->receiptor_name && $this->receiptor_name != $this->receiptor_email) return sprintf("%s <%s>", '=?utf-8?b?'.base64_encode($this->receiptor_name).'?=', $this->receiptor_email);
  52. return $this->receiptor_email;
  53. }
  54. function setTitle($title) {
  55. $this->title = $title;
  56. }
  57. function getTitle() {
  58. return '=?utf-8?b?'.base64_encode($this->title).'?=';
  59. }
  60. function setBCC($bcc)
  61. {
  62. $this->bcc = $bcc;
  63. }
  64. function setMessageID($messageId) {
  65. $this->messageId = $messageId;
  66. }
  67. function setReferences($references) {
  68. $this->references = $references;
  69. }
  70. function setReplyTo($replyTo)
  71. {
  72. $this->replyTo = $replyTo;
  73. }
  74. function setContent($content) {
  75. $content = preg_replace_callback('/<img([^>]+)>/i',array($this,'replaceResourceRealPath'), $content);
  76. $this->content = $content;
  77. }
  78. function replaceResourceRealPath($matches) {
  79. return preg_replace('/src=(["\']?)files/i','src=$1'.Context::getRequestUri().'files', $matches[0]);
  80. }
  81. function getPlainContent() {
  82. return chunk_split(base64_encode(str_replace(array("<",">","&"), array("&lt;","&gt;","&amp;"), $this->content)));
  83. }
  84. function getHTMLContent() {
  85. return chunk_split(base64_encode($this->content_type!='html'?nl2br($this->content):$this->content));
  86. }
  87. function setContentType($mode = 'html') {
  88. $this->content_type = $mode=='html'?'html':'';
  89. }
  90. function procAttachments()
  91. {
  92. if(count($this->attachments) > 0)
  93. {
  94. $this->body = $this->header.$this->body;
  95. $boundary = '----=='.uniqid(rand(),true);
  96. $this->header = "Content-Type: multipart/mixed;".$this->eol."\tboundary=\"".$boundary."\"".$this->eol.$this->eol;
  97. $this->body = "--".$boundary.$this->eol.$this->body.$this->eol.$this->eol;
  98. $res = array();
  99. $res[] = $this->body;
  100. foreach($this->attachments as $filename => $attachment)
  101. {
  102. $type = $this->returnMIMEType($filename);
  103. $file_str = FileHandler::readFile($attachment);
  104. $chunks = chunk_split(base64_encode($file_str));
  105. $tempBody = sprintf(
  106. "--".$boundary.$this->eol.
  107. "Content-Type: %s;".$this->eol.
  108. "\tname=\"%s\"".$this->eol.
  109. "Content-Transfer-Encoding: base64".$this->eol.
  110. "Content-Description: %s".$this->eol.
  111. "Content-Disposition: attachment;".$this->eol.
  112. "\tfilename=\"%s\"".$this->eol.$this->eol.
  113. "%s".$this->eol.$this->eol,
  114. $type,
  115. $filename,
  116. $filename,
  117. $filename,
  118. $chunks);
  119. $res[] = $tempBody;
  120. }
  121. $this->body = implode("", $res);
  122. $this->body .= "--".$boundary."--";
  123. }
  124. }
  125. function procCidAttachments()
  126. {
  127. if(count($this->cidAttachments) > 0)
  128. {
  129. $this->body = $this->header.$this->body;
  130. $boundary = '----=='.uniqid(rand(),true);
  131. $this->header = "Content-Type: multipart/relative;".$this->eol."\ttype=\"multipart/alternative\";".$this->eol."\tboundary=\"".$boundary."\"".$this->eol.$this->eol;
  132. $this->body = "--".$boundary.$this->eol.$this->body.$this->eol.$this->eol;
  133. $res = array();
  134. $res[] = $this->body;
  135. foreach($this->cidAttachments as $cid => $attachment)
  136. {
  137. $filename = basename($attachment);
  138. $type = $this->returnMIMEType(FileHandler::getRealPath($attachment));
  139. $file_str = FileHandler::readFile($attachment);
  140. $chunks = chunk_split(base64_encode($file_str));
  141. $tempBody = sprintf(
  142. "--".$boundary.$this->eol.
  143. "Content-Type: %s;".$this->eol.
  144. "\tname=\"%s\"".$this->eol.
  145. "Content-Transfer-Encoding: base64".$this->eol.
  146. "Content-ID: <%s>".$this->eol.
  147. "Content-Description: %s".$this->eol.
  148. "Content-Location: %s".$this->eol.$this->eol.
  149. "%s".$this->eol.$this->eol,
  150. $type,
  151. $filename,
  152. $cid,
  153. $filename,
  154. $filename,
  155. $chunks);
  156. $res[] = $tempBody;
  157. }
  158. $this->body = implode("", $res);
  159. $this->body .= "--".$boundary."--";
  160. }
  161. }
  162. function send() {
  163. $boundary = '----=='.uniqid(rand(),true);
  164. $this->eol = $GLOBALS['_qmail_compatibility'] == "Y" ? "\n" : "\r\n";
  165. $this->header = "Content-Type: multipart/alternative;".$this->eol."\tboundary=\"".$boundary."\"".$this->eol.$this->eol;
  166. $this->body = sprintf(
  167. "--%s".$this->eol.
  168. "Content-Type: text/plain; charset=utf-8; format=flowed".$this->eol.
  169. "Content-Transfer-Encoding: base64".$this->eol.
  170. "Content-Disposition: inline".$this->eol.$this->eol.
  171. "%s".
  172. "--%s".$this->eol.
  173. "Content-Type: text/html; charset=utf-8".$this->eol.
  174. "Content-Transfer-Encoding: base64".$this->eol.
  175. "Content-Disposition: inline".$this->eol.$this->eol.
  176. "%s".
  177. "--%s--".
  178. "",
  179. $boundary,
  180. $this->getPlainContent(),
  181. $boundary,
  182. $this->getHTMLContent(),
  183. $boundary
  184. );
  185. $this->procCidAttachments();
  186. $this->procAttachments();
  187. $headers = sprintf(
  188. "From: %s".$this->eol.
  189. "%s".
  190. "%s".
  191. "%s".
  192. "%s".
  193. "MIME-Version: 1.0".$this->eol."",
  194. $this->getSender(),
  195. $this->messageId?("Message-ID: <".$this->messageId.">".$this->eol):"",
  196. $this->replyTo?("Reply-To: <".$this->replyTo.">".$this->eol):"",
  197. $this->bcc?("Bcc: ".$this->bcc.$this->eol):"",
  198. $this->references?("References: <".$this->references.">".$this->eol."In-Reply-To: <".$this->references.">".$this->eol):""
  199. );
  200. $headers .= $this->header;
  201. if($this->additional_params) return mail($this->getReceiptor(), $this->getTitle(), $this->body, $headers, $this->additional_params);
  202. return mail($this->getReceiptor(), $this->getTitle(), $this->body, $headers);
  203. }
  204. function checkMailMX($email_address) {
  205. if(!Mail::isVaildMailAddress($email_address)) return false;
  206. list($user, $host) = explode("@", $email_address);
  207. if(function_exists('checkdnsrr')) {
  208. if (checkdnsrr($host, "MX") or checkdnsrr($host, "A")) return true;
  209. else return false;
  210. }
  211. return true;
  212. }
  213. function isVaildMailAddress($email_address) {
  214. if( preg_match("/([a-z0-9\_\-\.]+)@([a-z0-9\_\-\.]+)/i", $email_address) ) return $email_address;
  215. else return '';
  216. }
  217. function returnMIMEType($filename)
  218. {
  219. preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
  220. switch(strtolower($fileSuffix[1]))
  221. {
  222. case "js" :
  223. return "application/x-javascript";
  224. case "json" :
  225. return "application/json";
  226. case "jpg" :
  227. case "jpeg" :
  228. case "jpe" :
  229. return "image/jpg";
  230. case "png" :
  231. case "gif" :
  232. case "bmp" :
  233. case "tiff" :
  234. return "image/".strtolower($fileSuffix[1]);
  235. case "css" :
  236. return "text/css";
  237. case "xml" :
  238. return "application/xml";
  239. case "doc" :
  240. case "docx" :
  241. return "application/msword";
  242. case "xls" :
  243. case "xlt" :
  244. case "xlm" :
  245. case "xld" :
  246. case "xla" :
  247. case "xlc" :
  248. case "xlw" :
  249. case "xll" :
  250. return "application/vnd.ms-excel";
  251. case "ppt" :
  252. case "pps" :
  253. return "application/vnd.ms-powerpoint";
  254. case "rtf" :
  255. return "application/rtf";
  256. case "pdf" :
  257. return "application/pdf";
  258. case "html" :
  259. case "htm" :
  260. case "php" :
  261. return "text/html";
  262. case "txt" :
  263. return "text/plain";
  264. case "mpeg" :
  265. case "mpg" :
  266. case "mpe" :
  267. return "video/mpeg";
  268. case "mp3" :
  269. return "audio/mpeg3";
  270. case "wav" :
  271. return "audio/wav";
  272. case "aiff" :
  273. case "aif" :
  274. return "audio/aiff";
  275. case "avi" :
  276. return "video/msvideo";
  277. case "wmv" :
  278. return "video/x-ms-wmv";
  279. case "mov" :
  280. return "video/quicktime";
  281. case "zip" :
  282. return "application/zip";
  283. case "tar" :
  284. return "application/x-tar";
  285. case "swf" :
  286. return "application/x-shockwave-flash";
  287. default :
  288. if(function_exists("mime_content_type"))
  289. {
  290. $fileSuffix = mime_content_type($filename);
  291. }
  292. return "unknown/" . trim($fileSuffix[0], ".");
  293. }
  294. }
  295. }
  296. ?>