From ea9d3b743835bca13091093cd8709ccb825fa417 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 25 Jan 2020 23:10:39 +0100 Subject: [PATCH 1/7] Make EMailer util dynamic --- include/enotify.php | 2 +- mod/item.php | 2 +- src/DI.php | 8 ++++++++ src/Util/Emailer.php | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/include/enotify.php b/include/enotify.php index 144460693d..75ad7ef1fa 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -610,7 +610,7 @@ function notification($params) ]); // use the Emailer class to send the message - return Emailer::send([ + return DI::emailer()->send([ 'uid' => $params['uid'], 'fromName' => $sender_name, 'fromEmail' => $sender_email, diff --git a/mod/item.php b/mod/item.php index a9c5d43580..a5ff594a0c 100644 --- a/mod/item.php +++ b/mod/item.php @@ -816,7 +816,7 @@ function item_post(App $a) { 'htmlVersion' => $message, 'textVersion' => HTML::toPlaintext($html.$disclaimer) ]; - Emailer::send($params); + DI::emailer()->send($params); } } } diff --git a/src/DI.php b/src/DI.php index 3034bd971c..6eb1ed4b63 100644 --- a/src/DI.php +++ b/src/DI.php @@ -363,4 +363,12 @@ abstract class DI { return self::$dice->create(Util\Profiler::class); } + + /** + * @return Util\Emailer + */ + public static function emailer() + { + return self::$dice->create(Util\Emailer::class); + } } diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index 41835b6609..776d359216 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -31,7 +31,7 @@ class Emailer * @return bool * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function send(array $params) + public function send(array $params) { $params['sent'] = false; From 915abe8a338a9648a2125e6e0ea96e5def01cb6c Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 01:04:54 +0100 Subject: [PATCH 2/7] Add explicit parameters to Sender::send() method signature --- include/enotify.php | 141 ++++++++++++++++++++----------------------- mod/item.php | 21 +++---- src/Util/Emailer.php | 30 ++++----- 3 files changed, 90 insertions(+), 102 deletions(-) diff --git a/include/enotify.php b/include/enotify.php index 75ad7ef1fa..7b78d556d6 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -512,19 +512,19 @@ function notification($params) Logger::log('sending notification email'); if (isset($params['parent']) && (intval($params['parent']) != 0)) { - $id_for_parent = $params['parent']."@".$hostname; + $id_for_parent = $params['parent'] . "@" . $hostname; // Is this the first email notification for this parent item and user? if (!DBA::exists('notify-threads', ['master-parent-item' => $params['parent'], 'receiver-uid' => $params['uid']])) { - Logger::log("notify_id:".intval($notify_id).", parent: ".intval($params['parent'])."uid: ".intval($params['uid']), Logger::DEBUG); + Logger::log("notify_id:" . intval($notify_id) . ", parent: " . intval($params['parent']) . "uid: " . intval($params['uid']), Logger::DEBUG); - $fields = ['notify-id' => $notify_id, 'master-parent-item' => $params['parent'], - 'receiver-uid' => $params['uid'], 'parent-item' => 0]; + $fields = ['notify-id' => $notify_id, 'master-parent-item' => $params['parent'], + 'receiver-uid' => $params['uid'], 'parent-item' => 0]; DBA::insert('notify-threads', $fields); $additional_mail_header .= "Message-ID: <${id_for_parent}>\n"; - $log_msg = "include/enotify: No previous notification found for this parent:\n". - " parent: ${params['parent']}\n"." uid : ${params['uid']}\n"; + $log_msg = "include/enotify: No previous notification found for this parent:\n" . + " parent: ${params['parent']}\n" . " uid : ${params['uid']}\n"; Logger::log($log_msg, Logger::DEBUG); } else { // If not, just "follow" the thread. @@ -536,30 +536,30 @@ function notification($params) $textversion = BBCode::toPlaintext($body); $htmlversion = BBCode::convert($body); - $datarray = []; - $datarray['banner'] = $banner; - $datarray['product'] = $product; - $datarray['preamble'] = $preamble; - $datarray['sitename'] = $sitename; - $datarray['siteurl'] = $siteurl; - $datarray['type'] = $params['type']; - $datarray['parent'] = $parent_id; - $datarray['source_name'] = $params['source_name'] ?? ''; - $datarray['source_link'] = $params['source_link'] ?? ''; + $datarray = []; + $datarray['banner'] = $banner; + $datarray['product'] = $product; + $datarray['preamble'] = $preamble; + $datarray['sitename'] = $sitename; + $datarray['siteurl'] = $siteurl; + $datarray['type'] = $params['type']; + $datarray['parent'] = $parent_id; + $datarray['source_name'] = $params['source_name'] ?? ''; + $datarray['source_link'] = $params['source_link'] ?? ''; $datarray['source_photo'] = $params['source_photo'] ?? ''; - $datarray['uid'] = $params['uid']; - $datarray['username'] = $params['to_name'] ?? ''; - $datarray['hsitelink'] = $hsitelink; - $datarray['tsitelink'] = $tsitelink; - $datarray['hitemlink'] = ''.$itemlink.''; - $datarray['titemlink'] = $itemlink; - $datarray['thanks'] = $thanks; - $datarray['site_admin'] = $site_admin; - $datarray['title'] = stripslashes($title); - $datarray['htmlversion'] = $htmlversion; - $datarray['textversion'] = $textversion; - $datarray['subject'] = $subject; - $datarray['headers'] = $additional_mail_header; + $datarray['uid'] = $params['uid']; + $datarray['username'] = $params['to_name'] ?? ''; + $datarray['hsitelink'] = $hsitelink; + $datarray['tsitelink'] = $tsitelink; + $datarray['hitemlink'] = '' . $itemlink . ''; + $datarray['titemlink'] = $itemlink; + $datarray['thanks'] = $thanks; + $datarray['site_admin'] = $site_admin; + $datarray['title'] = stripslashes($title); + $datarray['htmlversion'] = $htmlversion; + $datarray['textversion'] = $textversion; + $datarray['subject'] = $subject; + $datarray['headers'] = $additional_mail_header; Hook::callAll('enotify_mail', $datarray); @@ -568,59 +568,52 @@ function notification($params) $content_allowed = ((!DI::config()->get('system', 'enotify_no_content')) || ($params['type'] == SYSTEM_EMAIL)); // load the template for private message notifications - $tpl = Renderer::getMarkupTemplate('email_notify_html.tpl'); + $tpl = Renderer::getMarkupTemplate('email_notify_html.tpl'); $email_html_body = Renderer::replaceMacros($tpl, [ - '$banner' => $datarray['banner'], - '$product' => $datarray['product'], - '$preamble' => str_replace("\n", "
\n", $datarray['preamble']), - '$sitename' => $datarray['sitename'], - '$siteurl' => $datarray['siteurl'], - '$source_name' => $datarray['source_name'], - '$source_link' => $datarray['source_link'], - '$source_photo' => $datarray['source_photo'], - '$username' => $datarray['username'], - '$hsitelink' => $datarray['hsitelink'], - '$hitemlink' => $datarray['hitemlink'], - '$thanks' => $datarray['thanks'], - '$site_admin' => $datarray['site_admin'], - '$title' => $datarray['title'], - '$htmlversion' => $datarray['htmlversion'], - '$content_allowed' => $content_allowed, + '$banner' => $datarray['banner'], + '$product' => $datarray['product'], + '$preamble' => str_replace("\n", "
\n", $datarray['preamble']), + '$sitename' => $datarray['sitename'], + '$siteurl' => $datarray['siteurl'], + '$source_name' => $datarray['source_name'], + '$source_link' => $datarray['source_link'], + '$source_photo' => $datarray['source_photo'], + '$username' => $datarray['username'], + '$hsitelink' => $datarray['hsitelink'], + '$hitemlink' => $datarray['hitemlink'], + '$thanks' => $datarray['thanks'], + '$site_admin' => $datarray['site_admin'], + '$title' => $datarray['title'], + '$htmlversion' => $datarray['htmlversion'], + '$content_allowed' => $content_allowed, ]); // load the template for private message notifications - $tpl = Renderer::getMarkupTemplate('email_notify_text.tpl'); + $tpl = Renderer::getMarkupTemplate('email_notify_text.tpl'); $email_text_body = Renderer::replaceMacros($tpl, [ - '$banner' => $datarray['banner'], - '$product' => $datarray['product'], - '$preamble' => $datarray['preamble'], - '$sitename' => $datarray['sitename'], - '$siteurl' => $datarray['siteurl'], - '$source_name' => $datarray['source_name'], - '$source_link' => $datarray['source_link'], - '$source_photo' => $datarray['source_photo'], - '$username' => $datarray['username'], - '$tsitelink' => $datarray['tsitelink'], - '$titemlink' => $datarray['titemlink'], - '$thanks' => $datarray['thanks'], - '$site_admin' => $datarray['site_admin'], - '$title' => $datarray['title'], - '$textversion' => $datarray['textversion'], - '$content_allowed' => $content_allowed, + '$banner' => $datarray['banner'], + '$product' => $datarray['product'], + '$preamble' => $datarray['preamble'], + '$sitename' => $datarray['sitename'], + '$siteurl' => $datarray['siteurl'], + '$source_name' => $datarray['source_name'], + '$source_link' => $datarray['source_link'], + '$source_photo' => $datarray['source_photo'], + '$username' => $datarray['username'], + '$tsitelink' => $datarray['tsitelink'], + '$titemlink' => $datarray['titemlink'], + '$thanks' => $datarray['thanks'], + '$site_admin' => $datarray['site_admin'], + '$title' => $datarray['title'], + '$textversion' => $datarray['textversion'], + '$content_allowed' => $content_allowed, ]); // use the Emailer class to send the message - return DI::emailer()->send([ - 'uid' => $params['uid'], - 'fromName' => $sender_name, - 'fromEmail' => $sender_email, - 'replyTo' => $sender_email, - 'toEmail' => $params['to_email'], - 'messageSubject' => $datarray['subject'], - 'htmlVersion' => $email_html_body, - 'textVersion' => $email_text_body, - 'additionalMailHeader' => $datarray['headers'] - ]); + return DI::emailer()->send($sender_name, $sender_email, $sender_email, $params['to_email'], + $datarray['subject'], $email_html_body, $email_text_body, + $datarray['headers'], $params['uid'] + ); } return false; diff --git a/mod/item.php b/mod/item.php index a5ff594a0c..2bda16764c 100644 --- a/mod/item.php +++ b/mod/item.php @@ -796,27 +796,20 @@ function item_post(App $a) { 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']=='') { + 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'] . '

'; + $link = '' . $a->user['username'] . '

'; $html = Item::prepareBody($datarray); - $message = '' . $link . $html . $disclaimer . ''; - $params = [ - 'fromName' => $a->user['username'], - 'fromEmail' => $a->user['email'], - 'toEmail' => $addr, - 'replyTo' => $a->user['email'], - 'messageSubject' => $subject, - 'htmlVersion' => $message, - 'textVersion' => HTML::toPlaintext($html.$disclaimer) - ]; - DI::emailer()->send($params); + $message = '' . $link . $html . $disclaimer . '';; + DI::emailer()->send($a->user['username'], $a->user['email'], $a->user['email'], $addr, + $subject, $message, HTML::toPlaintext($html . $disclaimer) + ); } } } diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index 776d359216..b706cacbb1 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -31,7 +31,9 @@ class Emailer * @return bool * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function send(array $params) + public function send(string $fromName, string $fromEmail, string $replyTo, string $toEmail, + string $subject, string $msgHtml, string $msgText, + string $additionalMailHeader = '', int $uid = null) { $params['sent'] = false; @@ -42,12 +44,12 @@ class Emailer } $email_textonly = false; - if (!empty($params['uid'])) { - $email_textonly = DI::pConfig()->get($params['uid'], "system", "email_textonly"); + if (!empty($uid)) { + $email_textonly = DI::pConfig()->get($uid, "system", "email_textonly"); } - $fromName = Email::encodeHeader(html_entity_decode($params['fromName'], ENT_QUOTES, 'UTF-8'), 'UTF-8'); - $messageSubject = Email::encodeHeader(html_entity_decode($params['messageSubject'], ENT_QUOTES, 'UTF-8'), 'UTF-8'); + $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'); // generate a mime boundary $mimeBoundary =rand(0, 9)."-" @@ -56,21 +58,21 @@ class Emailer .rand(10000, 99999); // generate a multipart/alternative message header - $messageHeader = ($params['additionalMailHeader'] ?? '') . - "From: $fromName <{$params['fromEmail']}>\n" . - "Reply-To: $fromName <{$params['replyTo']}>\n" . + $messageHeader = $additionalMailHeader . + "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($params['textVersion'])); - $htmlBody = chunk_split(base64_encode($params['htmlVersion'])); + $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"; - if (!$email_textonly && !is_null($params['htmlVersion'])) { + if (!$email_textonly && !is_null($msgHtml)) { $multipartMessageBody .= "--" . $mimeBoundary . "\n" . // text/html section "Content-Type: text/html; charset=UTF-8\n" . @@ -81,14 +83,14 @@ class Emailer "--" . $mimeBoundary . "--\n"; // message ending if (DI::config()->get("system", "sendmail_params", true)) { - $sendmail_params = '-f ' . $params['fromEmail']; + $sendmail_params = '-f ' . $fromEmail; } else { $sendmail_params = null; } // send the message $hookdata = [ - 'to' => $params['toEmail'], + 'to' => $toEmail, 'subject' => $messageSubject, 'body' => $multipartMessageBody, 'headers' => $messageHeader, @@ -109,7 +111,7 @@ class Emailer $hookdata['headers'], $hookdata['parameters'] ); - Logger::log("header " . 'To: ' . $params['toEmail'] . "\n" . $messageHeader, Logger::DEBUG); + Logger::log("header " . 'To: ' . $toEmail . "\n" . $messageHeader, Logger::DEBUG); Logger::log("return value " . (($res)?"true":"false"), Logger::DEBUG); return $res; } From 2b8f0677158feff781478898e1d58e1bb2100c96 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 20:23:58 +0100 Subject: [PATCH 3/7] 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; } } From 4efba4d0cf55711eeaa8076b75e4e49ae75f603f Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 20:41:53 +0100 Subject: [PATCH 4/7] fix boolean field --- src/Object/EMail.php | 4 ++-- src/Object/EMail/IEmail.php | 4 ++-- src/Util/Emailer.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Object/EMail.php b/src/Object/EMail.php index 5985b3f3be..496729a15d 100644 --- a/src/Object/EMail.php +++ b/src/Object/EMail.php @@ -91,9 +91,9 @@ class EMail implements IEmail /** * {@inheritDoc} */ - public function getMessage(bool $text = false) + public function getMessage(bool $plain = false) { - if ($text) { + if ($plain) { return $this->msgText; } else { return $this->msgHtml; diff --git a/src/Object/EMail/IEmail.php b/src/Object/EMail/IEmail.php index cd654b9d32..24df41b156 100644 --- a/src/Object/EMail/IEmail.php +++ b/src/Object/EMail/IEmail.php @@ -56,11 +56,11 @@ interface IEmail /** * Gets the message body of this email (either html or plaintext) * - * @param boolean $text True, if returned as plaintext + * @param boolean $plain True, if returned as plaintext * * @return string */ - function getMessage(bool $text = false); + function getMessage(bool $plain = false); /** * Gets any additional mail header diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index 9490b5c31a..f2f1edeccb 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -77,8 +77,8 @@ class Emailer "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\""; // assemble the final multipart message body with the text and html types included - $textBody = chunk_split(base64_encode($email->getMessage())); - $htmlBody = chunk_split(base64_encode($email->getMessage(true))); + $textBody = chunk_split(base64_encode($email->getMessage(true))); + $htmlBody = chunk_split(base64_encode($email->getMessage())); $multipartMessageBody = "--" . $mimeBoundary . "\n" . // plain text section "Content-Type: text/plain; charset=UTF-8\n" . "Content-Transfer-Encoding: base64\n\n" . From f6878b5bcfd8334aaca733610b82cdf316d0c220 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 20:44:41 +0100 Subject: [PATCH 5/7] fix return type --- src/Object/EMail.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Object/EMail.php b/src/Object/EMail.php index 496729a15d..5f152738d7 100644 --- a/src/Object/EMail.php +++ b/src/Object/EMail.php @@ -122,7 +122,7 @@ class EMail implements IEmail * @param string $email The email of the recipient * @param int $uid The (optional) UID of the recipient for further infos * - * @return EMail + * @return static */ public function withRecipient(string $email, int $uid = null) { From 765a0d889211a4b64e30a994472cf627319c2220 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 23:47:16 +0100 Subject: [PATCH 6/7] Some Renames: - EMail => EMail - toEmail => toAddress - fromEmail => fromAddress --- include/enotify.php | 6 ++---- src/Object/EMail/IEmail.php | 4 ++-- src/Object/EMail/ItemCCEMail.php | 6 +++--- src/Object/{EMail.php => Email.php} | 26 +++++++++++++------------- src/Util/Emailer.php | 6 +++--- 5 files changed, 23 insertions(+), 25 deletions(-) rename src/Object/{EMail.php => Email.php} (83%) diff --git a/include/enotify.php b/include/enotify.php index f55c9f6ccd..3332f4a082 100644 --- a/include/enotify.php +++ b/include/enotify.php @@ -15,10 +15,8 @@ use Friendica\Model\ItemContent; use Friendica\Model\Notify; use Friendica\Model\User; use Friendica\Model\UserItem; -use Friendica\Object\EMail; +use Friendica\Object\Email; use Friendica\Protocol\Activity; -use Friendica\Util\DateTimeFormat; -use Friendica\Util\Emailer; /** * Creates a notification entry and possibly sends a mail @@ -610,7 +608,7 @@ function notification($params) '$content_allowed' => $content_allowed, ]); - $email = new EMail($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']); diff --git a/src/Object/EMail/IEmail.php b/src/Object/EMail/IEmail.php index 24df41b156..6a159c0ba3 100644 --- a/src/Object/EMail/IEmail.php +++ b/src/Object/EMail/IEmail.php @@ -23,7 +23,7 @@ interface IEmail * * @return string */ - function getFromEmail(); + function getFromAddress(); /** * Gets the UID of the sender of this email @@ -44,7 +44,7 @@ interface IEmail * * @return string */ - function getToEmail(); + function getToAddress(); /** * Gets the subject of this email diff --git a/src/Object/EMail/ItemCCEMail.php b/src/Object/EMail/ItemCCEMail.php index c896360644..6ea1ae82ee 100644 --- a/src/Object/EMail/ItemCCEMail.php +++ b/src/Object/EMail/ItemCCEMail.php @@ -7,12 +7,12 @@ use Friendica\App\BaseURL; use Friendica\Content\Text\HTML; use Friendica\Core\L10n; use Friendica\Model\Item; -use Friendica\Object\EMail; +use Friendica\Object\Email; /** * Class for creating CC emails based on a received item */ -class ItemCCEMail extends EMail +class ItemCCEMail extends Email { public function __construct(App $a, L10n $l10n, BaseURL $baseUrl, array $item, string $toEmail, string $authorThumb) { @@ -21,7 +21,7 @@ class ItemCCEMail extends EMail $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'); + $subject = Email::encodeHeader($item['title'], 'UTF-8'); } else { $subject = Email::encodeHeader('[Friendica]' . ' ' . $l10n->t('%s posted an update.', $a->user['username']), 'UTF-8'); } diff --git a/src/Object/EMail.php b/src/Object/Email.php similarity index 83% rename from src/Object/EMail.php rename to src/Object/Email.php index 5f152738d7..7a6858b037 100644 --- a/src/Object/EMail.php +++ b/src/Object/Email.php @@ -7,19 +7,19 @@ 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()) + * Provides the possibility to reuse the email instance with new recipients (@see Email::withRecipient()) */ -class EMail implements IEmail +class Email implements IEmail { /** @var string */ private $fromName; /** @var string */ - private $fromEmail; + private $fromAddress; /** @var string */ private $replyTo; /** @var string */ - private $toEmail; + private $toAddress; /** @var string */ private $subject; @@ -38,9 +38,9 @@ class EMail implements IEmail string $additionalMailHeader = '', int $toUid = null) { $this->fromName = $fromName; - $this->fromEmail = $fromEmail; + $this->fromAddress = $fromEmail; $this->replyTo = $replyTo; - $this->toEmail = $toEmail; + $this->toAddress = $toEmail; $this->subject = $subject; $this->msgHtml = $msgHtml; $this->msgText = $msgText; @@ -59,9 +59,9 @@ class EMail implements IEmail /** * {@inheritDoc} */ - public function getFromEmail() + public function getFromAddress() { - return $this->fromEmail; + return $this->fromAddress; } /** @@ -75,9 +75,9 @@ class EMail implements IEmail /** * {@inheritDoc} */ - public function getToEmail() + public function getToAddress() { - return $this->toEmail; + return $this->toAddress; } /** @@ -126,9 +126,9 @@ class EMail implements IEmail */ public function withRecipient(string $email, int $uid = null) { - $newEmail = clone $this; - $newEmail->toEmail = $email; - $newEmail->toUid = $uid; + $newEmail = clone $this; + $newEmail->toAddress = $email; + $newEmail->toUid = $uid; return $newEmail; } diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index f2f1edeccb..b8dc127042 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -59,7 +59,7 @@ class Emailer } $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8'); - $fromEmail = $email->getFromEmail(); + $fromEmail = $email->getFromAddress(); $replyTo = $email->getReplyTo(); $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8'); @@ -102,7 +102,7 @@ class Emailer // send the message $hookdata = [ - 'to' => $email->getToEmail(), + 'to' => $email->getToAddress(), 'subject' => $messageSubject, 'body' => $multipartMessageBody, 'headers' => $messageHeader, @@ -123,7 +123,7 @@ class Emailer $hookdata['headers'], $hookdata['parameters'] ); - $this->logger->debug('header ' . 'To: ' . $email->getToEmail() . '\n' . $messageHeader); + $this->logger->debug('header ' . 'To: ' . $email->getToAddress() . '\n' . $messageHeader); $this->logger->debug('return value ' . (($res) ? 'true' : 'false')); return $res; } From 6350b210a67b22c0ad85d021221106376bc07b4e Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Mon, 27 Jan 2020 00:01:17 +0100 Subject: [PATCH 7/7] Renaming - part 2 :-) --- src/Object/EMail/ItemCCEMail.php | 4 ++-- src/Object/Email.php | 6 +++--- src/Util/Emailer.php | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Object/EMail/ItemCCEMail.php b/src/Object/EMail/ItemCCEMail.php index 6ea1ae82ee..1f1398ff55 100644 --- a/src/Object/EMail/ItemCCEMail.php +++ b/src/Object/EMail/ItemCCEMail.php @@ -14,7 +14,7 @@ use Friendica\Object\Email; */ class ItemCCEMail extends Email { - public function __construct(App $a, L10n $l10n, BaseURL $baseUrl, array $item, string $toEmail, string $authorThumb) + public function __construct(App $a, L10n $l10n, BaseURL $baseUrl, array $item, string $toAddress, string $authorThumb) { $disclaimer = '
' . $l10n->t('This message was sent to you by %s, a member of the Friendica social network.', $a->user['username']) . '
'; @@ -29,7 +29,7 @@ class ItemCCEMail extends Email $html = Item::prepareBody($item); $message = '' . $link . $html . $disclaimer . '';; - parent::__construct($a->user['username'], $a->user['email'], $a->user['email'], $toEmail, + parent::__construct($a->user['username'], $a->user['email'], $a->user['email'], $toAddress, $subject, $message, HTML::toPlaintext($html . $disclaimer)); } } diff --git a/src/Object/Email.php b/src/Object/Email.php index 7a6858b037..23e1fcfcd5 100644 --- a/src/Object/Email.php +++ b/src/Object/Email.php @@ -33,14 +33,14 @@ class Email implements IEmail /** @var int|null */ private $toUid = null; - public function __construct(string $fromName, string $fromEmail, string $replyTo, string $toEmail, + public function __construct(string $fromName, string $fromAddress, string $replyTo, string $toAddress, string $subject, string $msgHtml, string $msgText, string $additionalMailHeader = '', int $toUid = null) { $this->fromName = $fromName; - $this->fromAddress = $fromEmail; + $this->fromAddress = $fromAddress; $this->replyTo = $replyTo; - $this->toAddress = $toEmail; + $this->toAddress = $toAddress; $this->subject = $subject; $this->msgHtml = $msgHtml; $this->msgText = $msgText; diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index b8dc127042..8f6dc09e16 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -59,7 +59,7 @@ class Emailer } $fromName = Email::encodeHeader(html_entity_decode($email->getFromName(), ENT_QUOTES, 'UTF-8'), 'UTF-8'); - $fromEmail = $email->getFromAddress(); + $fromAddress = $email->getFromAddress(); $replyTo = $email->getReplyTo(); $messageSubject = Email::encodeHeader(html_entity_decode($email->getSubject(), ENT_QUOTES, 'UTF-8'), 'UTF-8'); @@ -71,7 +71,7 @@ class Emailer // generate a multipart/alternative message header $messageHeader = $email->getAdditionalMailHeader() . - "From: $fromName <{$fromEmail}>\n" . + "From: $fromName <{$fromAddress}>\n" . "Reply-To: $fromName <{$replyTo}>\n" . "MIME-Version: 1.0\n" . "Content-Type: multipart/alternative; boundary=\"{$mimeBoundary}\""; @@ -95,7 +95,7 @@ class Emailer "--" . $mimeBoundary . "--\n"; // message ending if ($this->config->get('system', 'sendmail_params', true)) { - $sendmail_params = '-f ' . $fromEmail; + $sendmail_params = '-f ' . $fromAddress; } else { $sendmail_params = null; }