3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Object;
24 use Friendica\Object\EMail\IEmail;
27 * The default implementation of the IEmail interface
29 * Provides the possibility to reuse the email instance with new recipients (@see Email::withRecipient())
31 class Email implements IEmail
45 /** @var string|null */
50 /** @var string[][] */
51 private $additionalMailHeader;
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)
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;
73 public function getFromName()
75 return $this->fromName;
81 public function getFromAddress()
83 return $this->fromAddress;
89 public function getReplyTo()
91 return $this->replyTo;
97 public function getToAddress()
99 return $this->toAddress;
105 public function getSubject()
107 return $this->subject;
113 public function getMessage(bool $plain = false)
116 return $this->msgText;
118 return $this->msgHtml;
125 public function getAdditionalMailHeader()
127 return $this->additionalMailHeader;
133 public function getAdditionalMailHeaderString()
137 foreach ($this->additionalMailHeader as $name => $values) {
138 if (is_array($values)) {
139 foreach ($values as $value) {
140 $headerString .= "$name: $value\n";
143 $headerString .= "$name: $values\n";
146 return $headerString;
152 public function getRecipientUid()
160 public function withRecipient(string $address, int $uid = null)
162 $newEmail = clone $this;
163 $newEmail->toAddress = $address;
164 $newEmail->toUid = $uid;
172 public function withMessage(string $plaintext, string $html = null)
174 $newMail = clone $this;
175 $newMail->msgText = $plaintext;
176 $newMail->msgHtml = $html;
182 * Returns the properties of the email as an array
186 private function toArray()
188 return get_object_vars($this);
194 public function __toString()
196 return json_encode($this->toArray());
202 public function jsonSerialize()
204 return $this->toArray();