mirror of
https://github.com/friendica/friendica
synced 2025-04-26 05:50:11 +00:00
Extract System emails from enotify
- Removed every SYSTEM_EMAIL occurrence in enotify - Introduced a "SystemMailBuilder" for build system emails - Replaced every SYSTEM_EMAIL usage in the classes with calling this builder - Added tests for this new Builder - Split the email templates between "base" template for email and concrete usages for different use cases
This commit is contained in:
parent
74490d6594
commit
3291728059
18 changed files with 647 additions and 118 deletions
|
@ -252,7 +252,7 @@ class Update
|
|||
}
|
||||
$sent[] = $admin['email'];
|
||||
|
||||
$lang = (($admin['language'])?$admin['language']:'en');
|
||||
$lang = (($admin['language']) ? $admin['language'] : 'en');
|
||||
$l10n = DI::l10n()->withLang($lang);
|
||||
|
||||
$preamble = Strings::deindent($l10n->t("
|
||||
|
@ -261,17 +261,15 @@ class Update
|
|||
This needs to be fixed soon and I can't do it alone. Please contact a
|
||||
friendica developer if you can not help me on your own. My database might be invalid.",
|
||||
$update_id));
|
||||
$body = $l10n->t("The error message is\n[pre]%s[/pre]", $error_message);
|
||||
$body = $l10n->t("The error message is\n[pre]%s[/pre]", $error_message);
|
||||
|
||||
notification([
|
||||
'uid' => $admin['uid'],
|
||||
'type' => SYSTEM_EMAIL,
|
||||
'to_email' => $admin['email'],
|
||||
'subject' => $l10n->t('[Friendica Notify] Database update'),
|
||||
'preamble' => $preamble,
|
||||
'body' => $body,
|
||||
'language' => $lang]
|
||||
);
|
||||
$email = DI::emailer()
|
||||
->newSystemMail($l10n)
|
||||
->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble, $body)
|
||||
->forUser($admin['uid'] ?? 0)
|
||||
->withRecipient($admin['email'])
|
||||
->build();
|
||||
DI::emailer()->send($email);
|
||||
}
|
||||
|
||||
//try the logger
|
||||
|
@ -301,15 +299,13 @@ class Update
|
|||
The friendica database was successfully updated from %s to %s.",
|
||||
$from_build, $to_build));
|
||||
|
||||
notification([
|
||||
'uid' => $admin['uid'],
|
||||
'type' => SYSTEM_EMAIL,
|
||||
'to_email' => $admin['email'],
|
||||
'subject' => DI::l10n()->t('[Friendica Notify] Database update'),
|
||||
'preamble' => $preamble,
|
||||
'body' => $preamble,
|
||||
'language' => $lang]
|
||||
);
|
||||
$email = DI::emailer()
|
||||
->newSystemMail($l10n)
|
||||
->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble)
|
||||
->forUser($admin['uid'] ?? 0)
|
||||
->withRecipient($admin['email'])
|
||||
->build();
|
||||
DI::emailer()->send($email);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -897,13 +897,13 @@ class User
|
|||
$password
|
||||
));
|
||||
|
||||
return notification([
|
||||
'type' => SYSTEM_EMAIL,
|
||||
'uid' => $user['uid'],
|
||||
'to_email' => $user['email'],
|
||||
'subject' => DI::l10n()->t('Registration at %s', $sitename),
|
||||
'body' => $body
|
||||
]);
|
||||
$email = DI::emailer()
|
||||
->newSystemMail(DI::l10n())
|
||||
->withMessage(DI::l10n()->t('Registration at %s', $sitename), $body)
|
||||
->forUser($user['uid'] ?? 0)
|
||||
->withRecipient($user['email'])
|
||||
->build();
|
||||
return DI::emailer()->send($email);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -965,15 +965,13 @@ class User
|
|||
$password
|
||||
));
|
||||
|
||||
return notification([
|
||||
'uid' => $user['uid'],
|
||||
'language' => $user['language'],
|
||||
'type' => SYSTEM_EMAIL,
|
||||
'to_email' => $user['email'],
|
||||
'subject' => DI::l10n()->t('Registration details for %s', $sitename),
|
||||
'preamble' => $preamble,
|
||||
'body' => $body
|
||||
]);
|
||||
$email = DI::emailer()
|
||||
->newSystemMail((!empty($user['language'])) ? DI::l10n()->withLang($user['language']) : DI::l10n())
|
||||
->withMessage(DI::l10n()->t('Registration details for %s', $sitename), $preamble, $body)
|
||||
->forUser($user['uid'] ?? 0)
|
||||
->withRecipient($user['email'])
|
||||
->build();
|
||||
return DI::emailer()->send($email);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -76,15 +76,13 @@ class Users extends BaseAdmin
|
|||
$preamble = sprintf($preamble, $user['username'], DI::config()->get('config', 'sitename'));
|
||||
$body = sprintf($body, DI::baseUrl()->get(), $user['nickname'], $result['password'], DI::config()->get('config', 'sitename'));
|
||||
|
||||
notification([
|
||||
'type' => SYSTEM_EMAIL,
|
||||
'language' => $user['language'],
|
||||
'to_name' => $user['username'],
|
||||
'to_email' => $user['email'],
|
||||
'uid' => $user['uid'],
|
||||
'subject' => DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')),
|
||||
'preamble' => $preamble,
|
||||
'body' => $body]);
|
||||
$email = DI::emailer()
|
||||
->newSystemMail((!empty($user['language'])) ? DI::l10n()->withLang($user['language']) : DI::l10n())
|
||||
->withMessage(DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')), $preamble, $body)
|
||||
->forUser($user['uid'] ?? 0)
|
||||
->withRecipient($user['email'])
|
||||
->build();
|
||||
return DI::emailer()->send($email);
|
||||
}
|
||||
|
||||
if (!empty($_POST['page_users_block'])) {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace Friendica\Object\EMail;
|
||||
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Util\Emailer;
|
||||
use JsonSerializable;
|
||||
|
||||
|
|
186
src/Util/EMailer/MailBuilder.php
Normal file
186
src/Util/EMailer/MailBuilder.php
Normal file
|
@ -0,0 +1,186 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Util\EMailer;
|
||||
|
||||
use Exception;
|
||||
use Friendica\App\BaseURL;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Model\User;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Object\Email;
|
||||
use Friendica\Object\EMail\IEmail;
|
||||
|
||||
/**
|
||||
* A base class for building new emails
|
||||
*/
|
||||
abstract class MailBuilder
|
||||
{
|
||||
/** @var L10n */
|
||||
protected $l10n;
|
||||
/** @var IConfig */
|
||||
protected $config;
|
||||
/** @var BaseURL */
|
||||
protected $baseUrl;
|
||||
|
||||
/** @var string */
|
||||
protected $headers;
|
||||
|
||||
/** @var string */
|
||||
protected $senderName = null;
|
||||
/** @var string */
|
||||
protected $senderAddress = null;
|
||||
/** @var string */
|
||||
protected $senderNoReply = null;
|
||||
|
||||
/** @var string */
|
||||
protected $recipientAddress = null;
|
||||
/** @var int */
|
||||
protected $recipientUid = null;
|
||||
|
||||
public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config)
|
||||
{
|
||||
$this->l10n = $l10n;
|
||||
$this->baseUrl = $baseUrl;
|
||||
$this->config = $config;
|
||||
|
||||
$hostname = $baseUrl->getHostname();
|
||||
if (strpos($hostname, ':')) {
|
||||
$hostname = substr($hostname, 0, strpos($hostname, ':'));
|
||||
}
|
||||
|
||||
$this->headers = "";
|
||||
$this->headers .= "Precedence: list\n";
|
||||
$this->headers .= "X-Friendica-Host: " . $hostname . "\n";
|
||||
$this->headers .= "X-Friendica-Platform: " . FRIENDICA_PLATFORM . "\n";
|
||||
$this->headers .= "X-Friendica-Version: " . FRIENDICA_VERSION . "\n";
|
||||
$this->headers .= "List-ID: <notification." . $hostname . ">\n";
|
||||
$this->headers .= "List-Archive: <" . $baseUrl->get() . "/notifications/system>\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the subject of the concrete builder, which inherits this base class
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getSubject();
|
||||
|
||||
/**
|
||||
* Gets the HTML version of the body of the concrete builder, which inherits this base class
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getHtmlMessage();
|
||||
|
||||
/**
|
||||
* Gets the Plaintext version of the body of the concrete builder, which inherits this base class
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract protected function getPlaintextMessage();
|
||||
|
||||
/**
|
||||
* Adds the User ID to the email in case the mail sending needs additional properties of this user
|
||||
*
|
||||
* @param int $uid The User ID
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function forUser(int $uid)
|
||||
{
|
||||
$this->recipientUid = $uid;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the sender to the email (if not called/set, the sender will get loaded with the help of the user id)
|
||||
*
|
||||
* @param string $name The name of the sender
|
||||
* @param string $address The (email) address of the sender
|
||||
* @param string|null $noReply Optional "no-reply" (email) address (if not set, it's the same as the address)
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function withSender(string $name, string $address, string $noReply = null)
|
||||
{
|
||||
$this->senderName = $name;
|
||||
$this->senderAddress = $address;
|
||||
$this->senderNoReply = $noReply ?? $this->senderNoReply;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a recipient to the email
|
||||
*
|
||||
* @param string $address The (email) address of the recipient
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function withRecipient(string $address)
|
||||
{
|
||||
$this->recipientAddress = $address;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a email based on the given attributes
|
||||
*
|
||||
* @param bool $raw True, if the email shouldn't get extended by the default email-template
|
||||
*
|
||||
* @return IEmail A new generated email
|
||||
*
|
||||
* @throws InternalServerErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function build(bool $raw = false)
|
||||
{
|
||||
if (empty($this->recipientAddress)) {
|
||||
throw new InternalServerErrorException('Recipient address is missing.');
|
||||
}
|
||||
|
||||
if ((empty($this->senderName) || empty($this->senderAddress)) &&
|
||||
!empty($this->recipientUid)) {
|
||||
$user = User::getById($this->recipientUid, ['username', 'email']);
|
||||
|
||||
if (!empty($user)) {
|
||||
$this->senderName = $user['username'];
|
||||
$this->senderAddress = $user['email'];
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($this->senderAddress) || empty($this->senderName)) {
|
||||
throw new InternalServerErrorException('Sender address or name is missing.');
|
||||
}
|
||||
|
||||
$this->senderNoReply = $this->senderNoReply ?? $this->senderAddress;
|
||||
|
||||
$msgHtml = $this->getHtmlMessage() ?? '';
|
||||
|
||||
if (!$raw) {
|
||||
// load the template for private message notifications
|
||||
$tpl = Renderer::getMarkupTemplate('email/notify/html.tpl');
|
||||
$msgHtml = Renderer::replaceMacros($tpl, [
|
||||
'$banner' => $this->l10n->t('Friendica Notification'),
|
||||
'$product' => FRIENDICA_PLATFORM,
|
||||
'$htmlversion' => $msgHtml,
|
||||
'$sitename' => $this->config->get('config', 'sitename'),
|
||||
'$siteurl' => $this->baseUrl->get(true),
|
||||
]);
|
||||
}
|
||||
|
||||
return new Email(
|
||||
$this->senderName,
|
||||
$this->senderAddress,
|
||||
$this->senderNoReply,
|
||||
$this->recipientAddress,
|
||||
$this->getSubject() ?? '',
|
||||
$msgHtml,
|
||||
$this->getPlaintextMessage() ?? '',
|
||||
$this->headers,
|
||||
$this->recipientUid ?? null);
|
||||
}
|
||||
}
|
110
src/Util/EMailer/SystemMailBuilder.php
Normal file
110
src/Util/EMailer/SystemMailBuilder.php
Normal file
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Util\EMailer;
|
||||
|
||||
use Exception;
|
||||
use Friendica\App\BaseURL;
|
||||
use Friendica\Content\Text\BBCode;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Core\Renderer;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Util\Emailer;
|
||||
|
||||
/**
|
||||
* Builder for system-wide emails without any dependency to concrete entities (like items, activities, ..)
|
||||
*/
|
||||
class SystemMailBuilder extends MailBuilder
|
||||
{
|
||||
/** @var string */
|
||||
protected $subject;
|
||||
/** @var string */
|
||||
protected $preamble;
|
||||
/** @var string */
|
||||
protected $body;
|
||||
|
||||
/** @var string */
|
||||
protected $siteAdmin;
|
||||
|
||||
public function __construct(L10n $l10n, BaseURL $baseUrl, IConfig $config)
|
||||
{
|
||||
parent::__construct($l10n, $baseUrl, $config);
|
||||
|
||||
$siteName = $this->config->get('config', 'sitename');
|
||||
|
||||
if ($this->config->get('config', 'admin_name')) {
|
||||
$this->siteAdmin = $l10n->t('%1$s, %2$s Administrator', $this->config->get('config', 'admin_name'), $siteName);
|
||||
} else {
|
||||
$this->siteAdmin = $l10n->t('%s Administrator', $siteName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a message
|
||||
*
|
||||
* @param string $subject The subject of the email
|
||||
* @param string $preamble The preamble of the email
|
||||
* @param string|null $body The body of the email (if not set, the preamble will get used as body)
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function withMessage(string $subject, string $preamble, string $body = null)
|
||||
{
|
||||
if (!isset($body)) {
|
||||
$body = $preamble;
|
||||
}
|
||||
|
||||
$this->subject = $subject;
|
||||
$this->preamble = $preamble;
|
||||
$this->body = $body;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
protected function getSubject()
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws InternalServerErrorException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getHtmlMessage()
|
||||
{
|
||||
$htmlVersion = BBCode::convert($this->body);
|
||||
|
||||
// load the template for private message notifications
|
||||
$tpl = Renderer::getMarkupTemplate('email/system/html.tpl');
|
||||
return Renderer::replaceMacros($tpl, [
|
||||
'$preamble' => str_replace("\n", "<br>\n", $this->preamble),
|
||||
'$thanks' => $this->l10n->t('thanks'),
|
||||
'$site_admin' => $this->siteAdmin,
|
||||
'$htmlversion' => $htmlVersion,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getPlaintextMessage()
|
||||
{
|
||||
$textVersion = BBCode::toPlaintext($this->body);
|
||||
|
||||
// load the template for private message notifications
|
||||
$tpl = Renderer::getMarkupTemplate('email/system/text.tpl');
|
||||
return Renderer::replaceMacros($tpl, [
|
||||
'$preamble' => $this->preamble,
|
||||
'$thanks' => $this->l10n->t('thanks'),
|
||||
'$site_admin' => $this->siteAdmin,
|
||||
'$textversion' => $textVersion,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -7,10 +7,12 @@ namespace Friendica\Util;
|
|||
use Friendica\App;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Core\Hook;
|
||||
use Friendica\Core\L10n;
|
||||
use Friendica\Core\PConfig\IPConfig;
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Object\EMail\IEmail;
|
||||
use Friendica\Protocol\Email;
|
||||
use Friendica\Util\EMailer\SystemMailBuilder;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
|
@ -35,6 +37,18 @@ class Emailer
|
|||
$this->baseUrl = $baseURL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new system email
|
||||
*
|
||||
* @param L10n $l10n The chosen language for the new email
|
||||
*
|
||||
* @return SystemMailBuilder
|
||||
*/
|
||||
public function newSystemMail(L10n $l10n)
|
||||
{
|
||||
return new SystemMailBuilder($l10n, $this->baseUrl, $this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a multipart/alternative message with Text and HTML versions
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue