engelsystem/includes/helper/email_helper.php

54 lines
1.3 KiB
PHP
Raw Normal View History

2013-12-26 13:34:48 +01:00
<?php
use Engelsystem\Mail\EngelsystemMailer;
2018-10-09 21:47:31 +02:00
use Engelsystem\Models\User\User;
2017-01-03 03:22:48 +01:00
/**
2018-10-09 21:47:31 +02:00
* @param array|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-08 21:15:56 +02:00
$user = Auth()->user();
2017-01-02 15:43:36 +01:00
2018-10-09 21:47:31 +02:00
if ($recipientUser instanceof User) {
$id = $user->id;
$lang = $user->settings->language;
$email = $user->contact->email ? $user->contact->email : $user->email;
$username = $user->name;
} else {
$id = $recipientUser['UID'];
$lang = $recipientUser['Sprache'];
$email = $recipientUser['email'];
$username = $recipientUser['Nick'];
}
if ($notIfItsMe && $user->id == $id) {
2017-01-02 03:57:23 +01:00
return true;
}
2017-01-02 15:43:36 +01:00
/** @var \Engelsystem\Helpers\Translator $translator */
$translator = app()->get('translator');
$locale = $translator->getLocale();
/** @var EngelsystemMailer $mailer */
$mailer = app('mailer');
2017-01-02 15:43:36 +01:00
2018-10-09 21:47:31 +02:00
$translator->setLocale($lang);
$status = $mailer->sendView(
2018-10-09 21:47:31 +02:00
$email,
2017-01-03 03:22:48 +01:00
$title,
'emails/mail',
2018-10-09 21:47:31 +02:00
['username' => $username, 'message' => $message]
2017-01-03 03:22:48 +01:00
);
$translator->setLocale($locale);
2017-01-03 03:22:48 +01:00
if (!$status) {
2017-01-02 03:57:23 +01:00
engelsystem_error('Unable to send email.');
}
2017-01-03 03:22:48 +01:00
return (bool)$status;
2013-12-26 13:34:48 +01:00
}