From 2b8f0677158feff781478898e1d58e1bb2100c96 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 20:23:58 +0100 Subject: [PATCH] Introduce interface for emailing and create email classes --- include/enotify.php | 10 ++- mod/item.php | 31 ++----- src/Object/EMail.php | 135 +++++++++++++++++++++++++++++++ src/Object/EMail/IEmail.php | 71 ++++++++++++++++ src/Object/EMail/ItemCCEMail.php | 35 ++++++++ src/Util/Emailer.php | 106 +++++++++++++----------- 6 files changed, 314 insertions(+), 74 deletions(-) create mode 100644 src/Object/EMail.php create mode 100644 src/Object/EMail/IEmail.php create mode 100644 src/Object/EMail/ItemCCEMail.php diff --git a/include/enotify.php b/include/enotify.php index 7b78d556d6..f55c9f6ccd 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -15,6 +15,7 @@ use Friendica\Model\ItemContent; use Friendica\Model\Notify; use Friendica\Model\User; use Friendica\Model\UserItem; +use Friendica\Object\EMail; use Friendica\Protocol\Activity; use Friendica\Util\DateTimeFormat; use Friendica\Util\Emailer; @@ -609,11 +610,12 @@ function notification($params) '$content_allowed' => $content_allowed, ]); - // use the Emailer class to send the message - return DI::emailer()->send($sender_name, $sender_email, $sender_email, $params['to_email'], + $email = new EMail($sender_name, $sender_email, $sender_email, $params['to_email'], $datarray['subject'], $email_html_body, $email_text_body, - $datarray['headers'], $params['uid'] - ); + $datarray['headers'], $params['uid']); + + // use the Emailer class to send the message + return DI::emailer()->send($email); } return false; diff --git a/mod/item.php b/mod/item.php index 2bda16764c..8b795d28cb 100644 --- a/mod/item.php +++ b/mod/item.php @@ -18,7 +18,6 @@ use Friendica\App; use Friendica\Content\Pager; use Friendica\Content\Text\BBCode; -use Friendica\Content\Text\HTML; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Protocol; @@ -34,11 +33,10 @@ use Friendica\Model\FileTag; use Friendica\Model\Item; use Friendica\Model\Photo; use Friendica\Model\Term; +use Friendica\Object\EMail\ItemCCEMail; use Friendica\Protocol\Activity; use Friendica\Protocol\Diaspora; -use Friendica\Protocol\Email; use Friendica\Util\DateTimeFormat; -use Friendica\Util\Emailer; use Friendica\Util\Security; use Friendica\Util\Strings; use Friendica\Worker\Delivery; @@ -788,28 +786,15 @@ function item_post(App $a) { Hook::callAll('post_local_end', $datarray); if (strlen($emailcc) && $profile_uid == local_user()) { - $erecips = explode(',', $emailcc); - if (count($erecips)) { - foreach ($erecips as $recip) { - $addr = trim($recip); - if (!strlen($addr)) { + $recipients = explode(',', $emailcc); + if (count($recipients)) { + foreach ($recipients as $recipient) { + $address = trim($recipient); + if (!strlen($address)) { continue; } - $disclaimer = '
' . DI::l10n()->t('This message was sent to you by %s, a member of the Friendica social network.', $a->user['username']) - . '
'; - $disclaimer .= DI::l10n()->t('You may visit them online at %s', DI::baseUrl() . '/profile/' . $a->user['nickname']) . EOL; - $disclaimer .= DI::l10n()->t('Please contact the sender by replying to this post if you do not wish to receive these messages.') . EOL; - if (!$datarray['title'] == '') { - $subject = Email::encodeHeader($datarray['title'], 'UTF-8'); - } else { - $subject = Email::encodeHeader('[Friendica]' . ' ' . DI::l10n()->t('%s posted an update.', $a->user['username']), 'UTF-8'); - } - $link = '' . $a->user['username'] . '

'; - $html = Item::prepareBody($datarray); - $message = '' . $link . $html . $disclaimer . '';; - DI::emailer()->send($a->user['username'], $a->user['email'], $a->user['email'], $addr, - $subject, $message, HTML::toPlaintext($html . $disclaimer) - ); + DI::emailer()->send(new ItemCCEMail(DI::app(), DI::l10n(), DI::baseUrl(), + $datarray, $address, $author['thumb'] ?? '')); } } } diff --git a/src/Object/EMail.php b/src/Object/EMail.php new file mode 100644 index 0000000000..5985b3f3be --- /dev/null +++ b/src/Object/EMail.php @@ -0,0 +1,135 @@ +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; + } +} diff --git a/src/Object/EMail/IEmail.php b/src/Object/EMail/IEmail.php new file mode 100644 index 0000000000..cd654b9d32 --- /dev/null +++ b/src/Object/EMail/IEmail.php @@ -0,0 +1,71 @@ +' . $l10n->t('This message was sent to you by %s, a member of the Friendica social network.', $a->user['username']) + . '
'; + $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->user['username'] . '

'; + $html = Item::prepareBody($item); + $message = '' . $link . $html . $disclaimer . '';; + + parent::__construct($a->user['username'], $a->user['email'], $a->user['email'], $toEmail, + $subject, $message, HTML::toPlaintext($html . $disclaimer)); + } +} diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index b706cacbb1..9490b5c31a 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -4,36 +4,46 @@ */ namespace Friendica\Util; +use Friendica\App; +use Friendica\Core\Config\IConfig; use Friendica\Core\Hook; -use Friendica\Core\Logger; -use Friendica\DI; +use Friendica\Core\PConfig\IPConfig; +use Friendica\Network\HTTPException\InternalServerErrorException; +use Friendica\Object\EMail\IEmail; use Friendica\Protocol\Email; +use Psr\Log\LoggerInterface; /** * class to handle emailing */ class Emailer { + /** @var IConfig */ + private $config; + /** @var IPConfig */ + private $pConfig; + /** @var LoggerInterface */ + private $logger; + /** @var App\BaseURL */ + private $baseUrl; + + public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger) + { + $this->config = $config; + $this->pConfig = $pConfig; + $this->logger = $logger; + $this->baseUrl = $baseURL; + } + /** * Send a multipart/alternative message with Text and HTML versions * - * @param array $params parameters - * fromName name of the sender - * fromEmail email of the sender - * replyTo address to direct responses - * toEmail destination email address - * messageSubject subject of the message - * htmlVersion html version of the message - * textVersion text only version of the message - * additionalMailHeader additions to the SMTP mail header - * optional uid user id of the destination user + * @param IEmail $email The email to send * * @return bool - * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws InternalServerErrorException */ - public function send(string $fromName, string $fromEmail, string $replyTo, string $toEmail, - string $subject, string $msgHtml, string $msgText, - string $additionalMailHeader = '', int $uid = null) + public function send(IEmail $email) { $params['sent'] = false; @@ -44,45 +54,47 @@ class Emailer } $email_textonly = false; - if (!empty($uid)) { - $email_textonly = DI::pConfig()->get($uid, "system", "email_textonly"); + if (!empty($email->getRecipientUid())) { + $email_textonly = $this->pConfig->get($email->getRecipientUid(), 'system', 'email_textonly'); } - $fromName = Email::encodeHeader(html_entity_decode($fromName, ENT_QUOTES, 'UTF-8'), 'UTF-8'); - $messageSubject = Email::encodeHeader(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'), 'UTF-8'); + $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8'); + $fromEmail = $email->getFromEmail(); + $replyTo = $email->getReplyTo(); + $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8'); // generate a mime boundary - $mimeBoundary =rand(0, 9)."-" - .rand(100000000, 999999999)."-" - .rand(100000000, 999999999)."=:" - .rand(10000, 99999); + $mimeBoundary = rand(0, 9) . '-' + . rand(100000000, 999999999) . '-' + . rand(100000000, 999999999) . '=:' + . rand(10000, 99999); // generate a multipart/alternative message header - $messageHeader = $additionalMailHeader . - "From: $fromName <{$fromEmail}>\n" . - "Reply-To: $fromName <{$replyTo}>\n" . - "MIME-Version: 1.0\n" . - "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\""; + $messageHeader = $email->getAdditionalMailHeader() . + "From: $fromName <{$fromEmail}>\n" . + "Reply-To: $fromName <{$replyTo}>\n" . + "MIME-Version: 1.0\n" . + "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\""; // assemble the final multipart message body with the text and html types included - $textBody = chunk_split(base64_encode($msgText)); - $htmlBody = chunk_split(base64_encode($msgHtml)); - $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section - "Content-Type: text/plain; charset=UTF-8\n" . - "Content-Transfer-Encoding: base64\n\n" . - $textBody . "\n"; + $textBody = chunk_split(base64_encode($email->getMessage())); + $htmlBody = chunk_split(base64_encode($email->getMessage(true))); + $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section + "Content-Type: text/plain; charset=UTF-8\n" . + "Content-Transfer-Encoding: base64\n\n" . + $textBody . "\n"; - if (!$email_textonly && !is_null($msgHtml)) { + if (!$email_textonly && !is_null($email->getMessage())) { $multipartMessageBody .= - "--" . $mimeBoundary . "\n" . // text/html section + "--" . $mimeBoundary . "\n" . // text/html section "Content-Type: text/html; charset=UTF-8\n" . "Content-Transfer-Encoding: base64\n\n" . $htmlBody . "\n"; } $multipartMessageBody .= - "--" . $mimeBoundary . "--\n"; // message ending + "--" . $mimeBoundary . "--\n"; // message ending - if (DI::config()->get("system", "sendmail_params", true)) { + if ($this->config->get('system', 'sendmail_params', true)) { $sendmail_params = '-f ' . $fromEmail; } else { $sendmail_params = null; @@ -90,15 +102,15 @@ class Emailer // send the message $hookdata = [ - 'to' => $toEmail, - 'subject' => $messageSubject, - 'body' => $multipartMessageBody, - 'headers' => $messageHeader, + 'to' => $email->getToEmail(), + 'subject' => $messageSubject, + 'body' => $multipartMessageBody, + 'headers' => $messageHeader, 'parameters' => $sendmail_params, - 'sent' => false, + 'sent' => false, ]; - Hook::callAll("emailer_send", $hookdata); + Hook::callAll('emailer_send', $hookdata); if ($hookdata['sent']) { return true; @@ -111,8 +123,8 @@ class Emailer $hookdata['headers'], $hookdata['parameters'] ); - Logger::log("header " . 'To: ' . $toEmail . "\n" . $messageHeader, Logger::DEBUG); - Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG); + $this->logger->debug('header ' . 'To: ' . $email->getToEmail() . '\n' . $messageHeader); + $this->logger->debug('return value ' . (($res) ? 'true' : 'false')); return $res; } }