From eb18a0d76135dd349da0a0c29d588ce600d5df71 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sat, 25 Jan 2020 23:10:38 +0100 Subject: [PATCH 1/4] Make EMailer util dynamic --- notifyall/notifyall.php | 2 +- securemail/securemail.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/notifyall/notifyall.php b/notifyall/notifyall.php index 6519a243..7bee5923 100644 --- a/notifyall/notifyall.php +++ b/notifyall/notifyall.php @@ -82,7 +82,7 @@ function notifyall_post(App $a) } foreach ($recips as $recip) { - Emailer::send([ + DI::emailer()->send([ 'fromName' => $sender_name, 'fromEmail' => $sender_email, 'replyTo' => $sender_email, diff --git a/securemail/securemail.php b/securemail/securemail.php index 6e665868..8b8a3fd8 100644 --- a/securemail/securemail.php +++ b/securemail/securemail.php @@ -116,7 +116,7 @@ function securemail_settings_post(App &$a, array &$b) // enable addon for test DI::pConfig()->set(local_user(), 'securemail', 'enable', 1); - $res = Emailer::send($params); + $res = DI::emailer()->send($params); // revert to saved value DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable); From b8287629104fcfb1dbae2e72c0ae90ab615d6f7c Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 01:04:53 +0100 Subject: [PATCH 2/4] Add explicit parameters to Sender::send() method signature --- notifyall/notifyall.php | 10 +--------- securemail/securemail.php | 14 +++----------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/notifyall/notifyall.php b/notifyall/notifyall.php index 7bee5923..2190a9d1 100644 --- a/notifyall/notifyall.php +++ b/notifyall/notifyall.php @@ -82,15 +82,7 @@ function notifyall_post(App $a) } foreach ($recips as $recip) { - DI::emailer()->send([ - 'fromName' => $sender_name, - 'fromEmail' => $sender_email, - 'replyTo' => $sender_email, - 'toEmail' => $recip['email'], - 'messageSubject' => $subject, - 'htmlVersion' => $htmlversion, - 'textVersion' => $textversion - ]); + DI::emailer()->send($sender_name, $sender_email, $sender_email, $recip['email'], $subject, $htmlversion, $textversion); } notice(DI::l10n()->t('Emails sent')); diff --git a/securemail/securemail.php b/securemail/securemail.php index 8b8a3fd8..1f7cd768 100644 --- a/securemail/securemail.php +++ b/securemail/securemail.php @@ -103,20 +103,12 @@ function securemail_settings_post(App &$a, array &$b) $subject = 'Friendica - Secure Mail - Test'; $message = 'This is a test message from your Friendica Secure Mail addon.'; - $params = [ - 'uid' => local_user(), - 'fromName' => $sitename, - 'fromEmail' => $sender_email, - 'toEmail' => $a->user['email'], - 'messageSubject' => $subject, - 'htmlVersion' => "

{$message}

", - 'textVersion' => $message, - ]; - // enable addon for test DI::pConfig()->set(local_user(), 'securemail', 'enable', 1); - $res = DI::emailer()->send($params); + $res = DI::emailer()->send($sitename, $sender_email, $sender_email, + $a->user['email'], $subject, "

{$message}

", $message, + '', local_user()); // revert to saved value DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable); From 49254a8307f80cfe3ebd165ca556c3acb893b7c6 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 20:23:58 +0100 Subject: [PATCH 3/4] Introduce interface for emailing and create email classes --- notifyall/NotifyAllEMail.php | 40 ++++++++++++++++++++++++++++++++++ notifyall/notifyall.php | 37 ++++++++----------------------- securemail/SecureTestEMail.php | 40 ++++++++++++++++++++++++++++++++++ securemail/securemail.php | 23 ++----------------- 4 files changed, 91 insertions(+), 49 deletions(-) create mode 100644 notifyall/NotifyAllEMail.php create mode 100644 securemail/SecureTestEMail.php diff --git a/notifyall/NotifyAllEMail.php b/notifyall/NotifyAllEMail.php new file mode 100644 index 00000000..eeca4faf --- /dev/null +++ b/notifyall/NotifyAllEMail.php @@ -0,0 +1,40 @@ +get('config', 'sitename'); + + if (empty($config->get('config', 'admin_name'))) { + $sender_name = '"' . $l10n->t('%s Administrator', $sitename) . '"'; + } else { + $sender_name = '"' . $l10n->t('%1$s, %2$s Administrator', $config->get('config', 'admin_name'), $sitename) . '"'; + } + + if (!$config->get('config', 'sender_email')) { + $sender_email = 'noreply@' . $baseUrl->getHostname(); + } else { + $sender_email = $config->get('config', 'sender_email'); + } + + $subject = $_REQUEST['subject']; + + $textversion = strip_tags(html_entity_decode(BBCode::convert(stripslashes(str_replace(["\\r", "\\n"], ["", "\n"], $text))), ENT_QUOTES, 'UTF-8')); + + $htmlversion = BBCode::convert(stripslashes(str_replace(["\\r", "\\n"], ["", "
\n"], $text))); + + parent::__construct($sender_name, $sender_email, $sender_email, '', $subject, $htmlversion, $textversion); + } +} diff --git a/notifyall/notifyall.php b/notifyall/notifyall.php index 2190a9d1..c2391fcc 100644 --- a/notifyall/notifyall.php +++ b/notifyall/notifyall.php @@ -8,12 +8,12 @@ * Author: Rabuzarus (Port to Friendica) */ +use Friendica\Addon\notifyall\NotifyAllEMail; use Friendica\App; -use Friendica\Content\Text\BBCode; +use Friendica\Database\DBA; use Friendica\Core\Logger; use Friendica\Core\Renderer; use Friendica\DI; -use Friendica\Util\Emailer; function notifyall_install() { @@ -45,27 +45,6 @@ function notifyall_post(App $a) return; } - $sitename = DI::config()->get('config', 'sitename'); - - if (empty(DI::config()->get('config', 'admin_name'))) { - $sender_name = '"' . DI::l10n()->t('%s Administrator', $sitename) . '"'; - } else { - $sender_name = '"' . DI::l10n()->t('%1$s, %2$s Administrator', DI::config()->get('config', 'admin_name'), $sitename) . '"'; - } - - if (!DI::config()->get('config', 'sender_email')) { - $sender_email = 'noreply@' . DI::baseUrl()->getHostname(); - } else { - $sender_email = DI::config()->get('config', 'sender_email'); - } - - $subject = $_REQUEST['subject']; - - - $textversion = strip_tags(html_entity_decode(BBCode::convert(stripslashes(str_replace(["\\r", "\\n"], ["", "\n"], $text))), ENT_QUOTES, 'UTF-8')); - - $htmlversion = BBCode::convert(stripslashes(str_replace(["\\r", "\\n"], ["", "
\n"], $text))); - // if this is a test, send it only to the admin(s) // admin_email might be a comma separated list, but we need "a@b','c@d','e@f if (intval($_REQUEST['test'])) { @@ -74,15 +53,17 @@ function notifyall_post(App $a) } $sql_extra = ((intval($_REQUEST['test'])) ? sprintf(" AND `email` in ( %s )", $email) : ''); - $recips = q("SELECT DISTINCT `email` FROM `user` WHERE `verified` AND NOT `account_removed` AND NOT `account_expired` $sql_extra"); + $recipients = DBA::p("SELECT DISTINCT `email` FROM `user` WHERE `verified` AND NOT `account_removed` AND NOT `account_expired` $sql_extra"); - if (! $recips) { + if (! $recipients) { notice(DI::l10n()->t('No recipients found.') . EOL); return; } - foreach ($recips as $recip) { - DI::emailer()->send($sender_name, $sender_email, $sender_email, $recip['email'], $subject, $htmlversion, $textversion); + $notifyEmail = new NotifyAllEMail(DI::l10n(), DI::config(), DI::baseUrl(), $text); + + foreach ($recipients as $recipient) { + DI::emailer()->send($notifyEmail->withRecipient($recipient['email'])); } notice(DI::l10n()->t('Emails sent')); @@ -92,7 +73,7 @@ function notifyall_post(App $a) function notifyall_content(&$a) { if (! is_site_admin()) { - return; + return ''; } $title = DI::l10n()->t('Send email to all members of this Friendica instance.'); diff --git a/securemail/SecureTestEMail.php b/securemail/SecureTestEMail.php new file mode 100644 index 00000000..d7f9a296 --- /dev/null +++ b/securemail/SecureTestEMail.php @@ -0,0 +1,40 @@ +get('config', 'sitename'); + + $hostname = $baseUrl->getHostname(); + if (strpos($hostname, ':')) { + $hostname = substr($hostname, 0, strpos($hostname, ':')); + } + + $sender_email = $config->get('config', 'sender_email'); + if (empty($sender_email)) { + $sender_email = 'noreply@' . $hostname; + } + + $subject = 'Friendica - Secure Mail - Test'; + $message = 'This is a test message from your Friendica Secure Mail addon.'; + + // enable addon for test + $pConfig->set(local_user(), 'securemail', 'enable', 1); + + parent::__construct($sitename, $sender_email, $sender_email, $a->user['email'], + $subject, "

{$message}

", $message, + '', local_user()); + } +} diff --git a/securemail/securemail.php b/securemail/securemail.php index 1f7cd768..e25e9669 100644 --- a/securemail/securemail.php +++ b/securemail/securemail.php @@ -6,12 +6,12 @@ * Author: Fabio Comuni */ +use Friendica\Addon\securemail\SecureTestEMail; use Friendica\App; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Renderer; use Friendica\DI; -use Friendica\Util\Emailer; require_once __DIR__ . '/vendor/autoload.php'; @@ -88,27 +88,8 @@ function securemail_settings_post(App &$a, array &$b) info(DI::l10n()->t('Secure Mail Settings saved.') . EOL); if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) { - $sitename = DI::config()->get('config', 'sitename'); - $hostname = DI::baseUrl()->getHostname(); - if (strpos($hostname, ':')) { - $hostname = substr($hostname, 0, strpos($hostname, ':')); - } - - $sender_email = DI::config()->get('config', 'sender_email'); - if (empty($sender_email)) { - $sender_email = 'noreply@' . $hostname; - } - - $subject = 'Friendica - Secure Mail - Test'; - $message = 'This is a test message from your Friendica Secure Mail addon.'; - - // enable addon for test - DI::pConfig()->set(local_user(), 'securemail', 'enable', 1); - - $res = DI::emailer()->send($sitename, $sender_email, $sender_email, - $a->user['email'], $subject, "

{$message}

", $message, - '', local_user()); + $res = DI::emailer()->send(new SecureTestEMail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl())); // revert to saved value DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable); From ae5ebf1e898f97bf29dc0a73440f8ee89f196f0a Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 26 Jan 2020 23:47:15 +0100 Subject: [PATCH 4/4] Some Renames: - EMail => EMail - toEmail => toAddress - fromEmail => fromAddress --- notifyall/{NotifyAllEMail.php => NotifyAllEmail.php} | 4 ++-- notifyall/notifyall.php | 4 ++-- securemail/{SecureTestEMail.php => SecureTestEmail.php} | 4 ++-- securemail/securemail.php | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) rename notifyall/{NotifyAllEMail.php => NotifyAllEmail.php} (95%) rename securemail/{SecureTestEMail.php => SecureTestEmail.php} (94%) diff --git a/notifyall/NotifyAllEMail.php b/notifyall/NotifyAllEmail.php similarity index 95% rename from notifyall/NotifyAllEMail.php rename to notifyall/NotifyAllEmail.php index eeca4faf..96a57a67 100644 --- a/notifyall/NotifyAllEMail.php +++ b/notifyall/NotifyAllEmail.php @@ -6,12 +6,12 @@ use Friendica\App\BaseURL; use Friendica\Content\Text\BBCode; use Friendica\Core\Config\IConfig; use Friendica\Core\L10n; -use Friendica\Object\EMail; +use Friendica\Object\Email; /** * Class for creating a Notify-All EMail */ -class NotifyAllEMail extends EMail +class NotifyAllEmail extends Email { public function __construct(L10n $l10n, IConfig $config, BaseURL $baseUrl, string $text) { diff --git a/notifyall/notifyall.php b/notifyall/notifyall.php index c2391fcc..6d179380 100644 --- a/notifyall/notifyall.php +++ b/notifyall/notifyall.php @@ -8,7 +8,7 @@ * Author: Rabuzarus (Port to Friendica) */ -use Friendica\Addon\notifyall\NotifyAllEMail; +use Friendica\Addon\notifyall\NotifyAllEmail; use Friendica\App; use Friendica\Database\DBA; use Friendica\Core\Logger; @@ -60,7 +60,7 @@ function notifyall_post(App $a) return; } - $notifyEmail = new NotifyAllEMail(DI::l10n(), DI::config(), DI::baseUrl(), $text); + $notifyEmail = new NotifyAllEmail(DI::l10n(), DI::config(), DI::baseUrl(), $text); foreach ($recipients as $recipient) { DI::emailer()->send($notifyEmail->withRecipient($recipient['email'])); diff --git a/securemail/SecureTestEMail.php b/securemail/SecureTestEmail.php similarity index 94% rename from securemail/SecureTestEMail.php rename to securemail/SecureTestEmail.php index d7f9a296..37fe5d7c 100644 --- a/securemail/SecureTestEMail.php +++ b/securemail/SecureTestEmail.php @@ -6,12 +6,12 @@ use Friendica\App; use Friendica\App\BaseURL; use Friendica\Core\Config\IConfig; use Friendica\Core\PConfig\IPConfig; -use Friendica\Object\EMail; +use Friendica\Object\Email; /** * Class for creating a Test email for the securemail addon */ -class SecureTestEMail extends EMail +class SecureTestEmail extends Email { public function __construct(App $a, IConfig $config, IPConfig $pConfig, BaseURL $baseUrl) { diff --git a/securemail/securemail.php b/securemail/securemail.php index e25e9669..55d03207 100644 --- a/securemail/securemail.php +++ b/securemail/securemail.php @@ -6,7 +6,7 @@ * Author: Fabio Comuni */ -use Friendica\Addon\securemail\SecureTestEMail; +use Friendica\Addon\securemail\SecureTestEmail; use Friendica\App; use Friendica\Core\Hook; use Friendica\Core\Logger; @@ -89,7 +89,7 @@ function securemail_settings_post(App &$a, array &$b) if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) { - $res = DI::emailer()->send(new SecureTestEMail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl())); + $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl())); // revert to saved value DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);