Introduce interface for emailing and create email classes

This commit is contained in:
nupplaPhil 2020-01-26 20:23:58 +01:00
parent 915abe8a33
commit 2b8f067715
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
6 changed files with 314 additions and 74 deletions

135
src/Object/EMail.php Normal file
View file

@ -0,0 +1,135 @@
<?php
namespace Friendica\Object;
use Friendica\Object\EMail\IEmail;
/**
* The default implementation of the IEmail interface
*
* Provides the possibility to reuse the email instance with new recipients (@see EMail::withRecipient())
*/
class EMail implements IEmail
{
/** @var string */
private $fromName;
/** @var string */
private $fromEmail;
/** @var string */
private $replyTo;
/** @var string */
private $toEmail;
/** @var string */
private $subject;
/** @var string */
private $msgHtml;
/** @var string */
private $msgText;
/** @var string */
private $additionalMailHeader = '';
/** @var int|null */
private $toUid = null;
public function __construct(string $fromName, string $fromEmail, string $replyTo, string $toEmail,
string $subject, string $msgHtml, string $msgText,
string $additionalMailHeader = '', int $toUid = null)
{
$this->fromName = $fromName;
$this->fromEmail = $fromEmail;
$this->replyTo = $replyTo;
$this->toEmail = $toEmail;
$this->subject = $subject;
$this->msgHtml = $msgHtml;
$this->msgText = $msgText;
$this->additionalMailHeader = $additionalMailHeader;
$this->toUid = $toUid;
}
/**
* {@inheritDoc}
*/
public function getFromName()
{
return $this->fromName;
}
/**
* {@inheritDoc}
*/
public function getFromEmail()
{
return $this->fromEmail;
}
/**
* {@inheritDoc}
*/
public function getReplyTo()
{
return $this->replyTo;
}
/**
* {@inheritDoc}
*/
public function getToEmail()
{
return $this->toEmail;
}
/**
* {@inheritDoc}
*/
public function getSubject()
{
return $this->subject;
}
/**
* {@inheritDoc}
*/
public function getMessage(bool $text = false)
{
if ($text) {
return $this->msgText;
} else {
return $this->msgHtml;
}
}
/**
* {@inheritDoc}
*/
public function getAdditionalMailHeader()
{
return $this->additionalMailHeader;
}
/**
* {@inheritDoc}
*/
public function getRecipientUid()
{
return $this->toUid;
}
/**
* Returns the current email with a new recipient
*
* @param string $email The email of the recipient
* @param int $uid The (optional) UID of the recipient for further infos
*
* @return EMail
*/
public function withRecipient(string $email, int $uid = null)
{
$newEmail = clone $this;
$newEmail->toEmail = $email;
$newEmail->toUid = $uid;
return $newEmail;
}
}

View file

@ -0,0 +1,71 @@
<?php
namespace Friendica\Object\EMail;
use Friendica\Util\Emailer;
/**
* Interface for a single mail, which can be send through Emailer::send()
*
* @see Emailer::send()
*/
interface IEmail
{
/**
* Gets the senders name for this email
*
* @return string
*/
function getFromName();
/**
* Gets the senders email address for this email
*
* @return string
*/
function getFromEmail();
/**
* Gets the UID of the sender of this email
*
* @return int|null
*/
function getRecipientUid();
/**
* Gets the reply-to address for this email
*
* @return string
*/
function getReplyTo();
/**
* Gets the senders email address
*
* @return string
*/
function getToEmail();
/**
* Gets the subject of this email
*
* @return string
*/
function getSubject();
/**
* Gets the message body of this email (either html or plaintext)
*
* @param boolean $text True, if returned as plaintext
*
* @return string
*/
function getMessage(bool $text = false);
/**
* Gets any additional mail header
*
* @return string
*/
function getAdditionalMailHeader();
}

View file

@ -0,0 +1,35 @@
<?php
namespace Friendica\Object\EMail;
use Friendica\App;
use Friendica\App\BaseURL;
use Friendica\Content\Text\HTML;
use Friendica\Core\L10n;
use Friendica\Model\Item;
use Friendica\Object\EMail;
/**
* Class for creating CC emails based on a received item
*/
class ItemCCEMail extends EMail
{
public function __construct(App $a, L10n $l10n, BaseURL $baseUrl, array $item, string $toEmail, string $authorThumb)
{
$disclaimer = '<hr />' . $l10n->t('This message was sent to you by %s, a member of the Friendica social network.', $a->user['username'])
. '<br />';
$disclaimer .= $l10n->t('You may visit them online at %s', $baseUrl . '/profile/' . $a->user['nickname']) . EOL;
$disclaimer .= $l10n->t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL;
if (!$item['title'] == '') {
$subject = EMail::encodeHeader($item['title'], 'UTF-8');
} else {
$subject = Email::encodeHeader('[Friendica]' . ' ' . $l10n->t('%s posted an update.', $a->user['username']), 'UTF-8');
}
$link = '<a href="' . $baseUrl . '/profile/' . $a->user['nickname'] . '"><img src="' . $authorThumb . '" alt="' . $a->user['username'] . '" /></a><br /><br />';
$html = Item::prepareBody($item);
$message = '<html><body>' . $link . $html . $disclaimer . '</body></html>';;
parent::__construct($a->user['username'], $a->user['email'], $a->user['email'], $toEmail,
$subject, $message, HTML::toPlaintext($html . $disclaimer));
}
}