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

namespace Engelsystem\Mail;

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

class Mailer
{
    /** @var SwiftMailer */
    protected $mailer;

    /** @var Renderer|null */
    protected $view;

    /** @var string */
    protected $fromAddress = '';

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

    public function __construct(SwiftMailer $mailer)
    {
        $this->mailer = $mailer;
    }

    /**
     * Send the mail
     *
     * @param string|string[] $to
     * @param string          $subject
     * @param string          $body
     * @return int
     */
    public function send($to, string $subject, string $body): int
    {
        /** @var SwiftMessage $message */
        $message = $this->mailer->createMessage();
        $message->setTo((array)$to)
            ->setFrom($this->fromAddress, $this->fromName)
            ->setSubject($subject)
            ->setBody($body);

        return $this->mailer->send($message);
    }

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

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

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

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