PageRenderTime 33ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zendframework/library/Zend/Mail/Message.php

https://bitbucket.org/pcelta/zf2
PHP | 538 lines | 255 code | 42 blank | 241 comment | 21 complexity | 9e3c6ff888999005c62c8465573ca3e4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Mail
  9. */
  10. namespace Zend\Mail;
  11. use Traversable;
  12. use Zend\Mime;
  13. /**
  14. * @category Zend
  15. * @package Zend_Mail
  16. */
  17. class Message
  18. {
  19. /**
  20. * Content of the message
  21. *
  22. * @var string|object
  23. */
  24. protected $body;
  25. /**
  26. * @var Headers
  27. */
  28. protected $headers;
  29. /**
  30. * Message encoding
  31. *
  32. * Used to determine whether or not to encode headers; defaults to ASCII.
  33. *
  34. * @var string
  35. */
  36. protected $encoding = 'ASCII';
  37. /**
  38. * Is the message valid?
  39. *
  40. * If we don't any From addresses, we're invalid, according to RFC2822.
  41. *
  42. * @return bool
  43. */
  44. public function isValid()
  45. {
  46. $from = $this->getFrom();
  47. if (!$from instanceof AddressList) {
  48. return false;
  49. }
  50. return (bool) count($from);
  51. }
  52. /**
  53. * Set the message encoding
  54. *
  55. * @param string $encoding
  56. * @return Message
  57. */
  58. public function setEncoding($encoding)
  59. {
  60. $this->encoding = $encoding;
  61. $this->getHeaders()->setEncoding($encoding);
  62. return $this;
  63. }
  64. /**
  65. * Get the message encoding
  66. *
  67. * @return string
  68. */
  69. public function getEncoding()
  70. {
  71. return $this->encoding;
  72. }
  73. /**
  74. * Compose headers
  75. *
  76. * @param Headers $headers
  77. * @return Message
  78. */
  79. public function setHeaders(Headers $headers)
  80. {
  81. $this->headers = $headers;
  82. $headers->setEncoding($this->getEncoding());
  83. return $this;
  84. }
  85. /**
  86. * Access headers collection
  87. *
  88. * Lazy-loads if not already attached.
  89. *
  90. * @return Headers
  91. */
  92. public function getHeaders()
  93. {
  94. if (null === $this->headers) {
  95. $this->setHeaders(new Headers());
  96. $date = Header\Date::fromString('Date: ' . date('r'));
  97. $this->headers->addHeader($date);
  98. }
  99. return $this->headers;
  100. }
  101. /**
  102. * Set (overwrite) From addresses
  103. *
  104. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList
  105. * @param string|null $name
  106. * @return Message
  107. */
  108. public function setFrom($emailOrAddressList, $name = null)
  109. {
  110. $this->clearHeaderByName('from');
  111. return $this->addFrom($emailOrAddressList, $name);
  112. }
  113. /**
  114. * Add a "From" address
  115. *
  116. * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList
  117. * @param string|null $name
  118. * @return Message
  119. */
  120. public function addFrom($emailOrAddressOrList, $name = null)
  121. {
  122. $addressList = $this->getFrom();
  123. $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__);
  124. return $this;
  125. }
  126. /**
  127. * Retrieve list of From senders
  128. *
  129. * @return AddressList
  130. */
  131. public function getFrom()
  132. {
  133. return $this->getAddressListFromHeader('from', __NAMESPACE__ . '\Header\From');
  134. }
  135. /**
  136. * Overwrite the address list in the To recipients
  137. *
  138. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList
  139. * @param null|string $name
  140. * @return Message
  141. */
  142. public function setTo($emailOrAddressList, $name = null)
  143. {
  144. $this->clearHeaderByName('to');
  145. return $this->addTo($emailOrAddressList, $name);
  146. }
  147. /**
  148. * Add one or more addresses to the To recipients
  149. *
  150. * Appends to the list.
  151. *
  152. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressOrList
  153. * @param null|string $name
  154. * @return Message
  155. */
  156. public function addTo($emailOrAddressOrList, $name = null)
  157. {
  158. $addressList = $this->getTo();
  159. $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__);
  160. return $this;
  161. }
  162. /**
  163. * Access the address list of the To header
  164. *
  165. * @return AddressList
  166. */
  167. public function getTo()
  168. {
  169. return $this->getAddressListFromHeader('to', __NAMESPACE__ . '\Header\To');
  170. }
  171. /**
  172. * Set (overwrite) CC addresses
  173. *
  174. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList
  175. * @param string|null $name
  176. * @return Message
  177. */
  178. public function setCc($emailOrAddressList, $name = null)
  179. {
  180. $this->clearHeaderByName('cc');
  181. return $this->addCc($emailOrAddressList, $name);
  182. }
  183. /**
  184. * Add a "Cc" address
  185. *
  186. * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList
  187. * @param string|null $name
  188. * @return Message
  189. */
  190. public function addCc($emailOrAddressOrList, $name = null)
  191. {
  192. $addressList = $this->getCc();
  193. $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__);
  194. return $this;
  195. }
  196. /**
  197. * Retrieve list of CC recipients
  198. *
  199. * @return AddressList
  200. */
  201. public function getCc()
  202. {
  203. return $this->getAddressListFromHeader('cc', __NAMESPACE__ . '\Header\Cc');
  204. }
  205. /**
  206. * Set (overwrite) BCC addresses
  207. *
  208. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList
  209. * @param string|null $name
  210. * @return Message
  211. */
  212. public function setBcc($emailOrAddressList, $name = null)
  213. {
  214. $this->clearHeaderByName('bcc');
  215. return $this->addBcc($emailOrAddressList, $name);
  216. }
  217. /**
  218. * Add a "Bcc" address
  219. *
  220. * @param string|Address|array|AddressList|Traversable $emailOrAddressOrList
  221. * @param string|null $name
  222. * @return Message
  223. */
  224. public function addBcc($emailOrAddressOrList, $name = null)
  225. {
  226. $addressList = $this->getBcc();
  227. $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__);
  228. return $this;
  229. }
  230. /**
  231. * Retrieve list of BCC recipients
  232. *
  233. * @return AddressList
  234. */
  235. public function getBcc()
  236. {
  237. return $this->getAddressListFromHeader('bcc', __NAMESPACE__ . '\Header\Bcc');
  238. }
  239. /**
  240. * Overwrite the address list in the Reply-To recipients
  241. *
  242. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressList
  243. * @param null|string $name
  244. * @return Message
  245. */
  246. public function setReplyTo($emailOrAddressList, $name = null)
  247. {
  248. $this->clearHeaderByName('reply-to');
  249. return $this->addReplyTo($emailOrAddressList, $name);
  250. }
  251. /**
  252. * Add one or more addresses to the Reply-To recipients
  253. *
  254. * Appends to the list.
  255. *
  256. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressOrList
  257. * @param null|string $name
  258. * @return Message
  259. */
  260. public function addReplyTo($emailOrAddressOrList, $name = null)
  261. {
  262. $addressList = $this->getReplyTo();
  263. $this->updateAddressList($addressList, $emailOrAddressOrList, $name, __METHOD__);
  264. return $this;
  265. }
  266. /**
  267. * Access the address list of the Reply-To header
  268. *
  269. * @return AddressList
  270. */
  271. public function getReplyTo()
  272. {
  273. return $this->getAddressListFromHeader('reply-to', __NAMESPACE__ . '\Header\ReplyTo');
  274. }
  275. /**
  276. * setSender
  277. *
  278. * @param mixed $emailOrAddress
  279. * @param mixed $name
  280. * @return Message
  281. */
  282. public function setSender($emailOrAddress, $name = null)
  283. {
  284. $header = $this->getHeaderByName('sender', __NAMESPACE__ . '\Header\Sender');
  285. $header->setAddress($emailOrAddress, $name);
  286. return $this;
  287. }
  288. /**
  289. * Retrieve the sender address, if any
  290. *
  291. * @return null|Address\AddressInterface
  292. */
  293. public function getSender()
  294. {
  295. $header = $this->getHeaderByName('sender', __NAMESPACE__ . '\Header\Sender');
  296. return $header->getAddress();
  297. }
  298. /**
  299. * Set the message subject header value
  300. *
  301. * @param string $subject
  302. * @return Message
  303. */
  304. public function setSubject($subject)
  305. {
  306. $headers = $this->getHeaders();
  307. if (!$headers->has('subject')) {
  308. $header = new Header\Subject();
  309. $headers->addHeader($header);
  310. } else {
  311. $header = $headers->get('subject');
  312. }
  313. $header->setSubject($subject);
  314. return $this;
  315. }
  316. /**
  317. * Get the message subject header value
  318. *
  319. * @return null|string
  320. */
  321. public function getSubject()
  322. {
  323. $headers = $this->getHeaders();
  324. if (!$headers->has('subject')) {
  325. return null;
  326. }
  327. $header = $headers->get('subject');
  328. return $header->getFieldValue();
  329. }
  330. /**
  331. * Set the message body
  332. *
  333. * @param null|string|\Zend\Mime\Message|object $body
  334. * @throws Exception\InvalidArgumentException
  335. * @return Message
  336. */
  337. public function setBody($body)
  338. {
  339. if (!is_string($body) && $body !== null) {
  340. if (!is_object($body)) {
  341. throw new Exception\InvalidArgumentException(sprintf(
  342. '%s expects a string or object argument; received "%s"',
  343. __METHOD__,
  344. gettype($body)
  345. ));
  346. }
  347. if (!$body instanceof Mime\Message) {
  348. if (!method_exists($body, '__toString')) {
  349. throw new Exception\InvalidArgumentException(sprintf(
  350. '%s expects object arguments of type Zend\Mime\Message or implementing __toString(); object of type "%s" received',
  351. __METHOD__,
  352. get_class($body)
  353. ));
  354. }
  355. }
  356. }
  357. $this->body = $body;
  358. if (!$this->body instanceof Mime\Message) {
  359. return $this;
  360. }
  361. // Get headers, and set Mime-Version header
  362. $headers = $this->getHeaders();
  363. $this->getHeaderByName('mime-version', __NAMESPACE__ . '\Header\MimeVersion');
  364. // Multipart content headers
  365. if ($this->body->isMultiPart()) {
  366. $mime = $this->body->getMime();
  367. $header = $this->getHeaderByName('content-type', __NAMESPACE__ . '\Header\ContentType');
  368. $header->setType('multipart/mixed');
  369. $header->addParameter('boundary', $mime->boundary());
  370. return $this;
  371. }
  372. // MIME single part headers
  373. $parts = $this->body->getParts();
  374. if (!empty($parts)) {
  375. $part = array_shift($parts);
  376. $headers->addHeaders($part->getHeadersArray());
  377. }
  378. return $this;
  379. }
  380. /**
  381. * Return the currently set message body
  382. *
  383. * @return object
  384. */
  385. public function getBody()
  386. {
  387. return $this->body;
  388. }
  389. /**
  390. * Get the string-serialized message body text
  391. *
  392. * @return string
  393. */
  394. public function getBodyText()
  395. {
  396. if ($this->body instanceof Mime\Message) {
  397. return $this->body->generateMessage(Headers::EOL);
  398. }
  399. return (string) $this->body;
  400. }
  401. /**
  402. * Retrieve a header by name
  403. *
  404. * If not found, instantiates one based on $headerClass.
  405. *
  406. * @param string $headerName
  407. * @param string $headerClass
  408. * @return \Zend\Mail\Header\HeaderInterface
  409. */
  410. protected function getHeaderByName($headerName, $headerClass)
  411. {
  412. $headers = $this->getHeaders();
  413. if ($headers->has($headerName)) {
  414. $header = $headers->get($headerName);
  415. } else {
  416. $header = new $headerClass();
  417. $headers->addHeader($header);
  418. }
  419. return $header;
  420. }
  421. /**
  422. * Clear a header by name
  423. *
  424. * @param string $headerName
  425. */
  426. protected function clearHeaderByName($headerName)
  427. {
  428. $this->getHeaders()->removeHeader($headerName);
  429. }
  430. /**
  431. * Retrieve the AddressList from a named header
  432. *
  433. * Used with To, From, Cc, Bcc, and ReplyTo headers. If the header does not
  434. * exist, instantiates it.
  435. *
  436. * @param string $headerName
  437. * @param string $headerClass
  438. * @throws Exception\DomainException
  439. * @return AddressList
  440. */
  441. protected function getAddressListFromHeader($headerName, $headerClass)
  442. {
  443. $header = $this->getHeaderByName($headerName, $headerClass);
  444. if (!$header instanceof Header\AbstractAddressList) {
  445. throw new Exception\DomainException(sprintf(
  446. 'Cannot grab address list from header of type "%s"; not an AbstractAddressList implementation',
  447. get_class($header)
  448. ));
  449. }
  450. return $header->getAddressList();
  451. }
  452. /**
  453. * Update an address list
  454. *
  455. * Proxied to this from addFrom, addTo, addCc, addBcc, and addReplyTo.
  456. *
  457. * @param AddressList $addressList
  458. * @param string|Address\AddressInterface|array|AddressList|Traversable $emailOrAddressOrList
  459. * @param null|string $name
  460. * @param string $callingMethod
  461. * @throws Exception\InvalidArgumentException
  462. */
  463. protected function updateAddressList(AddressList $addressList, $emailOrAddressOrList, $name, $callingMethod)
  464. {
  465. if ($emailOrAddressOrList instanceof Traversable) {
  466. foreach ($emailOrAddressOrList as $address) {
  467. $addressList->add($address);
  468. }
  469. return;
  470. }
  471. if (is_array($emailOrAddressOrList)) {
  472. $addressList->addMany($emailOrAddressOrList);
  473. return;
  474. }
  475. if (!is_string($emailOrAddressOrList) && !$emailOrAddressOrList instanceof Address\AddressInterface) {
  476. throw new Exception\InvalidArgumentException(sprintf(
  477. '%s expects a string, AddressInterface, array, AddressList, or Traversable as its first argument; received "%s"',
  478. $callingMethod,
  479. (is_object($emailOrAddressOrList) ? get_class($emailOrAddressOrList) : gettype($emailOrAddressOrList))
  480. ));
  481. }
  482. $addressList->add($emailOrAddressOrList, $name);
  483. }
  484. /**
  485. * Serialize to string
  486. *
  487. * @return string
  488. */
  489. public function toString()
  490. {
  491. $headers = $this->getHeaders();
  492. return $headers->toString()
  493. . Headers::EOL
  494. . $this->getBodyText();
  495. }
  496. }