Update copyright
[friendica.git/.git] / src / Object / Email.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Object;
23
24 use Friendica\Object\EMail\IEmail;
25
26 /**
27  * The default implementation of the IEmail interface
28  *
29  * Provides the possibility to reuse the email instance with new recipients (@see Email::withRecipient())
30  */
31 class Email implements IEmail
32 {
33         /** @var string */
34         private $fromName;
35         /** @var string */
36         private $fromAddress;
37         /** @var string */
38         private $replyTo;
39
40         /** @var string */
41         private $toAddress;
42
43         /** @var string */
44         private $subject;
45         /** @var string|null */
46         private $msgHtml;
47         /** @var string */
48         private $msgText;
49
50         /** @var string[][] */
51         private $additionalMailHeader;
52         /** @var int|null */
53         private $toUid;
54
55         public function __construct(string $fromName, string $fromAddress, string $replyTo, string $toAddress,
56                                     string $subject, string $msgHtml, string $msgText,
57                                     array $additionalMailHeader = [], int $toUid = null)
58         {
59                 $this->fromName             = $fromName;
60                 $this->fromAddress          = $fromAddress;
61                 $this->replyTo              = $replyTo;
62                 $this->toAddress            = $toAddress;
63                 $this->subject              = $subject;
64                 $this->msgHtml              = $msgHtml;
65                 $this->msgText              = $msgText;
66                 $this->additionalMailHeader = $additionalMailHeader;
67                 $this->toUid                = $toUid;
68         }
69
70         /**
71          * {@inheritDoc}
72          */
73         public function getFromName()
74         {
75                 return $this->fromName;
76         }
77
78         /**
79          * {@inheritDoc}
80          */
81         public function getFromAddress()
82         {
83                 return $this->fromAddress;
84         }
85
86         /**
87          * {@inheritDoc}
88          */
89         public function getReplyTo()
90         {
91                 return $this->replyTo;
92         }
93
94         /**
95          * {@inheritDoc}
96          */
97         public function getToAddress()
98         {
99                 return $this->toAddress;
100         }
101
102         /**
103          * {@inheritDoc}
104          */
105         public function getSubject()
106         {
107                 return $this->subject;
108         }
109
110         /**
111          * {@inheritDoc}
112          */
113         public function getMessage(bool $plain = false)
114         {
115                 if ($plain) {
116                         return $this->msgText;
117                 } else {
118                         return $this->msgHtml;
119                 }
120         }
121
122         /**
123          * {@inheritDoc}
124          */
125         public function getAdditionalMailHeader()
126         {
127                 return $this->additionalMailHeader;
128         }
129
130         /**
131          * {@inheritDoc}
132          */
133         public function getAdditionalMailHeaderString()
134         {
135                 $headerString = '';
136
137                 foreach ($this->additionalMailHeader as $name => $values) {
138                         if (is_array($values)) {
139                                 foreach ($values as $value) {
140                                         $headerString .= "$name: $value\n";
141                                 }
142                         } else {
143                                 $headerString .= "$name: $values\n";
144                         }
145                 }
146                 return $headerString;
147         }
148
149         /**
150          * {@inheritDoc}
151          */
152         public function getRecipientUid()
153         {
154                 return $this->toUid;
155         }
156
157         /**
158          * {@inheritDoc}
159          */
160         public function withRecipient(string $address, int $uid = null)
161         {
162                 $newEmail            = clone $this;
163                 $newEmail->toAddress = $address;
164                 $newEmail->toUid     = $uid;
165
166                 return $newEmail;
167         }
168
169         /**
170          * {@inheritDoc}
171          */
172         public function withMessage(string $plaintext, string $html = null)
173         {
174                 $newMail          = clone $this;
175                 $newMail->msgText = $plaintext;
176                 $newMail->msgHtml = $html;
177
178                 return $newMail;
179         }
180
181         /**
182          * Returns the properties of the email as an array
183          *
184          * @return array
185          */
186         private function toArray()
187         {
188                 return get_object_vars($this);
189         }
190
191         /**
192          * @inheritDoc
193          */
194         public function __toString()
195         {
196                 return json_encode($this->toArray());
197         }
198
199         /**
200          * @inheritDoc
201          */
202         public function jsonSerialize()
203         {
204                 return $this->toArray();
205         }
206 }