forked from francisberesford/snapcms-yii2-mandrill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mailer.php
60 lines (52 loc) · 1.38 KB
/
Mailer.php
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
<?php
namespace snapcms\mandrill;
use Yii;
use yii\mail\BaseMailer;
/**
* @author Francis Beresford <francis@snapfrozen.com>
*/
class Mailer extends BaseMailer
{
/**
* @var string message default class name.
*/
public $messageClass = 'snapcms\mandrill\Message';
/**
*
* @var string your Mandrill API key
*/
public $apiKey;
/**
* @var \Swift_Mailer Swift mailer instance.
*/
private $_mandrill;
/**
* @return array|\Mandrill Swift mailer instance or array configuration.
*/
public function getMandrill()
{
if (!is_object($this->_mandrill))
{
$this->_mandrill = new \Mandrill($this->apiKey);
}
return $this->_mandrill;
}
/**
* @inheritdoc
*/
protected function sendMessage($message)
{
$address = $message->getTo();
if (is_array($address)) {
$address = implode(', ', array_keys($address));
}
Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
try {
$this->Mandrill->messages->send($message->getMandrillMessageArray());
} catch (Mandrill_Error $e) {
\Yii::error('A mandrill error occurred: ' . get_class($e) . ' - ' . $e->getMessage(), __METHOD__);
return false;
}
return true;
}
}