2013-12-26 13:34:48 +01:00
|
|
|
<?php
|
|
|
|
|
2019-07-08 01:31:59 +02:00
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2018-09-05 13:40:03 +02:00
|
|
|
use Engelsystem\Mail\EngelsystemMailer;
|
2018-10-09 21:47:31 +02:00
|
|
|
use Engelsystem\Models\User\User;
|
2019-04-29 23:17:58 +02:00
|
|
|
use Psr\Log\LogLevel;
|
2018-09-05 13:40:03 +02:00
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
2018-10-17 01:30:10 +02:00
|
|
|
* @param User $recipientUser
|
|
|
|
* @param string $title
|
|
|
|
* @param string $message
|
|
|
|
* @param bool $notIfItsMe
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-10-09 21:47:31 +02:00
|
|
|
function engelsystem_email_to_user($recipientUser, $title, $message, $notIfItsMe = false)
|
2017-01-02 03:57:23 +01:00
|
|
|
{
|
2018-10-31 12:48:22 +01:00
|
|
|
if ($notIfItsMe && auth()->user()->id == $recipientUser->id) {
|
2019-04-29 23:17:58 +02:00
|
|
|
return true;
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2019-07-08 01:31:59 +02:00
|
|
|
/** @var Translator $translator */
|
2018-08-28 22:23:59 +02:00
|
|
|
$translator = app()->get('translator');
|
|
|
|
$locale = $translator->getLocale();
|
2019-04-29 23:17:58 +02:00
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
$status = true;
|
2019-04-28 14:54:32 +02:00
|
|
|
try {
|
|
|
|
/** @var EngelsystemMailer $mailer */
|
|
|
|
$mailer = app('mailer');
|
2019-04-29 23:17:58 +02:00
|
|
|
|
2019-04-28 14:54:32 +02:00
|
|
|
$translator->setLocale($recipientUser->settings->language);
|
2021-12-20 21:07:00 +01:00
|
|
|
$mailer->sendView(
|
2022-06-16 23:00:56 +02:00
|
|
|
$recipientUser->contact->email ?: $recipientUser->email,
|
2019-04-28 14:54:32 +02:00
|
|
|
$title,
|
|
|
|
'emails/mail',
|
|
|
|
['username' => $recipientUser->name, 'message' => $message]
|
|
|
|
);
|
2019-04-29 23:17:58 +02:00
|
|
|
} catch (Exception $e) {
|
2021-12-20 21:07:00 +01:00
|
|
|
$status = false;
|
2019-04-29 23:17:58 +02:00
|
|
|
engelsystem_log(sprintf(
|
|
|
|
'An exception occurred while sending a mail to %s in %s:%u: %s',
|
|
|
|
$recipientUser->name,
|
|
|
|
$e->getFile(),
|
|
|
|
$e->getLine(),
|
|
|
|
$e->getMessage()
|
|
|
|
), LogLevel::CRITICAL);
|
2019-04-28 14:54:32 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2018-09-05 13:40:03 +02:00
|
|
|
$translator->setLocale($locale);
|
2017-01-03 03:22:48 +01:00
|
|
|
|
2018-09-05 13:40:03 +02:00
|
|
|
if (!$status) {
|
2019-04-28 14:54:32 +02:00
|
|
|
error(sprintf(__('User %s could not be notified by email due to an error.'), User_Nick_render($recipientUser)));
|
|
|
|
engelsystem_log(sprintf('User %s could not be notified by email due to an error.', $recipientUser->name));
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-03 03:22:48 +01:00
|
|
|
|
2021-12-20 21:07:00 +01:00
|
|
|
return $status;
|
2013-12-26 13:34:48 +01:00
|
|
|
}
|