Completed
Push — master ( 32a670...32a37c )
by Damian
37s queued 21s
created

src/Control/Email/SwiftMailer.php (1 issue)

Severity
1
<?php
2
3
namespace SilverStripe\Control\Email;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\Core\Injector\Injector;
8
use Swift_Mailer;
9
use Swift_Message;
10
11
/**
12
 * Mailer objects are responsible for actually sending emails.
13
 * The default Mailer class will use PHP's mail() function.
14
 */
15
class SwiftMailer implements Mailer
16
{
17
18
    use Configurable;
19
    use Injectable;
20
21
    /**
22
     * @var array
23
     * @config
24
     */
25
    private static $swift_plugins = array(
0 ignored issues
show
The private property $swift_plugins is not used, and could be removed.
Loading history...
26
        SwiftPlugin::class,
27
    );
28
29
    /**
30
     * @var Swift_Mailer
31
     */
32
    private $swift;
33
34
    /**
35
     * @param Email $message
36
     * @return bool Whether the sending was "successful" or not
37
     */
38
    public function send($message)
39
    {
40
        $swiftMessage = $message->getSwiftMessage();
41
        $failedRecipients = array();
42
        $result = $this->sendSwift($swiftMessage, $failedRecipients);
43
        $message->setFailedRecipients($failedRecipients);
44
45
        return $result != 0;
46
    }
47
48
    /**
49
     * @param Swift_Message $message
50
     * @param array $failedRecipients
51
     * @return int
52
     */
53
    protected function sendSwift($message, &$failedRecipients = null)
54
    {
55
        return $this->getSwiftMailer()->send($message, $failedRecipients);
56
    }
57
58
    /**
59
     * @return Swift_Mailer
60
     */
61
    public function getSwiftMailer()
62
    {
63
        return $this->swift;
64
    }
65
66
    /**
67
     * @param Swift_Mailer $swift
68
     * @return $this
69
     */
70
    public function setSwiftMailer($swift)
71
    {
72
        // register any required plugins
73
        foreach ($this->config()->get('swift_plugins') as $plugin) {
74
            $swift->registerPlugin(Injector::inst()->create($plugin));
75
        }
76
        $this->swift = $swift;
77
78
        return $this;
79
    }
80
}
81