PageRenderTime 27ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ojs/ojs-2.0.1/classes/mail/Mail.inc.php

https://github.com/mcrider/pkpUpgradeTestSuite
PHP | 363 lines | 232 code | 67 blank | 64 comment | 52 complexity | 483a1be65c1ce527fdfc3235b6326139 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Mail.inc.php
  4. *
  5. * Copyright (c) 2003-2004 The Public Knowledge Project
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package mail
  9. *
  10. * Class defining basic operations for handling and sending emails.
  11. *
  12. * $Id: Mail.inc.php,v 1.17 2005/05/15 10:36:06 kevin Exp $
  13. */
  14. define('MAIL_EOL', Core::isWindows() ? "\r\n" : "\n");
  15. define('MAIL_WRAP', 76);
  16. class Mail extends DataObject {
  17. /**
  18. * Constructor.
  19. */
  20. function Mail() {
  21. parent::DataObject();
  22. }
  23. function addRecipient($email, $name = '') {
  24. if ($recipients = &$this->getData('recipients') == null) {
  25. $recipients = array();
  26. }
  27. array_push($recipients, array('name' => $name, 'email' => $email));
  28. return $this->setData('recipients', $recipients);
  29. }
  30. function setEnvelopeSender($envelopeSender) {
  31. $this->setData('envelopeSender', $envelopeSender);
  32. }
  33. function getEnvelopeSender() {
  34. return $this->getData('envelopeSender');
  35. }
  36. function getContentType() {
  37. return $this->getData('content_type');
  38. }
  39. function setContentType($contentType) {
  40. return $this->setData('content_type', $contentType);
  41. }
  42. function getRecipients() {
  43. return $this->getData('recipients');
  44. }
  45. function setRecipients(&$recipients) {
  46. return $this->setData('recipients', &$recipients);
  47. }
  48. function addCc($email, $name = '') {
  49. if ($ccs = &$this->getData('ccs') == null) {
  50. $ccs = array();
  51. }
  52. array_push($ccs, array('name' => $name, 'email' => $email));
  53. return $this->setData('ccs', $ccs);
  54. }
  55. function getCcs() {
  56. return $this->getData('ccs');
  57. }
  58. function setCcs(&$ccs) {
  59. return $this->setData('ccs', &$ccs);
  60. }
  61. function addBcc($email, $name = '') {
  62. if ($bccs = &$this->getData('bccs') == null) {
  63. $bccs = array();
  64. }
  65. array_push($bccs, array('name' => $name, 'email' => $email));
  66. return $this->setData('bccs', $bccs);
  67. }
  68. function getBccs() {
  69. return $this->getData('bccs');
  70. }
  71. function setBccs(&$bccs) {
  72. return $this->setData('bccs', &$bccs);
  73. }
  74. function addHeader($name, $content) {
  75. $updated = false;
  76. if ($headers = &$this->getData('headers') == null) {
  77. $headers = array();
  78. }
  79. foreach ($headers as $key => $value) {
  80. if ($headers[$key]['name'] == $name) {
  81. $headers[$key]['content'] = $content;
  82. $updated = true;
  83. }
  84. }
  85. if (!$updated) {
  86. array_push($headers, array('name' => $name,'content' => $content));
  87. }
  88. return $this->setData('headers', $headers);
  89. }
  90. function getHeaders() {
  91. return $this->getData('headers');
  92. }
  93. /**
  94. * Adds a file attachment to the email.
  95. * @param $filePath string complete path to the file to attach
  96. * @param $fileName string attachment file name (optional)
  97. * @param $contentType string attachment content type (optional)
  98. * @param $contentDisposition string attachment content disposition, inline or attachment (optional, default attachment)
  99. */
  100. function addAttachment($filePath, $fileName = '', $contentType = '', $contentDisposition = 'attachment') {
  101. if ($attachments = &$this->getData('attachments') == null) {
  102. $attachments = array();
  103. }
  104. /* If the arguments $fileName and $contentType are not specified,
  105. then try and determine them automatically. */
  106. if (empty($fileName)) {
  107. $fileName = basename($filePath);
  108. }
  109. if (empty($contentType)) {
  110. if (function_exists('mime_content_type')) {
  111. $contentType = mime_content_type($filePath);
  112. } else {
  113. $contentType = 'application/x-unknown-content-type';
  114. }
  115. }
  116. // Open the file and read contents into $attachment
  117. if (is_readable($filePath) && is_file($filePath)) {
  118. $fp = fopen($filePath, 'rb');
  119. if ($fp) {
  120. $content = '';
  121. while (!feof($fp)) {
  122. $content .= fread($fp, 4096);
  123. }
  124. fclose($fp);
  125. }
  126. }
  127. if (isset($content)) {
  128. /* Encode the contents in base64. */
  129. $content = chunk_split(base64_encode($content), MAIL_WRAP, MAIL_EOL);
  130. array_push($attachments, array('filename' => $fileName, 'content-type' => $contentType, 'disposition' => $contentDisposition, 'content' => $content));
  131. return $this->setData('attachments', $attachments);
  132. } else {
  133. return false;
  134. }
  135. }
  136. function &getAttachments() {
  137. return $this->getData('attachments');
  138. }
  139. function hasAttachments() {
  140. $attachments = &$this->getAttachments();
  141. return ($attachments != null && count($attachments) != 0);
  142. }
  143. function setFrom($email, $name = '') {
  144. return $this->setData('from', array('name' => $name, 'email' => $email));
  145. }
  146. function getFrom() {
  147. return $this->getData('from');
  148. }
  149. function setSubject($subject) {
  150. return $this->setData('subject', $subject);
  151. }
  152. function getSubject() {
  153. return $this->getData('subject');
  154. }
  155. function setBody($body) {
  156. return $this->setData('body', $body);
  157. }
  158. function getBody() {
  159. return $this->getData('body');
  160. }
  161. /**
  162. * Return a string containing the from address.
  163. * @param $encode boolean encode the data (e.g., when sending)
  164. * @return string
  165. */
  166. function getFromString($encode = true) {
  167. $from = $this->getFrom();
  168. if ($from == null) {
  169. return null;
  170. } else {
  171. return ($encode ? String::encode_mime_header($from['name']) : $from['name']) . ' <'.$from['email'].'>';
  172. }
  173. }
  174. /**
  175. * Return a string from an array of (name, email) pairs.
  176. * @param $encode boolean
  177. * @return string;
  178. */
  179. function getAddressArrayString($addresses, $encode = true) {
  180. if ($addresses == null) {
  181. return null;
  182. } else {
  183. $addressString = '';
  184. foreach ($addresses as $address) {
  185. if (!empty($addressString)) {
  186. $addressString .= ', ';
  187. }
  188. if (Core::isWindows()) {
  189. $addressString .= $address['email'];
  190. } else {
  191. $addressString .= ($encode ? String::encode_mime_header($address['name']) : $address['name']) . ' <'.$address['email'].'>';
  192. }
  193. }
  194. return $addressString;
  195. }
  196. }
  197. /**
  198. * Return a string containing the recipients.
  199. * @param $encode boolean
  200. * @return string
  201. */
  202. function getRecipientString($encode = true) {
  203. return $this->getAddressArrayString($this->getRecipients(), $encode);
  204. }
  205. /**
  206. * Return a string containing the Cc recipients.
  207. * @param $encode boolean
  208. * @return string
  209. */
  210. function getCcString($encode = true) {
  211. return $this->getAddressArrayString($this->getCcs(), $encode);
  212. }
  213. /**
  214. * Return a string containing the Bcc recipients.
  215. * @param $encode boolean
  216. * @return string
  217. */
  218. function getBccString($encode = true) {
  219. return $this->getAddressArrayString($this->getBccs(), $encode);
  220. }
  221. /**
  222. * Send the email.
  223. * @return boolean
  224. */
  225. function send() {
  226. $recipients = $this->getRecipientString();
  227. $from = $this->getFromString();
  228. $subject = String::encode_mime_header($this->getSubject());
  229. $body = $this->getBody();
  230. if (Core::isWindows()) {
  231. // FIXME Is this correct?
  232. // Convert *nix-style linebreaks to DOS-style linebreaks
  233. $body = String::regexp_replace("/([^\r]|^)\n/", "\$1\r\n", $body);
  234. } else {
  235. // Convert DOS-style linebreaks to *nix
  236. $body = String::regexp_replace("/\r\n/", "\n", $body);
  237. }
  238. if ($this->getContentType() != null) {
  239. $this->addHeader('Content-Type', $this->getContentType());
  240. } elseif ($this->hasAttachments()) {
  241. // Only add MIME headers if sending an attachment
  242. $mimeBoundary = '==boundary_'.md5(microtime());
  243. /* Add MIME-Version and Content-Type as headers. */
  244. $this->addHeader('MIME-Version', '1.0');
  245. $this->addHeader('Content-Type', 'multipart/mixed; boundary="'.$mimeBoundary.'"');
  246. } else {
  247. $this->addHeader('Content-Type', 'text/plain; charset="'.Config::getVar('i18n', 'client_charset').'"');
  248. }
  249. /* Add $from, $ccs, and $bccs as headers. */
  250. if ($from != null) {
  251. $this->addHeader('From', $from);
  252. }
  253. $ccs = $this->getCcString();
  254. if ($ccs != null) {
  255. $this->addHeader('Cc', $ccs);
  256. }
  257. $bccs = $this->getBccString();
  258. if ($bccs != null) {
  259. $this->addHeader('Bcc', $bccs);
  260. }
  261. $headers = '';
  262. foreach ($this->getHeaders() as $header) {
  263. if (!empty($headers)) {
  264. $headers .= MAIL_EOL;
  265. }
  266. $headers .= $header['name'].': '.$header['content'];
  267. }
  268. if ($this->hasAttachments()) {
  269. // Add the body
  270. $mailBody = 'This message is in MIME format and requires a MIME-capable mail client to view.'.MAIL_EOL.MAIL_EOL;
  271. $mailBody .= '--'.$mimeBoundary.MAIL_EOL;
  272. $mailBody .= sprintf('Content-Type: text/plain; charset=%s', Config::getVar('i18n', 'client_charset')) . MAIL_EOL.MAIL_EOL;
  273. $mailBody .= wordwrap($body, MAIL_WRAP, MAIL_EOL).MAIL_EOL.MAIL_EOL;
  274. // Add the attachments
  275. $attachments = $this->getAttachments();
  276. foreach ($attachments as $attachment) {
  277. $mailBody .= '--'.$mimeBoundary.MAIL_EOL;
  278. $mailBody .= 'Content-Type: '.$attachment['content-type'].'; name="'.$attachment['filename'].'"'.MAIL_EOL;
  279. $mailBody .= 'Content-transfer-encoding: base64'.MAIL_EOL;
  280. $mailBody .= 'Content-disposition: '.$attachment['disposition'].MAIL_EOL.MAIL_EOL;
  281. $mailBody .= $attachment['content'].MAIL_EOL.MAIL_EOL;
  282. }
  283. $mailBody .= '--'.$mimeBoundary.'--';
  284. } else {
  285. // Just add the body
  286. $mailBody = wordwrap($body, MAIL_WRAP, MAIL_EOL);
  287. }
  288. if ($this->getEnvelopeSender() != null) {
  289. $additionalParameters = '-f ' . $this->getEnvelopeSender();
  290. } else {
  291. $additionalParameters = null;
  292. }
  293. return String::mail($recipients, $subject, $mailBody, $headers, $additionalParameters);
  294. }
  295. }
  296. ?>