PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/osCommerce/OM/Core/Mail.php

https://github.com/bodi000/oscommerce
PHP | 375 lines | 294 code | 75 blank | 6 comment | 41 complexity | 1b6bea20db823cf66c7da4c0a6cc33d6 MD5 | raw file
  1. <?php
  2. /**
  3. * osCommerce Online Merchant
  4. *
  5. * @copyright Copyright (c) 2011 osCommerce; http://www.oscommerce.com
  6. * @license BSD License; http://www.oscommerce.com/bsdlicense.txt
  7. */
  8. namespace osCommerce\OM\Core;
  9. class Mail {
  10. var $_to = array(),
  11. $_from = array(),
  12. $_cc = array(),
  13. $_bcc = array(),
  14. $_subject,
  15. $_body_plain,
  16. $_body_html,
  17. $_attachments = array(),
  18. $_images = array(),
  19. $_boundary,
  20. $_headers = array('X-Mailer' => 'osCommerce'),
  21. $_body,
  22. $_content_transfer_encoding = '7bit',
  23. $_charset = 'iso-8859-1';
  24. public function __construct($to = null, $to_email_address = null, $from = null, $from_email_address = null, $subject = null) {
  25. if ( !empty($to_email_address) ) {
  26. $this->_to[] = array('name' => $to,
  27. 'email_address' => $to_email_address);
  28. }
  29. if ( !empty($from_email_address) ) {
  30. $this->_from = array('name' => $from,
  31. 'email_address' => $from_email_address);
  32. }
  33. if ( !empty($subject) ) {
  34. $this->_subject = $subject;
  35. }
  36. }
  37. function addTo($name = null, $email_address) {
  38. $this->_to[] = array('name' => $name,
  39. 'email_address' => $email_address);
  40. }
  41. function setFrom($name = null, $email_address) {
  42. $this->_from = array('name' => $name,
  43. 'email_address' => $email_address);
  44. }
  45. function addCC($name = null, $email_address) {
  46. $this->_cc[] = array('name' => $name,
  47. 'email_address' => $email_address);
  48. }
  49. function addBCC($name = null, $email_address) {
  50. $this->_bcc[] = array('name' => $name,
  51. 'email_address' => $email_address);
  52. }
  53. function clearTo() {
  54. $this->_to = array();
  55. $this->_cc = array();
  56. $this->_bcc = array();
  57. $this->_headers = array('X-Mailer' => 'osCommerce');
  58. }
  59. function setSubject($subject) {
  60. $this->_subject = $subject;
  61. }
  62. function setBodyPlain($body) {
  63. $this->_body_plain = $body;
  64. $this->_body = null;
  65. }
  66. function setBodyHTML($body) {
  67. $this->_body_html = $body;
  68. $this->_body = null;
  69. }
  70. function setContentTransferEncoding($encoding) {
  71. $this->_content_transfer_encoding = $encoding;
  72. }
  73. function setCharset($charset) {
  74. $this->_charset = $charset;
  75. }
  76. function addHeader($key, $value) {
  77. if ( ( strpos($key, "\n") !== false ) || ( strpos($key, "\r") !== false ) ) {
  78. return false;
  79. }
  80. if ( ( strpos($value, "\n") !== false ) || ( strpos($value, "\r") !== false ) ) {
  81. return false;
  82. }
  83. $this->_headers[$key] = $value;
  84. }
  85. function addAttachment($file, $is_uploaded = false) {
  86. if ( $is_uploaded === true ) {
  87. } elseif ( file_exists($file) && is_readable($file) ) {
  88. $data = file_get_contents($file);
  89. $filename = basename($file);
  90. $mimetype = $this->_get_mime_type($filename);
  91. } else {
  92. return false;
  93. }
  94. $this->_attachments[] = array('filename' => $filename,
  95. 'mimetype' => $mimetype,
  96. 'data' => chunk_split(base64_encode($data)));
  97. }
  98. function addImage($file, $is_uploaded = false) {
  99. if ( $is_uploaded === true ) {
  100. } elseif ( file_exists($file) && is_readable($file) ) {
  101. $data = file_get_contents($file);
  102. $filename = basename($file);
  103. $mimetype = $this->_get_mime_type($filename);
  104. } else {
  105. return false;
  106. }
  107. $this->_images[] = array('id' => md5(uniqid(time())),
  108. 'filename' => $filename,
  109. 'mimetype' => $mimetype,
  110. 'data' => chunk_split(base64_encode($data)));
  111. }
  112. function send() {
  113. if ( empty($this->_body) ) {
  114. if ( !empty($this->_body_plain) && !empty($this->_body_html) ) {
  115. $this->_boundary = '=_____MULTIPART_MIXED_BOUNDARY____';
  116. $this->_related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____';
  117. $this->_alternative_boundary = '=_____MULTIPART_ALTERNATIVE_BOUNDARY____';
  118. $this->_headers['MIME-Version'] = '1.0';
  119. $this->_headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
  120. $this->_headers['Content-Transfer-Encoding'] = $this->_content_transfer_encoding;
  121. if ( !empty($this->_images) ) {
  122. foreach ( $this->_images as $image ) {
  123. $this->_body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->_body_html);
  124. }
  125. unset($image);
  126. }
  127. $this->_body = 'This is a multi-part message in MIME format.' . "\n\n" .
  128. '--' . $this->_boundary . "\n";
  129. $this->_body .= 'Content-Type: multipart/alternative; boundary="' . $this->_alternative_boundary . '";' . "\n\n" .
  130. '--' . $this->_alternative_boundary . "\n" .
  131. 'Content-Type: text/plain; charset="' . $this->_charset . '"' . "\n" .
  132. 'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
  133. $this->_body_plain . "\n\n" .
  134. '--' . $this->_alternative_boundary . "\n" .
  135. 'Content-Type: multipart/related; boundary="' . $this->_related_boundary . '"' . "\n\n" .
  136. '--' . $this->_related_boundary . "\n" .
  137. 'Content-Type: text/html; charset="' . $this->_charset . '"' . "\n" .
  138. 'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
  139. $this->_body_html . "\n\n";
  140. if ( !empty($this->_images) ) {
  141. foreach ( $this->_images as $image ) {
  142. $this->_body .= $this->_build_image($image, $this->_related_boundary);
  143. }
  144. unset($image);
  145. }
  146. $this->_body .= '--' . $this->_related_boundary . '--' . "\n\n" .
  147. '--' . $this->_alternative_boundary . '--' . "\n\n";
  148. if ( !empty($this->_attachments) ) {
  149. foreach ( $this->_attachments as $attachment ) {
  150. $this->_body .= $this->_build_attachment($attachment, $this->_boundary);
  151. }
  152. unset($attachment);
  153. }
  154. $this->_body .= '--' . $this->_boundary . '--' . "\n\n";
  155. } elseif ( !empty($this->_body_html) && !empty($this->_images) ) {
  156. $this->_boundary = '=_____MULTIPART_MIXED_BOUNDARY____';
  157. $this->_related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____';
  158. $this->_headers['MIME-Version'] = '1.0';
  159. $this->_headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
  160. foreach ( $this->_images as $image ) {
  161. $this->_body_html = str_replace('src="' . $image['filename'] . '"', 'src="cid:' . $image['id'] . '"', $this->_body_html);
  162. }
  163. unset($image);
  164. $this->_body = 'This is a multi-part message in MIME format.' . "\n\n" .
  165. '--' . $this->_boundary . "\n" .
  166. 'Content-Type: multipart/related; boundary="' . $this->_related_boundary . '";' . "\n\n" .
  167. '--' . $this->_related_boundary . "\n" .
  168. 'Content-Type: text/html; charset="' . $this->_charset . '"' . "\n" .
  169. 'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
  170. $this->_body_html . "\n\n";
  171. foreach ( $this->_images as $image ) {
  172. $this->_body .= $this->_build_image($image, $this->_related_boundary);
  173. }
  174. unset($image);
  175. $this->_body .= '--' . $this->_related_boundary . '--' . "\n\n";
  176. foreach ( $this->_attachments as $attachment ) {
  177. $this->_body .= $this->_build_attachment($attachment, $this->_boundary);
  178. }
  179. unset($attachment);
  180. $this->_body .= '--' . $this->_boundary . '--' . "\n";
  181. } elseif ( !empty($this->_attachments) ) {
  182. $this->_boundary = '=_____MULTIPART_MIXED_BOUNDARY____';
  183. $this->_related_boundary = '=_____MULTIPART_RELATED_BOUNDARY____';
  184. $this->_headers['MIME-Version'] = '1.0';
  185. $this->_headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
  186. $this->_body = 'This is a multi-part message in MIME format.' . "\n\n" .
  187. '--' . $this->_boundary . "\n" .
  188. 'Content-Type: multipart/related; boundary="' . $this->_related_boundary . '";' . "\n\n" .
  189. '--' . $this->_related_boundary . "\n" .
  190. 'Content-Type: text/' . (empty($this->_body_plain) ? 'html' : 'plain') . '; charset="' . $this->_charset . '"' . "\n" .
  191. 'Content-Transfer-Encoding: ' . $this->_content_transfer_encoding . "\n\n" .
  192. (empty($this->_body_plain) ? $this->_body_html : $this->_body_plain) . "\n\n" .
  193. '--' . $this->_related_boundary . '--' . "\n\n";
  194. foreach ( $this->_attachments as $attachment ) {
  195. $this->_body .= $this->_build_attachment($attachment, $this->_boundary);
  196. }
  197. unset($attachment);
  198. $this->_body .= '--' . $this->_boundary . '--' . "\n";
  199. } elseif ( !empty($this->_body_html) ) {
  200. $this->_headers['MIME-Version'] = '1.0';
  201. $this->_headers['Content-Type'] = 'text/html; charset="' . $this->_charset . '"';
  202. $this->_headers['Content-Transfer-Encoding'] = $this->_content_transfer_encoding;
  203. $this->_body = $this->_body_html . "\n";
  204. } else {
  205. $this->_body = $this->_body_plain . "\n";
  206. }
  207. }
  208. $to_email_addresses = array();
  209. foreach ( $this->_to as $to ) {
  210. if ( ( strpos($to['email_address'], "\n") !== false ) || ( strpos($to['email_address'], "\r") !== false ) ) {
  211. return false;
  212. }
  213. if ( ( strpos($to['name'], "\n") !== false ) || ( strpos($to['name'], "\r") !== false ) ) {
  214. return false;
  215. }
  216. if ( empty($to['name']) ) {
  217. $to_email_addresses[] = $to['email_address'];
  218. } else {
  219. $to_email_addresses[] = '"' . $to['name'] . '" <' . $to['email_address'] . '>';
  220. }
  221. }
  222. unset($to);
  223. $cc_email_addresses = array();
  224. foreach ( $this->_cc as $cc ) {
  225. if ( empty($cc['name']) ) {
  226. $cc_email_addresses[] = $cc['email_address'];
  227. } else {
  228. $cc_email_addresses[] = '"' . $cc['name'] . '" <' . $cc['email_address'] . '>';
  229. }
  230. }
  231. unset($cc);
  232. $bcc_email_addresses = array();
  233. foreach ( $this->_bcc as $bcc ) {
  234. if ( empty($bcc['name']) ) {
  235. $bcc_email_addresses[] = $bcc['email_address'];
  236. } else {
  237. $bcc_email_addresses[] = '"' . $bcc['name'] . '" <' . $bcc['email_address'] . '>';
  238. }
  239. }
  240. unset($bcc);
  241. if ( empty($this->_from['name']) ) {
  242. $this->addHeader('From', $this->_from['email_address']);
  243. } else {
  244. $this->addHeader('From', '"' . $this->_from['name'] . '" <' . $this->_from['email_address'] . '>');
  245. }
  246. if ( !empty($cc_email_addresses) ) {
  247. $this->addHeader('Cc', implode(', ', $cc_email_addresses));
  248. }
  249. if ( !empty($bcc_email_addresses) ) {
  250. $this->addHeader('Bcc', implode(', ', $bcc_email_addresses));
  251. }
  252. $headers = '';
  253. foreach ( $this->_headers as $key => $value ) {
  254. $headers .= $key . ': ' . $value . "\n";
  255. }
  256. if ( empty($this->_from['email_address']) || empty($to_email_addresses) ) {
  257. return false;
  258. }
  259. if ( empty($this->_from['name']) ) {
  260. @ini_set('sendmail_from', $this->_from['email_address']);
  261. } else {
  262. @ini_set('sendmail_from', '"' . $this->_from['name'] . '" <' . $this->_from['email_address'] . '>');
  263. }
  264. @mail(implode(', ', $to_email_addresses), $this->_subject, $this->_body, $headers);
  265. @ini_restore('sendmail_from');
  266. }
  267. function _get_mime_type($file) {
  268. $ext = substr($file, strrpos($file, '.') + 1);
  269. $mime_types = array('gif' => 'image/gif',
  270. 'jpg' => 'image/jpeg',
  271. 'jpeg' => 'image/jpeg',
  272. 'jpe' => 'image/jpeg',
  273. 'bmp' => 'image/bmp',
  274. 'png' => 'image/png',
  275. 'tif' => 'image/tiff',
  276. 'tiff' => 'image/tiff',
  277. 'swf' => 'application/x-shockwave-flash');
  278. if (isset($mime_types[$ext])) {
  279. return $mime_types[$ext];
  280. } else {
  281. return 'application/octet-stream';
  282. }
  283. }
  284. function _build_attachment($attachment, $boundary) {
  285. return '--' . $boundary . "\n" .
  286. 'Content-Type: ' . $attachment['mimetype'] . '; name="' . $attachment['filename'] . '"' . "\n" .
  287. 'Content-Disposition: attachment' . "\n" .
  288. 'Content-Transfer-Encoding: base64' . "\n\n" .
  289. $attachment['data'] . "\n\n";
  290. }
  291. function _build_image($image, $boundary) {
  292. return '--' . $boundary . "\n" .
  293. 'Content-Type: ' . $image['mimetype'] . '; name="' . $image['filename'] . '"' . "\n" .
  294. 'Content-ID: ' . $image['id'] . "\n" .
  295. 'Content-Disposition: inline' . "\n" .
  296. 'Content-Transfer-Encoding: base64' . "\n\n" .
  297. $image['data'] . "\n\n";
  298. }
  299. }
  300. ?>