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

/vendor/swift/classes/Swift/Mime/SimpleMessage.php

https://github.com/popovag/kohana_email
PHP | 607 lines | 323 code | 49 blank | 235 comment | 32 complexity | 42f52acc5897a3acfe067b4360f47011 MD5 | raw file
  1. <?php
  2. /*
  3. The default Message class Swift Mailer.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //@require 'Swift/Mime/Message.php';
  16. //@require 'Swift/Mime/MimePart.php';
  17. //@require 'Swift/Mime/MimeEntity.php';
  18. //@require 'Swift/Mime/HeaderSet.php';
  19. //@require 'Swift/Mime/ContentEncoder.php';
  20. /**
  21. * The default email message class.
  22. * @package Swift
  23. * @subpackage Mime
  24. * @author Chris Corbyn
  25. */
  26. class Swift_Mime_SimpleMessage extends Swift_Mime_MimePart
  27. implements Swift_Mime_Message
  28. {
  29. /**
  30. * Create a new SimpleMessage with $headers, $encoder and $cache.
  31. * @param Swift_Mime_HeaderSet $headers
  32. * @param Swift_Mime_ContentEncoder $encoder
  33. * @param Swift_KeyCache $cache
  34. * @param string $charset
  35. */
  36. public function __construct(Swift_Mime_HeaderSet $headers,
  37. Swift_Mime_ContentEncoder $encoder, Swift_KeyCache $cache, $charset = null)
  38. {
  39. parent::__construct($headers, $encoder, $cache, $charset);
  40. $this->getHeaders()->defineOrdering(array(
  41. 'Return-Path',
  42. 'Sender',
  43. 'Message-ID',
  44. 'Date',
  45. 'Subject',
  46. 'From',
  47. 'Reply-To',
  48. 'To',
  49. 'Cc',
  50. 'Bcc',
  51. 'MIME-Version',
  52. 'Content-Type',
  53. 'Content-Transfer-Encoding'
  54. ));
  55. $this->getHeaders()->setAlwaysDisplayed(
  56. array('Date', 'Message-ID', 'From')
  57. );
  58. $this->getHeaders()->addTextHeader('MIME-Version', '1.0');
  59. $this->setDate(time());
  60. $this->setId($this->getId());
  61. $this->getHeaders()->addMailboxHeader('From');
  62. }
  63. /**
  64. * Always returns {@link LEVEL_TOP} for a message instance.
  65. * @return int
  66. */
  67. public function getNestingLevel()
  68. {
  69. return self::LEVEL_TOP;
  70. }
  71. /**
  72. * Set the subject of this message.
  73. * @param string $subject
  74. */
  75. public function setSubject($subject)
  76. {
  77. if (!$this->_setHeaderFieldModel('Subject', $subject))
  78. {
  79. $this->getHeaders()->addTextHeader('Subject', $subject);
  80. }
  81. return $this;
  82. }
  83. /**
  84. * Get the subject of this message.
  85. * @return string
  86. */
  87. public function getSubject()
  88. {
  89. return $this->_getHeaderFieldModel('Subject');
  90. }
  91. /**
  92. * Set the date at which this message was created.
  93. * @param int $date
  94. */
  95. public function setDate($date)
  96. {
  97. if (!$this->_setHeaderFieldModel('Date', $date))
  98. {
  99. $this->getHeaders()->addDateHeader('Date', $date);
  100. }
  101. return $this;
  102. }
  103. /**
  104. * Get the date at which this message was created.
  105. * @return int
  106. */
  107. public function getDate()
  108. {
  109. return $this->_getHeaderFieldModel('Date');
  110. }
  111. /**
  112. * Set the return-path (the bounce address) of this message.
  113. * @param string $address
  114. */
  115. public function setReturnPath($address)
  116. {
  117. if (!$this->_setHeaderFieldModel('Return-Path', $address))
  118. {
  119. $this->getHeaders()->addPathHeader('Return-Path', $address);
  120. }
  121. return $this;
  122. }
  123. /**
  124. * Get the return-path (bounce address) of this message.
  125. * @return string
  126. */
  127. public function getReturnPath()
  128. {
  129. return $this->_getHeaderFieldModel('Return-Path');
  130. }
  131. /**
  132. * Set the sender of this message.
  133. * This does not override the From field, but it has a higher significance.
  134. * @param string $sender
  135. * @param string $name optional
  136. */
  137. public function setSender($address, $name = null)
  138. {
  139. if (!is_array($address) && isset($name))
  140. {
  141. $address = array($address => $name);
  142. }
  143. if (!$this->_setHeaderFieldModel('Sender', (array) $address))
  144. {
  145. $this->getHeaders()->addMailboxHeader('Sender', (array) $address);
  146. }
  147. return $this;
  148. }
  149. /**
  150. * Get the sender of this message.
  151. * @return string
  152. */
  153. public function getSender()
  154. {
  155. return $this->_getHeaderFieldModel('Sender');
  156. }
  157. /**
  158. * Add a From: address to this message.
  159. *
  160. * If $name is passed this name will be associated with the address.
  161. *
  162. * @param string $address
  163. * @param string $name optional
  164. */
  165. public function addFrom($address, $name = null)
  166. {
  167. $current = $this->getFrom();
  168. $current[$address] = $name;
  169. return $this->setFrom($current);
  170. }
  171. /**
  172. * Set the from address of this message.
  173. *
  174. * You may pass an array of addresses if this message is from multiple people.
  175. *
  176. * If $name is passed and the first parameter is a string, this name will be
  177. * associated with the address.
  178. *
  179. * @param string $addresses
  180. * @param string $name optional
  181. */
  182. public function setFrom($addresses, $name = null)
  183. {
  184. if (!is_array($addresses) && isset($name))
  185. {
  186. $addresses = array($addresses => $name);
  187. }
  188. if (!$this->_setHeaderFieldModel('From', (array) $addresses))
  189. {
  190. $this->getHeaders()->addMailboxHeader('From', (array) $addresses);
  191. }
  192. return $this;
  193. }
  194. /**
  195. * Get the from address of this message.
  196. *
  197. * @return string
  198. */
  199. public function getFrom()
  200. {
  201. return $this->_getHeaderFieldModel('From');
  202. }
  203. /**
  204. * Add a Reply-To: address to this message.
  205. *
  206. * If $name is passed this name will be associated with the address.
  207. *
  208. * @param string $address
  209. * @param string $name optional
  210. */
  211. public function addReplyTo($address, $name = null)
  212. {
  213. $current = $this->getReplyTo();
  214. $current[$address] = $name;
  215. return $this->setReplyTo($current);
  216. }
  217. /**
  218. * Set the reply-to address of this message.
  219. *
  220. * You may pass an array of addresses if replies will go to multiple people.
  221. *
  222. * If $name is passed and the first parameter is a string, this name will be
  223. * associated with the address.
  224. *
  225. * @param string $addresses
  226. * @param string $name optional
  227. */
  228. public function setReplyTo($addresses, $name = null)
  229. {
  230. if (!is_array($addresses) && isset($name))
  231. {
  232. $addresses = array($addresses => $name);
  233. }
  234. if (!$this->_setHeaderFieldModel('Reply-To', (array) $addresses))
  235. {
  236. $this->getHeaders()->addMailboxHeader('Reply-To', (array) $addresses);
  237. }
  238. return $this;
  239. }
  240. /**
  241. * Get the reply-to address of this message.
  242. *
  243. * @return string
  244. */
  245. public function getReplyTo()
  246. {
  247. return $this->_getHeaderFieldModel('Reply-To');
  248. }
  249. /**
  250. * Add a To: address to this message.
  251. *
  252. * If $name is passed this name will be associated with the address.
  253. *
  254. * @param string $address
  255. * @param string $name optional
  256. */
  257. public function addTo($address, $name = null)
  258. {
  259. $current = $this->getTo();
  260. $current[$address] = $name;
  261. return $this->setTo($current);
  262. }
  263. /**
  264. * Set the to addresses of this message.
  265. *
  266. * If multiple recipients will receive the message and array should be used.
  267. *
  268. * If $name is passed and the first parameter is a string, this name will be
  269. * associated with the address.
  270. *
  271. * @param array $addresses
  272. * @param string $name optional
  273. */
  274. public function setTo($addresses, $name = null)
  275. {
  276. if (!is_array($addresses) && isset($name))
  277. {
  278. $addresses = array($addresses => $name);
  279. }
  280. if (!$this->_setHeaderFieldModel('To', (array) $addresses))
  281. {
  282. $this->getHeaders()->addMailboxHeader('To', (array) $addresses);
  283. }
  284. return $this;
  285. }
  286. /**
  287. * Get the To addresses of this message.
  288. *
  289. * @return array
  290. */
  291. public function getTo()
  292. {
  293. return $this->_getHeaderFieldModel('To');
  294. }
  295. /**
  296. * Add a Cc: address to this message.
  297. *
  298. * If $name is passed this name will be associated with the address.
  299. *
  300. * @param string $address
  301. * @param string $name optional
  302. */
  303. public function addCc($address, $name = null)
  304. {
  305. $current = $this->getCc();
  306. $current[$address] = $name;
  307. return $this->setCc($current);
  308. }
  309. /**
  310. * Set the Cc addresses of this message.
  311. *
  312. * If $name is passed and the first parameter is a string, this name will be
  313. * associated with the address.
  314. *
  315. * @param array $addresses
  316. * @param string $name optional
  317. */
  318. public function setCc($addresses, $name = null)
  319. {
  320. if (!is_array($addresses) && isset($name))
  321. {
  322. $addresses = array($addresses => $name);
  323. }
  324. if (!$this->_setHeaderFieldModel('Cc', (array) $addresses))
  325. {
  326. $this->getHeaders()->addMailboxHeader('Cc', (array) $addresses);
  327. }
  328. return $this;
  329. }
  330. /**
  331. * Get the Cc address of this message.
  332. *
  333. * @return array
  334. */
  335. public function getCc()
  336. {
  337. return $this->_getHeaderFieldModel('Cc');
  338. }
  339. /**
  340. * Add a Bcc: address to this message.
  341. *
  342. * If $name is passed this name will be associated with the address.
  343. *
  344. * @param string $address
  345. * @param string $name optional
  346. */
  347. public function addBcc($address, $name = null)
  348. {
  349. $current = $this->getBcc();
  350. $current[$address] = $name;
  351. return $this->setBcc($current);
  352. }
  353. /**
  354. * Set the Bcc addresses of this message.
  355. *
  356. * If $name is passed and the first parameter is a string, this name will be
  357. * associated with the address.
  358. *
  359. * @param array $addresses
  360. * @param string $name optional
  361. */
  362. public function setBcc($addresses, $name = null)
  363. {
  364. if (!is_array($addresses) && isset($name))
  365. {
  366. $addresses = array($addresses => $name);
  367. }
  368. if (!$this->_setHeaderFieldModel('Bcc', (array) $addresses))
  369. {
  370. $this->getHeaders()->addMailboxHeader('Bcc', (array) $addresses);
  371. }
  372. return $this;
  373. }
  374. /**
  375. * Get the Bcc addresses of this message.
  376. *
  377. * @return array
  378. */
  379. public function getBcc()
  380. {
  381. return $this->_getHeaderFieldModel('Bcc');
  382. }
  383. /**
  384. * Set the priority of this message.
  385. * The value is an integer where 1 is the highest priority and 5 is the lowest.
  386. * @param int $priority
  387. */
  388. public function setPriority($priority)
  389. {
  390. $priorityMap = array(
  391. 1 => 'Highest',
  392. 2 => 'High',
  393. 3 => 'Normal',
  394. 4 => 'Low',
  395. 5 => 'Lowest'
  396. );
  397. $pMapKeys = array_keys($priorityMap);
  398. if ($priority > max($pMapKeys))
  399. {
  400. $priority = max($pMapKeys);
  401. }
  402. elseif ($priority < min($pMapKeys))
  403. {
  404. $priority = min($pMapKeys);
  405. }
  406. if (!$this->_setHeaderFieldModel('X-Priority',
  407. sprintf('%d (%s)', $priority, $priorityMap[$priority])))
  408. {
  409. $this->getHeaders()->addTextHeader('X-Priority',
  410. sprintf('%d (%s)', $priority, $priorityMap[$priority]));
  411. }
  412. return $this;
  413. }
  414. /**
  415. * Get the priority of this message.
  416. * The returned value is an integer where 1 is the highest priority and 5
  417. * is the lowest.
  418. * @return int
  419. */
  420. public function getPriority()
  421. {
  422. list($priority) = sscanf($this->_getHeaderFieldModel('X-Priority'),
  423. '%[1-5]'
  424. );
  425. return isset($priority) ? $priority : 3;
  426. }
  427. /**
  428. * Ask for a delivery receipt from the recipient to be sent to $addresses
  429. * @param array $addresses
  430. */
  431. public function setReadReceiptTo($addresses)
  432. {
  433. if (!$this->_setHeaderFieldModel('Disposition-Notification-To', $addresses))
  434. {
  435. $this->getHeaders()
  436. ->addMailboxHeader('Disposition-Notification-To', $addresses);
  437. }
  438. return $this;
  439. }
  440. /**
  441. * Get the addresses to which a read-receipt will be sent.
  442. * @return string
  443. */
  444. public function getReadReceiptTo()
  445. {
  446. return $this->_getHeaderFieldModel('Disposition-Notification-To');
  447. }
  448. /**
  449. * Attach a {@link Swift_Mime_MimeEntity} such as an Attachment or MimePart.
  450. * @param Swift_Mime_MimeEntity $entity
  451. */
  452. public function attach(Swift_Mime_MimeEntity $entity)
  453. {
  454. $this->setChildren(array_merge($this->getChildren(), array($entity)));
  455. return $this;
  456. }
  457. /**
  458. * Remove an already attached entity.
  459. * @param Swift_Mime_MimeEntity $entity
  460. */
  461. public function detach(Swift_Mime_MimeEntity $entity)
  462. {
  463. $newChildren = array();
  464. foreach ($this->getChildren() as $child)
  465. {
  466. if ($entity !== $child)
  467. {
  468. $newChildren[] = $child;
  469. }
  470. }
  471. $this->setChildren($newChildren);
  472. return $this;
  473. }
  474. /**
  475. * Attach a {@link Swift_Mime_MimeEntity} and return it's CID source.
  476. * This method should be used when embedding images or other data in a message.
  477. * @param Swift_Mime_MimeEntity $entity
  478. * @return string
  479. */
  480. public function embed(Swift_Mime_MimeEntity $entity)
  481. {
  482. $this->attach($entity);
  483. return 'cid:' . $entity->getId();
  484. }
  485. /**
  486. * Get this message as a complete string.
  487. * @return string
  488. */
  489. public function toString()
  490. {
  491. if (count($children = $this->getChildren()) > 0 && $this->getBody() != '')
  492. {
  493. $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
  494. $string = parent::toString();
  495. $this->setChildren($children);
  496. }
  497. else
  498. {
  499. $string = parent::toString();
  500. }
  501. return $string;
  502. }
  503. /**
  504. * Write this message to a {@link Swift_InputByteStream}.
  505. * @param Swift_InputByteStream $is
  506. */
  507. public function toByteStream(Swift_InputByteStream $is)
  508. {
  509. if (count($children = $this->getChildren()) > 0 && $this->getBody() != '')
  510. {
  511. $this->setChildren(array_merge(array($this->_becomeMimePart()), $children));
  512. parent::toByteStream($is);
  513. $this->setChildren($children);
  514. }
  515. else
  516. {
  517. parent::toByteStream($is);
  518. }
  519. }
  520. // -- Protected methods
  521. /** @see Swift_Mime_SimpleMimeEntity::_getIdField() */
  522. protected function _getIdField()
  523. {
  524. return 'Message-ID';
  525. }
  526. // -- Private methods
  527. /** Turn the body of this message into a child of itself if needed */
  528. private function _becomeMimePart()
  529. {
  530. $part = new parent($this->getHeaders()->newInstance(), $this->getEncoder(),
  531. $this->_getCache(), $this->_userCharset
  532. );
  533. $part->setContentType($this->_userContentType);
  534. $part->setBody($this->getBody());
  535. $part->setFormat($this->_userFormat);
  536. $part->setDelSp($this->_userDelSp);
  537. $part->_setNestingLevel($this->_getTopNestingLevel());
  538. return $part;
  539. }
  540. /** Get the highest nesting level nested inside this message */
  541. private function _getTopNestingLevel()
  542. {
  543. $highestLevel = $this->getNestingLevel();
  544. foreach ($this->getChildren() as $child)
  545. {
  546. $childLevel = $child->getNestingLevel();
  547. if ($highestLevel < $childLevel)
  548. {
  549. $highestLevel = $childLevel;
  550. }
  551. }
  552. return $highestLevel;
  553. }
  554. }