summaryrefslogtreecommitdiff
path: root/app/Core/Mail/Client.php
blob: 334f8816fa803e4d8295982ae721dee8d766711a (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

namespace Kanboard\Core\Mail;

use Kanboard\Job\EmailJob;
use Pimple\Container;
use Kanboard\Core\Base;

/**
 * Mail Client
 *
 * @package  Kanboard\Core\Mail
 * @author   Frederic Guillot
 */
class Client extends Base
{
    /**
     * Mail transport instances
     *
     * @access private
     * @var \Pimple\Container
     */
    private $transports;

    /**
     * Constructor
     *
     * @access public
     * @param  \Pimple\Container   $container
     */
    public function __construct(Container $container)
    {
        parent::__construct($container);
        $this->transports = new Container;
    }

    /**
     * Send a HTML email
     *
     * @access public
     * @param  string  $recipientEmail
     * @param  string  $recipientName
     * @param  string  $subject
     * @param  string  $html
     * @return Client
     */
    public function send($recipientEmail, $recipientName, $subject, $html)
    {
        if (! empty($recipientEmail)) {
            $this->queueManager->push(EmailJob::getInstance($this->container)->withParams(
                $recipientEmail,
                $recipientName,
                $subject,
                $html,
                $this->getAuthorName(),
                $this->getAuthorEmail()
            ));
        }

        return $this;
    }

    /**
     * Get author name
     *
     * @access public
     * @return string
     */
    public function getAuthorName()
    {
        $author = 'Kanboard';

        if ($this->userSession->isLogged()) {
            $author = e('%s via Kanboard', $this->helper->user->getFullname());
        }

        return $author;
    }

    /**
     * Get author email
     *
     * @access public
     * @return string
     */
    public function getAuthorEmail()
    {
        if ($this->userSession->isLogged()) {
            $userData = $this->userSession->getAll();
            return ! empty($userData['email']) ? $userData['email'] : '';
        }

        return '';
    }

    /**
     * Get mail transport instance
     *
     * @access public
     * @param  string  $transport
     * @return ClientInterface
     */
    public function getTransport($transport)
    {
        return $this->transports[$transport];
    }

    /**
     * Add a new mail transport
     *
     * @access public
     * @param  string  $transport
     * @param  string  $class
     * @return Client
     */
    public function setTransport($transport, $class)
    {
        $container = $this->container;

        $this->transports[$transport] = function () use ($class, $container) {
            return new $class($container);
        };

        return $this;
    }

    /**
     * Return the list of registered transports
     *
     * @access public
     * @return array
     */
    public function getAvailableTransports()
    {
        $availableTransports = $this->transports->keys();
        return array_combine($availableTransports, $availableTransports);
    }
}