2020-01-26 14:23:58 -05:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2022-01-02 02:27:47 -05:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2020-01-26 14:23:58 -05:00
|
|
|
|
|
|
|
namespace Friendica\Object;
|
|
|
|
|
|
|
|
use Friendica\Object\EMail\IEmail;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default implementation of the IEmail interface
|
|
|
|
*
|
2020-01-26 17:47:16 -05:00
|
|
|
* Provides the possibility to reuse the email instance with new recipients (@see Email::withRecipient())
|
2020-01-26 14:23:58 -05:00
|
|
|
*/
|
2020-01-26 17:47:16 -05:00
|
|
|
class Email implements IEmail
|
2020-01-26 14:23:58 -05:00
|
|
|
{
|
|
|
|
/** @var string */
|
|
|
|
private $fromName;
|
|
|
|
/** @var string */
|
2020-01-26 17:47:16 -05:00
|
|
|
private $fromAddress;
|
2020-01-26 14:23:58 -05:00
|
|
|
/** @var string */
|
|
|
|
private $replyTo;
|
|
|
|
|
|
|
|
/** @var string */
|
2020-01-26 17:47:16 -05:00
|
|
|
private $toAddress;
|
2020-01-26 14:23:58 -05:00
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $subject;
|
2020-01-31 13:32:17 -05:00
|
|
|
/** @var string|null */
|
2020-01-26 14:23:58 -05:00
|
|
|
private $msgHtml;
|
|
|
|
/** @var string */
|
|
|
|
private $msgText;
|
|
|
|
|
2020-09-19 14:14:55 -04:00
|
|
|
/** @var string[][] */
|
|
|
|
private $additionalMailHeader;
|
2020-01-26 14:23:58 -05:00
|
|
|
/** @var int|null */
|
2020-09-19 14:14:55 -04:00
|
|
|
private $toUid;
|
2020-01-26 14:23:58 -05:00
|
|
|
|
2020-01-26 18:01:17 -05:00
|
|
|
public function __construct(string $fromName, string $fromAddress, string $replyTo, string $toAddress,
|
2020-01-26 14:23:58 -05:00
|
|
|
string $subject, string $msgHtml, string $msgText,
|
2020-09-19 14:14:55 -04:00
|
|
|
array $additionalMailHeader = [], int $toUid = null)
|
2020-01-26 14:23:58 -05:00
|
|
|
{
|
|
|
|
$this->fromName = $fromName;
|
2020-01-26 18:01:17 -05:00
|
|
|
$this->fromAddress = $fromAddress;
|
2020-01-26 14:23:58 -05:00
|
|
|
$this->replyTo = $replyTo;
|
2020-01-26 18:01:17 -05:00
|
|
|
$this->toAddress = $toAddress;
|
2020-01-26 14:23:58 -05:00
|
|
|
$this->subject = $subject;
|
|
|
|
$this->msgHtml = $msgHtml;
|
|
|
|
$this->msgText = $msgText;
|
|
|
|
$this->additionalMailHeader = $additionalMailHeader;
|
|
|
|
$this->toUid = $toUid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getFromName()
|
|
|
|
{
|
|
|
|
return $this->fromName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2020-01-26 17:47:16 -05:00
|
|
|
public function getFromAddress()
|
2020-01-26 14:23:58 -05:00
|
|
|
{
|
2020-01-26 17:47:16 -05:00
|
|
|
return $this->fromAddress;
|
2020-01-26 14:23:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getReplyTo()
|
|
|
|
{
|
|
|
|
return $this->replyTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2020-01-26 17:47:16 -05:00
|
|
|
public function getToAddress()
|
2020-01-26 14:23:58 -05:00
|
|
|
{
|
2020-01-26 17:47:16 -05:00
|
|
|
return $this->toAddress;
|
2020-01-26 14:23:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getSubject()
|
|
|
|
{
|
|
|
|
return $this->subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2022-11-19 19:08:44 -05:00
|
|
|
public function getMessage(bool $plain = false): string
|
2020-01-26 14:23:58 -05:00
|
|
|
{
|
2020-01-26 14:41:53 -05:00
|
|
|
if ($plain) {
|
2020-01-26 14:23:58 -05:00
|
|
|
return $this->msgText;
|
|
|
|
} else {
|
2022-11-19 19:08:44 -05:00
|
|
|
return $this->msgHtml ?? '';
|
2020-01-26 14:23:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getAdditionalMailHeader()
|
|
|
|
{
|
|
|
|
return $this->additionalMailHeader;
|
|
|
|
}
|
|
|
|
|
2020-09-19 14:14:55 -04:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getAdditionalMailHeaderString()
|
|
|
|
{
|
|
|
|
$headerString = '';
|
|
|
|
|
|
|
|
foreach ($this->additionalMailHeader as $name => $values) {
|
2022-11-13 02:28:06 -05:00
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = [$values];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($values as $value) {
|
|
|
|
$headerString .= "$name: $value\r\n";
|
2020-09-19 14:14:55 -04:00
|
|
|
}
|
|
|
|
}
|
2022-11-13 02:28:06 -05:00
|
|
|
|
2020-09-19 14:14:55 -04:00
|
|
|
return $headerString;
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:23:58 -05:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getRecipientUid()
|
|
|
|
{
|
|
|
|
return $this->toUid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-31 13:32:17 -05:00
|
|
|
* {@inheritDoc}
|
2020-01-26 14:23:58 -05:00
|
|
|
*/
|
2020-01-31 13:32:17 -05:00
|
|
|
public function withRecipient(string $address, int $uid = null)
|
2020-01-26 14:23:58 -05:00
|
|
|
{
|
2020-01-26 17:47:16 -05:00
|
|
|
$newEmail = clone $this;
|
2020-01-31 13:32:17 -05:00
|
|
|
$newEmail->toAddress = $address;
|
2020-01-26 17:47:16 -05:00
|
|
|
$newEmail->toUid = $uid;
|
2020-01-26 14:23:58 -05:00
|
|
|
|
|
|
|
return $newEmail;
|
|
|
|
}
|
2020-01-29 14:20:40 -05:00
|
|
|
|
|
|
|
/**
|
2020-01-31 13:32:17 -05:00
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function withMessage(string $plaintext, string $html = null)
|
|
|
|
{
|
|
|
|
$newMail = clone $this;
|
|
|
|
$newMail->msgText = $plaintext;
|
|
|
|
$newMail->msgHtml = $html;
|
|
|
|
|
|
|
|
return $newMail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the properties of the email as an array
|
2020-01-29 14:20:40 -05:00
|
|
|
*
|
2020-01-31 13:32:17 -05:00
|
|
|
* @return array
|
2020-01-29 14:20:40 -05:00
|
|
|
*/
|
2020-01-31 13:32:17 -05:00
|
|
|
private function toArray()
|
2020-01-29 14:20:40 -05:00
|
|
|
{
|
2020-01-31 13:32:17 -05:00
|
|
|
return get_object_vars($this);
|
|
|
|
}
|
2020-01-29 14:20:40 -05:00
|
|
|
|
2020-01-31 13:32:17 -05:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return json_encode($this->toArray());
|
|
|
|
}
|
2020-01-29 14:20:40 -05:00
|
|
|
|
2020-01-31 13:32:17 -05:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function jsonSerialize()
|
|
|
|
{
|
|
|
|
return $this->toArray();
|
2020-01-29 14:20:40 -05:00
|
|
|
}
|
2020-01-26 14:23:58 -05:00
|
|
|
}
|