PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/swiftmailer/classes/Swift/Mime/SimpleMessage.php

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