summaryrefslogtreecommitdiff
path: root/includes/helper/email_helper.php
blob: 4fc1022672e82d1f4e065adc0ae73a47dde57494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

use Engelsystem\Mail\EngelsystemMailer;
use Engelsystem\Models\User\User;

/**
 * @param array|User $recipientUser
 * @param string     $title
 * @param string     $message
 * @param bool       $notIfItsMe
 * @return bool
 */
function engelsystem_email_to_user($recipientUser, $title, $message, $notIfItsMe = false)
{
    $user = Auth()->user();

    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) {
        return true;
    }

    /** @var \Engelsystem\Helpers\Translator $translator */
    $translator = app()->get('translator');
    $locale = $translator->getLocale();
    /** @var EngelsystemMailer $mailer */
    $mailer = app('mailer');

    $translator->setLocale($lang);
    $status = $mailer->sendView(
        $email,
        $title,
        'emails/mail',
        ['username' => $username, 'message' => $message]
    );
    $translator->setLocale($locale);

    if (!$status) {
        engelsystem_error('Unable to send email.');
    }

    return (bool)$status;
}