summaryrefslogtreecommitdiff
path: root/src/Mail/EngelsystemMailer.php
blob: 816606815bfcc2b4c1d52d3e3f6e51103649ec41 (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
<?php

namespace Engelsystem\Mail;

use Engelsystem\Renderer\Renderer;
use Swift_Mailer as SwiftMailer;

class EngelsystemMailer extends Mailer
{
    /** @var Renderer|null */
    protected $view;

    /** @var string */
    protected $subjectPrefix = null;

    /**
     * @param SwiftMailer $mailer
     * @param Renderer    $view
     */
    public function __construct(SwiftMailer $mailer, Renderer $view = null)
    {
        parent::__construct($mailer);

        $this->view = $view;
    }

    /**
     * Send a template
     *
     * @param string $to
     * @param string $subject
     * @param string $template
     * @param array  $data
     * @return int
     */
    public function sendView($to, $subject, $template, $data = []): int
    {
        $body = $this->view->render($template, $data);

        return $this->send($to, $subject, $body);
    }

    /**
     * Send the mail
     *
     * @param string|string[] $to
     * @param string          $subject
     * @param string          $body
     * @return int
     */
    public function send($to, string $subject, string $body): int
    {
        if ($this->subjectPrefix) {
            $subject = sprintf('[%s] %s', $this->subjectPrefix, $subject);
        }

        return parent::send($to, $subject, $body);
    }

    /**
     * @return string
     */
    public function getSubjectPrefix(): string
    {
        return $this->subjectPrefix;
    }

    /**
     * @param string $subjectPrefix
     */
    public function setSubjectPrefix(string $subjectPrefix)
    {
        $this->subjectPrefix = $subjectPrefix;
    }
}