Merge pull request #949 from nupplaphil/task/split_emails

[various] Introduce specific Email classes
This commit is contained in:
Hypolite Petovan 2020-01-26 17:13:43 -05:00 committed by GitHub
commit bfa25a197c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 65 deletions

View File

@ -0,0 +1,40 @@
<?php
namespace Friendica\Addon\notifyall;
use Friendica\App\BaseURL;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Config\IConfig;
use Friendica\Core\L10n;
use Friendica\Object\Email;
/**
* Class for creating a Notify-All EMail
*/
class NotifyAllEmail extends Email
{
public function __construct(L10n $l10n, IConfig $config, BaseURL $baseUrl, string $text)
{
$sitename = $config->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"], ["", "<br />\n"], $text)));
parent::__construct($sender_name, $sender_email, $sender_email, '', $subject, $htmlversion, $textversion);
}
}

View File

@ -8,12 +8,12 @@
* Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus> (Port to Friendica) * Author: Rabuzarus <https://friendica.kommune4.de/profile/rabuzarus> (Port to Friendica)
*/ */
use Friendica\Addon\notifyall\NotifyAllEmail;
use Friendica\App; use Friendica\App;
use Friendica\Content\Text\BBCode; use Friendica\Database\DBA;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\DI; use Friendica\DI;
use Friendica\Util\Emailer;
function notifyall_install() function notifyall_install()
{ {
@ -45,27 +45,6 @@ function notifyall_post(App $a)
return; 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"], ["", "<br />\n"], $text)));
// if this is a test, send it only to the admin(s) // 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 // admin_email might be a comma separated list, but we need "a@b','c@d','e@f
if (intval($_REQUEST['test'])) { if (intval($_REQUEST['test'])) {
@ -74,23 +53,17 @@ function notifyall_post(App $a)
} }
$sql_extra = ((intval($_REQUEST['test'])) ? sprintf(" AND `email` in ( %s )", $email) : ''); $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); notice(DI::l10n()->t('No recipients found.') . EOL);
return; return;
} }
foreach ($recips as $recip) { $notifyEmail = new NotifyAllEmail(DI::l10n(), DI::config(), DI::baseUrl(), $text);
Emailer::send([
'fromName' => $sender_name, foreach ($recipients as $recipient) {
'fromEmail' => $sender_email, DI::emailer()->send($notifyEmail->withRecipient($recipient['email']));
'replyTo' => $sender_email,
'toEmail' => $recip['email'],
'messageSubject' => $subject,
'htmlVersion' => $htmlversion,
'textVersion' => $textversion
]);
} }
notice(DI::l10n()->t('Emails sent')); notice(DI::l10n()->t('Emails sent'));
@ -100,7 +73,7 @@ function notifyall_post(App $a)
function notifyall_content(&$a) function notifyall_content(&$a)
{ {
if (! is_site_admin()) { if (! is_site_admin()) {
return; return '';
} }
$title = DI::l10n()->t('Send email to all members of this Friendica instance.'); $title = DI::l10n()->t('Send email to all members of this Friendica instance.');

View File

@ -0,0 +1,40 @@
<?php
namespace Friendica\Addon\securemail;
use Friendica\App;
use Friendica\App\BaseURL;
use Friendica\Core\Config\IConfig;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Object\Email;
/**
* Class for creating a Test email for the securemail addon
*/
class SecureTestEmail extends Email
{
public function __construct(App $a, IConfig $config, IPConfig $pConfig, BaseURL $baseUrl)
{
$sitename = $config->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, "<p>{$message}</p>", $message,
'', local_user());
}
}

View File

@ -6,12 +6,12 @@
* Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm> * Author: Fabio Comuni <http://kirgroup.com/profile/fabrixxm>
*/ */
use Friendica\Addon\securemail\SecureTestEmail;
use Friendica\App; use Friendica\App;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\DI; use Friendica\DI;
use Friendica\Util\Emailer;
require_once __DIR__ . '/vendor/autoload.php'; require_once __DIR__ . '/vendor/autoload.php';
@ -88,35 +88,8 @@ function securemail_settings_post(App &$a, array &$b)
info(DI::l10n()->t('Secure Mail Settings saved.') . EOL); info(DI::l10n()->t('Secure Mail Settings saved.') . EOL);
if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) { if ($_POST['securemail-submit'] == DI::l10n()->t('Save and send test')) {
$sitename = DI::config()->get('config', 'sitename');
$hostname = DI::baseUrl()->getHostname(); $res = DI::emailer()->send(new SecureTestEmail(DI::app(), DI::config(), DI::pConfig(), DI::baseUrl()));
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.';
$params = [
'uid' => local_user(),
'fromName' => $sitename,
'fromEmail' => $sender_email,
'toEmail' => $a->user['email'],
'messageSubject' => $subject,
'htmlVersion' => "<p>{$message}</p>",
'textVersion' => $message,
];
// enable addon for test
DI::pConfig()->set(local_user(), 'securemail', 'enable', 1);
$res = Emailer::send($params);
// revert to saved value // revert to saved value
DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable); DI::pConfig()->set(local_user(), 'securemail', 'enable', $enable);