Message.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Mail\Provider;
  8. /**
  9. * Mail Message Object
  10. *
  11. * This object is used to define a mail message that can be used to transfer data to a provider
  12. *
  13. * @since 30.0.0
  14. *
  15. */
  16. class Message implements \OCP\Mail\Provider\IMessage {
  17. /**
  18. * initialize the mail message object
  19. *
  20. * @since 30.0.0
  21. *
  22. * @param array $data message data array
  23. */
  24. public function __construct(
  25. protected array $data = [],
  26. ) {
  27. }
  28. /**
  29. * arbitrary unique text string identifying this message
  30. *
  31. * @since 30.0.0
  32. *
  33. * @return string id of this message
  34. */
  35. public function id(): string {
  36. // return id of message
  37. return (isset($this->data['id'])) ? $this->data['id'] : '';
  38. }
  39. /**
  40. * sets the sender of this message
  41. *
  42. * @since 30.0.0
  43. *
  44. * @param IAddress $value sender's mail address object
  45. *
  46. * @return self return this object for command chaining
  47. */
  48. public function setFrom(IAddress $value): self {
  49. // create or update field in data store with value
  50. $this->data['from'] = $value;
  51. // return this object for command chaining
  52. return $this;
  53. }
  54. /**
  55. * gets the sender of this message
  56. *
  57. * @since 30.0.0
  58. *
  59. * @return IAddress|null sender's mail address object
  60. */
  61. public function getFrom(): IAddress|null {
  62. // evaluate if data store field exists and return value(s) or null otherwise
  63. return (isset($this->data['from'])) ? $this->data['from'] : null;
  64. }
  65. /**
  66. * sets the sender's reply to address of this message
  67. *
  68. * @since 30.0.0
  69. *
  70. * @param IAddress $value senders's reply to mail address object
  71. *
  72. * @return self return this object for command chaining
  73. */
  74. public function setReplyTo(IAddress $value): self {
  75. // create or update field in data store with value
  76. $this->data['replyTo'] = $value;
  77. // return this object for command chaining
  78. return $this;
  79. }
  80. /**
  81. * gets the sender's reply to address of this message
  82. *
  83. * @since 30.0.0
  84. *
  85. * @return IAddress|null sender's reply to mail address object
  86. */
  87. public function getReplyTo(): IAddress|null {
  88. // evaluate if data store field exists and return value(s) or null otherwise
  89. return (isset($this->data['replyTo'])) ? $this->data['replyTo'] : null;
  90. }
  91. /**
  92. * sets the recipient(s) of this message
  93. *
  94. * @since 30.0.0
  95. *
  96. * @param IAddress ...$value collection of or one or more mail address objects
  97. *
  98. * @return self return this object for command chaining
  99. */
  100. public function setTo(IAddress ...$value): self {
  101. // create or update field in data store with value
  102. $this->data['to'] = $value;
  103. // return this object for command chaining
  104. return $this;
  105. }
  106. /**
  107. * gets the recipient(s) of this message
  108. *
  109. * @since 30.0.0
  110. *
  111. * @return array<int,IAddress> collection of all recipient mail address objects
  112. */
  113. public function getTo(): array {
  114. // evaluate if data store field exists and return value(s) or empty collection
  115. return (isset($this->data['to'])) ? $this->data['to'] : [];
  116. }
  117. /**
  118. * sets the copy to recipient(s) of this message
  119. *
  120. * @since 30.0.0
  121. *
  122. * @param IAddress ...$value collection of or one or more mail address objects
  123. *
  124. * @return self return this object for command chaining
  125. */
  126. public function setCc(IAddress ...$value): self {
  127. // create or update field in data store with value
  128. $this->data['cc'] = $value;
  129. // return this object for command chaining
  130. return $this;
  131. }
  132. /**
  133. * gets the copy to recipient(s) of this message
  134. *
  135. * @since 30.0.0
  136. *
  137. * @return array<int,IAddress> collection of all copied recipient mail address objects
  138. */
  139. public function getCc(): array {
  140. // evaluate if data store field exists and return value(s) or empty collection
  141. return (isset($this->data['cc'])) ? $this->data['cc'] : [];
  142. }
  143. /**
  144. * sets the blind copy to recipient(s) of this message
  145. *
  146. * @since 30.0.0
  147. *
  148. * @param IAddress ...$value collection of or one or more mail address objects
  149. *
  150. * @return self return this object for command chaining
  151. */
  152. public function setBcc(IAddress ...$value): self {
  153. // create or update field in data store with value
  154. $this->data['bcc'] = $value;
  155. // return this object for command chaining
  156. return $this;
  157. }
  158. /**
  159. * gets the blind copy to recipient(s) of this message
  160. *
  161. * @since 30.0.0
  162. *
  163. * @return array<int,IAddress> collection of all blind copied recipient mail address objects
  164. */
  165. public function getBcc(): array {
  166. // evaluate if data store field exists and return value(s) or empty collection
  167. return (isset($this->data['bcc'])) ? $this->data['bcc'] : [];
  168. }
  169. /**
  170. * sets the subject of this message
  171. *
  172. * @since 30.0.0
  173. *
  174. * @param string $value subject of mail message
  175. *
  176. * @return self return this object for command chaining
  177. */
  178. public function setSubject(string $value): self {
  179. // create or update field in data store with value
  180. $this->data['subject'] = $value;
  181. // return this object for command chaining
  182. return $this;
  183. }
  184. /**
  185. * gets the subject of this message
  186. *
  187. * @since 30.0.0
  188. *
  189. * @return string|null subject of message or null if one is not set
  190. */
  191. public function getSubject(): string|null {
  192. // evaluate if data store field exists and return value(s) or null otherwise
  193. return (isset($this->data['subject'])) ? $this->data['subject'] : null;
  194. }
  195. /**
  196. * sets the plain text or html body of this message
  197. *
  198. * @since 30.0.0
  199. *
  200. * @param string $value text or html body of message
  201. * @param bool $html html flag - true for html
  202. *
  203. * @return self return this object for command chaining
  204. */
  205. public function setBody(string $value, bool $html = false): self {
  206. // evaluate html flag and create or update appropriate field in data store with value
  207. if ($html) {
  208. $this->data['bodyHtml'] = $value;
  209. } else {
  210. $this->data['bodyPlain'] = $value;
  211. }
  212. // return this object for command chaining
  213. return $this;
  214. }
  215. /**
  216. * gets either the html or plain text body of this message
  217. *
  218. * html body will be returned over plain text if html body exists
  219. *
  220. * @since 30.0.0
  221. *
  222. * @return string|null html/plain body of this message or null if one is not set
  223. */
  224. public function getBody(): string|null {
  225. // evaluate if data store field(s) exists and return value
  226. if (isset($this->data['bodyHtml'])) {
  227. return $this->data['bodyHtml'];
  228. } elseif (isset($this->data['bodyPlain'])) {
  229. return $this->data['bodyPlain'];
  230. }
  231. // return null if data fields did not exist in data store
  232. return null;
  233. }
  234. /**
  235. * sets the html body of this message
  236. *
  237. * @since 30.0.0
  238. *
  239. * @param string $value html body of message
  240. *
  241. * @return self return this object for command chaining
  242. */
  243. public function setBodyHtml(string $value): self {
  244. // create or update field in data store with value
  245. $this->data['bodyHtml'] = $value;
  246. // return this object for command chaining
  247. return $this;
  248. }
  249. /**
  250. * gets the html body of this message
  251. *
  252. * @since 30.0.0
  253. *
  254. * @return string|null html body of this message or null if one is not set
  255. */
  256. public function getBodyHtml(): string|null {
  257. // evaluate if data store field exists and return value(s) or null otherwise
  258. return (isset($this->data['bodyHtml'])) ? $this->data['bodyHtml'] : null;
  259. }
  260. /**
  261. * sets the plain text body of this message
  262. *
  263. * @since 30.0.0
  264. *
  265. * @param string $value plain text body of message
  266. *
  267. * @return self return this object for command chaining
  268. */
  269. public function setBodyPlain(string $value): self {
  270. // create or update field in data store with value
  271. $this->data['bodyPlain'] = $value;
  272. // return this object for command chaining
  273. return $this;
  274. }
  275. /**
  276. * gets the plain text body of this message
  277. *
  278. * @since 30.0.0
  279. *
  280. * @return string|null plain text body of this message or null if one is not set
  281. */
  282. public function getBodyPlain(): string|null {
  283. // evaluate if data store field exists and return value(s) or null otherwise
  284. return (isset($this->data['bodyPlain'])) ? $this->data['bodyPlain'] : null;
  285. }
  286. /**
  287. * sets the attachments of this message
  288. *
  289. * @since 30.0.0
  290. *
  291. * @param IAttachment ...$value collection of or one or more mail attachment objects
  292. *
  293. * @return self return this object for command chaining
  294. */
  295. public function setAttachments(IAttachment ...$value): self {
  296. // create or update field in data store with value
  297. $this->data['attachments'] = $value;
  298. // return this object for command chaining
  299. return $this;
  300. }
  301. /**
  302. * gets the attachments of this message
  303. *
  304. * @since 30.0.0
  305. *
  306. * @return array<int,IAttachment> collection of all mail attachment objects
  307. */
  308. public function getAttachments(): array {
  309. // evaluate if data store field exists and return value(s) or null otherwise
  310. return (isset($this->data['attachments'])) ? $this->data['attachments'] : [];
  311. }
  312. }