]> git.mxchange.org Git - friendica.git/blob - src/Object/Email.php
Merge branch 'develop' into show_image_upload_limit
[friendica.git] / src / Object / Email.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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): string
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                                 $values = [$values];
140                         }
141
142                         foreach ($values as $value) {
143                                 $headerString .= "$name: $value\r\n";
144                         }
145                 }
146
147                 return $headerString;
148         }
149
150         /**
151          * {@inheritDoc}
152          */
153         public function getRecipientUid()
154         {
155                 return $this->toUid;
156         }
157
158         /**
159          * {@inheritDoc}
160          */
161         public function withRecipient(string $address, int $uid = null)
162         {
163                 $newEmail            = clone $this;
164                 $newEmail->toAddress = $address;
165                 $newEmail->toUid     = $uid;
166
167                 return $newEmail;
168         }
169
170         /**
171          * {@inheritDoc}
172          */
173         public function withMessage(string $plaintext, string $html = null)
174         {
175                 $newMail          = clone $this;
176                 $newMail->msgText = $plaintext;
177                 $newMail->msgHtml = $html;
178
179                 return $newMail;
180         }
181
182         /**
183          * Returns the properties of the email as an array
184          *
185          * @return array
186          */
187         private function toArray()
188         {
189                 return get_object_vars($this);
190         }
191
192         /**
193          * @inheritDoc
194          */
195         public function __toString()
196         {
197                 return json_encode($this->toArray());
198         }
199
200         /**
201          * @inheritDoc
202          */
203         #[\ReturnTypeWillChange]
204         public function jsonSerialize()
205         {
206                 return $this->toArray();
207         }
208 }