1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\queue; |
10
|
|
|
|
11
|
|
|
use craft\queue\BaseJob; |
12
|
|
|
use Craft; |
13
|
|
|
use craft\elements\User; |
14
|
|
|
use craft\helpers\StringHelper; |
15
|
|
|
use yii\helpers\Json; |
16
|
|
|
use yii\mail\MessageInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 2.3.2 |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractEmailByKey extends BaseJob |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The email key |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
abstract public function getKey(): string; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The recipients |
33
|
|
|
* |
34
|
|
|
* @return User[] |
35
|
|
|
*/ |
36
|
|
|
abstract public function getRecipients(): array; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The email params |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
abstract public function getParams(): array; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns a default description for [[getDescription()]]. |
47
|
|
|
* |
48
|
|
|
* @return string|null |
49
|
|
|
*/ |
50
|
|
|
protected function defaultDescription() |
51
|
|
|
{ |
52
|
|
|
return 'Sending an email by key.'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param \craft\queue\QueueInterface|\yii\queue\Queue $queue |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
public function execute($queue) |
60
|
|
|
{ |
61
|
|
|
// A random tracking string |
62
|
|
|
$id = StringHelper::randomString(); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
Craft::info( |
66
|
|
|
sprintf( |
67
|
|
|
"[Sending email via job '%s'.] %s", |
68
|
|
|
(string)$id, |
69
|
|
|
(string)Json::encode( |
70
|
|
|
[ |
71
|
|
|
'tracking' => $id, |
72
|
|
|
'key' => $this->getKey(), |
73
|
|
|
'recipients' => count($this->getRecipients()) |
74
|
|
|
] |
75
|
|
|
) |
76
|
|
|
), |
77
|
|
|
__METHOD__ |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
foreach ($this->getRecipients() as $recipient) { |
81
|
|
|
if (!$this->composeMessage($recipient, $this->getParams())->send()) { |
82
|
|
|
Craft::warning( |
83
|
|
|
sprintf( |
84
|
|
|
"Failed to send email via job '%s'", |
85
|
|
|
(string)$id |
86
|
|
|
), |
87
|
|
|
__METHOD__ |
88
|
|
|
); |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
Craft::info( |
93
|
|
|
sprintf( |
94
|
|
|
"Successfully sent email via job '%s'", |
95
|
|
|
(string)$id |
96
|
|
|
), |
97
|
|
|
__METHOD__ |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} catch (\Exception $e) { |
101
|
|
|
Craft::warning( |
102
|
|
|
sprintf( |
103
|
|
|
"Exception caught while trying to run '%s' (Id: %s) job. Exception: [%s].", |
104
|
|
|
(string)get_class($this), |
105
|
|
|
$id, |
106
|
|
|
(string)Json::encode([ |
107
|
|
|
'Trace' => $e->getTraceAsString(), |
108
|
|
|
'File' => $e->getFile(), |
109
|
|
|
'Line' => $e->getLine(), |
110
|
|
|
'Code' => $e->getCode(), |
111
|
|
|
'Message' => $e->getMessage() |
112
|
|
|
]) |
113
|
|
|
), |
114
|
|
|
__METHOD__ |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
throw $e; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $recipient |
123
|
|
|
* @param array $params |
124
|
|
|
* @return MessageInterface |
125
|
|
|
* @throws \yii\base\InvalidConfigException |
126
|
|
|
*/ |
127
|
|
|
protected function composeMessage($recipient, array $params): MessageInterface |
128
|
|
|
{ |
129
|
|
|
// Attempt to convert a $recipient to a User element |
130
|
|
|
$recipient = $this->resolveUser($recipient); |
131
|
|
|
|
132
|
|
|
return Craft::$app->getMailer() |
133
|
|
|
->composeFromKey( |
134
|
|
|
$this->getKey(), |
135
|
|
|
array_merge( |
136
|
|
|
['recipient' => $recipient], |
137
|
|
|
$params |
138
|
|
|
) |
139
|
|
|
)->setTo($recipient); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param User|int|string|array $user |
144
|
|
|
* @return User |
145
|
|
|
*/ |
146
|
|
|
protected function resolveUser($user) |
147
|
|
|
{ |
148
|
|
|
if ($user instanceof User) { |
149
|
|
|
return $user; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// An Id |
153
|
|
|
if (is_numeric($user)) { |
154
|
|
|
return Craft::$app->getUsers()->getUserById($user); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// An email |
158
|
|
|
if (is_string($user)) { |
159
|
|
|
if (!$element = Craft::$app->getUsers()->getUserByUsernameOrEmail($user)) { |
160
|
|
|
$element = new User([ |
161
|
|
|
'email' => $user |
162
|
|
|
]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $element; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if (is_array($user)) { |
169
|
|
|
$email = key($user); |
170
|
|
|
$user = reset($user); |
171
|
|
|
|
172
|
|
|
// $user was an array [email => name] |
173
|
|
|
if (is_string($email)) { |
174
|
|
|
@list($firstName, $lastName) = explode(' ', $user); |
|
|
|
|
175
|
|
|
|
176
|
|
|
// Resolve user (and set name) |
177
|
|
|
if ($element = $this->resolveUser($email)) { |
178
|
|
|
$element->firstName = $firstName ?: $element->firstName; |
179
|
|
|
$element->lastName = $lastName ?: $element->lastName; |
180
|
|
|
return $element; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return new User([ |
184
|
|
|
'email' => $email, |
185
|
|
|
'firstName' => $firstName, |
186
|
|
|
'lastName' => $lastName |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// An array of [$user] |
191
|
|
|
if (!$element = $this->resolveUser($user)) { |
192
|
|
|
return new User([ |
193
|
|
|
'email' => $user |
194
|
|
|
]); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $element; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return null; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: