This repository has been archived by the owner on Jan 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailer.php
111 lines (96 loc) · 2.84 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
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
<?php
namespace talview\sesmailer;
use Aws\Ses\Exception\SesException;
use \Aws\Ses\SesClient;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\Console;
use yii\mail\BaseMailer;
/**
* Mailer implementing email queueing and delivery functions
* @author Mani Ka <[email protected]>
*/
class Mailer extends BaseMailer
{
/**
* @var SesClient|array
*/
public $client;
public $messageConfig = [] ;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->client === null) {
throw new InvalidConfigException('The "client" property must be set.');
}
if (!$this->client instanceof SesClient) {
$this->client = new SesClient($this->client);
}
}
/**
*
* @param \talview\sesmailer\Message $message
*
* @return bool
*/
protected function sendMessage($message)
{
try {
return $this->client->sendRawEmail([
'RawMessage' => [
'Data' => $message->getSwiftMessage()
]
]);
} catch (SesException $e) {
echo $e->getMessage();
Yii::error($e->getMessage());
}
return false;
}
/**
*
* @param \talview\sesmailer\Message $message
*
* @return bool
*/
protected function sendMessageAsync($message)
{
try {
return $this->client->sendRawEmailAsync([
'RawMessage' => [
'Data' => base64_encode($message->getSwiftMessage())
]
]);
} catch (SesException $e) {
Console::output($e->getMessage());
Yii::error($e->getMessage());
}
return false;
}
/**
* Sends the given email message.
* This method will log a message about the email being sent.
* If [[useFileTransport]] is true, it will save the email as a file under [[fileTransportPath]].
* Otherwise, it will call [[sendMessage()]] to send the email to its recipient(s).
* Child classes should implement [[sendMessage()]] with the actual email sending logic.
* @param \talview\sesmailer\Message $message email message instance to be sent
* @return \GuzzleHttp\Promise\Promise whether the message has been sent successfully
*/
public function sendAsync($message)
{
if (!$this->beforeSend($message)) {
return false;
}
$address = $message->getTo();
if (is_array($address)) {
$address = implode(', ', array_keys($address));
}
Yii::info('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
$promise = $this->sendMessageAsync($message);
$this->afterSend($message, true);// async promises
return $promise;
}
}