simplify mail creation

This commit is contained in:
nupplaPhil 2020-02-04 21:04:08 +01:00
parent 9f95b975b4
commit 0b94b84dc7
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
8 changed files with 32 additions and 25 deletions

View file

@ -65,9 +65,9 @@ function lostpass_post(App $a)
Login Name: %3$s', $resetlink, DI::baseUrl(), $user['nickname'])); Login Name: %3$s', $resetlink, DI::baseUrl(), $user['nickname']));
$email = DI::emailer() $email = DI::emailer()
->newSystemMail((!empty($user['language'])) ? DI::l10n()->withLang($user['language']) : DI::l10n()) ->newSystemMail()
->withMessage(DI::l10n()->t('Password reset requested at %s', $sitename), $preamble, $body) ->withMessage(DI::l10n()->t('Password reset requested at %s', $sitename), $preamble, $body)
->forUser($user['uid'] ?? 0) ->forUser($user)
->withRecipient($user['email']) ->withRecipient($user['email'])
->build(); ->build();
@ -157,9 +157,9 @@ function lostpass_generate_password($user)
', DI::baseUrl(), $user['nickname'], $new_password)); ', DI::baseUrl(), $user['nickname'], $new_password));
$email = DI::emailer() $email = DI::emailer()
->newSystemMail((!empty($user['language'])) ? DI::l10n()->withLang($user['language']) : DI::l10n()) ->newSystemMail()
->withMessage(DI::l10n()->t('Your password has been changed at %s', $sitename), $preamble, $body) ->withMessage(DI::l10n()->t('Your password has been changed at %s', $sitename), $preamble, $body)
->forUser($user['uid'] ?? 0) ->forUser($user)
->withRecipient($user['email']) ->withRecipient($user['email'])
->build(); ->build();
DI::emailer()->send($email); DI::emailer()->send($email);

View file

@ -42,12 +42,12 @@ function removeme_post(App $a)
} }
$email = DI::emailer() $email = DI::emailer()
->newSystemMail((!empty($admin['language'])) ? DI::l10n()->withLang($admin['language']) : DI::l10n()->withLang('en')) ->newSystemMail()
->withMessage( ->withMessage(
DI::l10n()->t('[Friendica System Notify]') . ' ' . DI::l10n()->t('User deleted their account'), DI::l10n()->t('[Friendica System Notify]') . ' ' . DI::l10n()->t('User deleted their account'),
DI::l10n()->t('On your Friendica node an user deleted their account. Please ensure that their data is removed from the backups.'), DI::l10n()->t('On your Friendica node an user deleted their account. Please ensure that their data is removed from the backups.'),
DI::l10n()->t('The user id is %d', local_user())) DI::l10n()->t('The user id is %d', local_user()))
->forUser($admin['uid'] ?? 0) ->forUser($admin)
->withRecipient($admin['email']) ->withRecipient($admin['email'])
->build(); ->build();
DI::emailer()->send($email); DI::emailer()->send($email);

View file

@ -264,9 +264,9 @@ class Update
$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);
$email = DI::emailer() $email = DI::emailer()
->newSystemMail($l10n) ->newSystemMail()
->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble, $body) ->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble, $body)
->forUser($admin['uid'] ?? 0) ->forUser($admin)
->withRecipient($admin['email']) ->withRecipient($admin['email'])
->build(); ->build();
DI::emailer()->send($email); DI::emailer()->send($email);
@ -300,9 +300,9 @@ class Update
$from_build, $to_build)); $from_build, $to_build));
$email = DI::emailer() $email = DI::emailer()
->newSystemMail($l10n) ->newSystemMail()
->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble) ->withMessage($l10n->t('[Friendica Notify] Database update'), $preamble)
->forUser($admin['uid'] ?? 0) ->forUser($admin)
->withRecipient($admin['email']) ->withRecipient($admin['email'])
->build(); ->build();
DI::emailer()->send($email); DI::emailer()->send($email);

View file

@ -898,9 +898,9 @@ class User
)); ));
$email = DI::emailer() $email = DI::emailer()
->newSystemMail(DI::l10n()) ->newSystemMail()
->withMessage(DI::l10n()->t('Registration at %s', $sitename), $body) ->withMessage(DI::l10n()->t('Registration at %s', $sitename), $body)
->forUser($user['uid'] ?? 0) ->forUser($user)
->withRecipient($user['email']) ->withRecipient($user['email'])
->build(); ->build();
return DI::emailer()->send($email); return DI::emailer()->send($email);
@ -966,9 +966,9 @@ class User
)); ));
$email = DI::emailer() $email = DI::emailer()
->newSystemMail($l10n) ->newSystemMail()
->withMessage(DI::l10n()->t('Registration details for %s', $sitename), $preamble, $body) ->withMessage(DI::l10n()->t('Registration details for %s', $sitename), $preamble, $body)
->forUser($user['uid'] ?? 0) ->forUser($user)
->withRecipient($user['email']) ->withRecipient($user['email'])
->build(); ->build();
return DI::emailer()->send($email); return DI::emailer()->send($email);

View file

@ -77,9 +77,9 @@ class Users extends BaseAdmin
$body = sprintf($body, DI::baseUrl()->get(), $user['nickname'], $result['password'], DI::config()->get('config', 'sitename')); $body = sprintf($body, DI::baseUrl()->get(), $user['nickname'], $result['password'], DI::config()->get('config', 'sitename'));
$email = DI::emailer() $email = DI::emailer()
->newSystemMail((!empty($user['language'])) ? DI::l10n()->withLang($user['language']) : DI::l10n()) ->newSystemMail()
->withMessage(DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')), $preamble, $body) ->withMessage(DI::l10n()->t('Registration details for %s', DI::config()->get('config', 'sitename')), $preamble, $body)
->forUser($user['uid'] ?? 0) ->forUser($user)
->withRecipient($user['email']) ->withRecipient($user['email'])
->build(); ->build();
return DI::emailer()->send($email); return DI::emailer()->send($email);

View file

@ -86,13 +86,18 @@ abstract class MailBuilder
/** /**
* Adds the User ID to the email in case the mail sending needs additional properties of this user * Adds the User ID to the email in case the mail sending needs additional properties of this user
* *
* @param int $uid The User ID * @todo Once the user array is replaced with a user entity, replace this array parameter as well
* @param array $user The user entity/array, for which the email should be sent
* *
* @return static * @return static
*/ */
public function forUser(int $uid) public function forUser(array $user)
{ {
$this->recipientUid = $uid; $this->recipientUid = $user['uid'] ?? 0;
try {
$this->l10n = $user['language'] ? $this->l10n->withLang($user['language']) : $this->l10n;
} catch (Exception $e) {
}
return $this; return $this;
} }

View file

@ -28,18 +28,22 @@ class Emailer
private $logger; private $logger;
/** @var App\BaseURL */ /** @var App\BaseURL */
private $baseUrl; private $baseUrl;
/** @var L10n */
private $l10n;
/** @var string */ /** @var string */
private $siteEmailAddress; private $siteEmailAddress;
/** @var string */ /** @var string */
private $siteEmailName; private $siteEmailName;
public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger) public function __construct(IConfig $config, IPConfig $pConfig, App\BaseURL $baseURL, LoggerInterface $logger,
L10n $defaultLang)
{ {
$this->config = $config; $this->config = $config;
$this->pConfig = $pConfig; $this->pConfig = $pConfig;
$this->logger = $logger; $this->logger = $logger;
$this->baseUrl = $baseURL; $this->baseUrl = $baseURL;
$this->l10n = $defaultLang;
$this->siteEmailAddress = $this->config->get('config', 'sender_email'); $this->siteEmailAddress = $this->config->get('config', 'sender_email');
if (empty($sysEmailAddress)) { if (empty($sysEmailAddress)) {
@ -77,13 +81,11 @@ class Emailer
/** /**
* Creates a new system email * Creates a new system email
* *
* @param L10n $l10n The chosen language for the new email
*
* @return SystemMailBuilder * @return SystemMailBuilder
*/ */
public function newSystemMail(L10n $l10n) public function newSystemMail()
{ {
return new SystemMailBuilder($l10n, $this->baseUrl, $this->config, return new SystemMailBuilder($this->l10n, $this->baseUrl, $this->config,
$this->getSiteEmailAddress(), $this->getSiteEmailName()); $this->getSiteEmailAddress(), $this->getSiteEmailName());
} }

View file

@ -88,7 +88,7 @@ class MailBuilderTest extends MockedTest
->withMessage('Subject', 'Html', 'text') ->withMessage('Subject', 'Html', 'text')
->withRecipient('recipient@friendica.local') ->withRecipient('recipient@friendica.local')
->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local') ->withSender('Sender', 'sender@friendica.local', 'no-reply@friendica.local')
->forUser(100) ->forUser(['uid' => 100])
->build(true); ->build(true);
$this->assertEmail($testEmail, [ $this->assertEmail($testEmail, [