/src-qt5/desktop-utils/lumina-textedit/tests/test.php

https://github.com/trueos/lumina · PHP · 224 lines · 127 code · 23 blank · 74 comment · 6 complexity · 00e9ab382a429f699c25a500e8f773cd MD5 · raw file

  1. <?php
  2. class Mail
  3. {
  4. private $from = ['name' => '', 'email' => ''];
  5. private $to = [];
  6. private $subject = '';
  7. private $message = '';
  8. private $files = [];
  9. private $multipart = false;
  10. private $boundary = '';
  11. private $uniqId = '';
  12. private $replyTo = [];
  13. private $timestamp = null;
  14. const CRLF = "\r\n";
  15. public function __construct()
  16. {
  17. $this->uniqId = '<php-mail-' . md5(microtime()) . mt_rand() . '@git.php.net>';
  18. }
  19. /**
  20. * Return unique id of mail
  21. * @return string unique Id of message in format: '<php-mail-...@git.php.net';
  22. */
  23. public function getId()
  24. {
  25. return $this->uniqId;
  26. }
  27. /**
  28. * Add parent mail for this mail
  29. * @param string $uniqId unique Id of message in format: '<php-mail-...@git.php.net';
  30. */
  31. public function addReplyTo($uniqId)
  32. {
  33. $this->replyTo[] = $uniqId;
  34. }
  35. /**
  36. * Add attached text file to mail
  37. * @param string $name unique file name
  38. * @param string $data file content
  39. */
  40. public function addTextFile($name , $data)
  41. {
  42. $this->files[trim($name)] = chunk_split(base64_encode($data), 76, self::CRLF);
  43. }
  44. /**
  45. * Return length of attached file
  46. * @param string $name unique file name
  47. * @return int file length
  48. */
  49. public function getFileLength($name)
  50. {
  51. $name = trim($name);
  52. return isset($this->files[$name]) ? strlen($this->files[$name]) : 0;
  53. }
  54. /**
  55. * Delete attached file
  56. * @param string $name unique file name
  57. */
  58. public function dropFile($name)
  59. {
  60. $name = trim($name);
  61. unset($this->files[$name]);
  62. }
  63. /**
  64. * Set "From" address
  65. * @param string $email email author address
  66. * @param string $name author name
  67. */
  68. public function setFrom($email, $name = '')
  69. {
  70. $this->from = ['email' => trim($email), 'name' => trim($name)];
  71. }
  72. /**
  73. * Add recipient address
  74. * @param string $email recipient address
  75. * @param string $name recipient name
  76. */
  77. public function addTo($email, $name = '')
  78. {
  79. $this->to[] = ['email' => trim($email), 'name' => trim($name)];
  80. }
  81. /**
  82. * Set mail subject
  83. * @param string $subject subject
  84. */
  85. public function setSubject($subject)
  86. {
  87. $this->subject = trim($subject);
  88. }
  89. /**
  90. * Set timestamp
  91. * @param string $timestamp timestamp
  92. */
  93. public function setTimestamp($timestamp)
  94. {
  95. $this->timestamp = trim($timestamp);
  96. }
  97. /**
  98. * Set mail body text
  99. * @param string $message body text
  100. */
  101. public function setMessage($message)
  102. {
  103. $this->message = $message;
  104. }
  105. /**
  106. * Format header string
  107. * @param string $name header name
  108. * @param string $value header value
  109. * @return string header string
  110. */
  111. private function makeHeader($name, $value)
  112. {
  113. return $name . ': ' . $value;
  114. }
  115. /**
  116. * Format address string
  117. * @param array $address array with email adress and name
  118. * @return string address string
  119. */
  120. private function makeAddress(array $address)
  121. {
  122. return $address['name'] ? $this->utf8SafeEncode($address['name'], 100) . ' <'. $address['email'] . '>' : $address['email'];
  123. }
  124. /**
  125. * Cut end encode string by mb_encode_mimeheader
  126. * @param string $value utf8 string
  127. * @param int $maxLenght max length
  128. * @return string encoded string
  129. */
  130. private function utf8SafeEncode($value, $maxLenght = null)
  131. {
  132. if ($maxLenght) $value = mb_substr($value, 0, $maxLenght);
  133. return mb_encode_mimeheader($value, 'UTF-8', 'Q');
  134. }
  135. /**
  136. * Prepare heade part of mail
  137. * @return string header part of mail
  138. */
  139. private function makeHeaders()
  140. {
  141. $headers = [];
  142. $headers[] = $this->makeHeader('From', $this->makeAddress($this->from));
  143. $headers[] = $this->makeHeader('Message-ID', $this->uniqId);
  144. if (count($this->replyTo)) {
  145. $replyTo = implode(' ', $this->replyTo);
  146. $headers[] = $this->makeHeader('References', $replyTo);
  147. $headers[] = $this->makeHeader('In-Reply-To', $replyTo);
  148. }
  149. $headers[] = $this->makeHeader('MIME-Version', '1.0');
  150. $headers[] = $this->makeHeader('Date', date(DATE_RFC2822, $this->timestamp ?: time()));
  151. if ($this->multipart) {
  152. $this->boundary = sha1($this->uniqId);
  153. $headers[] = $this->makeHeader('Content-Type', 'multipart/mixed; boundary="' . $this->boundary . '"');
  154. } else {
  155. $headers[] = $this->makeHeader('Content-Type', 'text/plain; charset="utf-8"');
  156. // we use base64 for avoiding some problems sush string length limit, safety encoding etc.
  157. $headers[] = $this->makeHeader('Content-Transfer-Encoding', 'quoted-printable');
  158. }
  159. return implode(self::CRLF , $headers);
  160. }
  161. /**
  162. * Prepare body part of mail
  163. * @return string mail body
  164. */
  165. private function makeBody()
  166. {
  167. $body = '';
  168. if ($this->multipart) {
  169. $body .= '--' . $this->boundary . self::CRLF;
  170. $body .= $this->makeHeader('Content-Type', 'text/plain; charset="utf-8"') . self::CRLF;
  171. $body .= $this->makeHeader('Content-Transfer-Encoding', 'quoted-printable') . self::CRLF;
  172. $body .= self::CRLF;
  173. $body .= quoted_printable_encode($this->message);
  174. foreach ($this->files as $name => $data) {
  175. $body .= self::CRLF . '--' . $this->boundary . self::CRLF;
  176. $body .= $this->makeHeader('Content-Type', 'text/plain; charset="utf-8"') . self::CRLF;
  177. $body .= $this->makeHeader('Content-Transfer-Encoding', 'base64') . self::CRLF;
  178. $body .= $this->makeHeader('Content-Disposition', 'attachment; filename="' . $name . '"') . self::CRLF;
  179. $body .= self::CRLF;
  180. $body .= $data;
  181. }
  182. $body .= self::CRLF . '--' . $this->boundary . '--';
  183. } else {
  184. $body = quoted_printable_encode($this->message);
  185. }
  186. return $body;
  187. }
  188. /**
  189. * Send current mail
  190. * @return bool
  191. */
  192. public function send()
  193. {
  194. $this->multipart = (bool) count($this->files);
  195. $receivers = implode(', ', array_map([$this, 'makeAddress'], $this->to));
  196. $subject = $this->utf8SafeEncode($this->subject, 450);
  197. $headers = $this->makeHeaders();
  198. $body = $this->makeBody();
  199. return mail($receivers, $subject, $body, $headers, "-f noreply@php.net");
  200. }
  201. }