/
QueueEmailTask.php
183 lines (158 loc) · 4.13 KB
/
QueueEmailTask.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Queue\Shell\Task;
use Cake\Core\Configure;
use Cake\Log\Log;
use Cake\Mailer\Email;
use Exception;
use RuntimeException;
use Throwable;
/**
* @author Mark Scherer
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class QueueEmailTask extends QueueTask {
/**
* List of default variables for EmailComponent
*
* @var array
*/
public $defaults = [
'to' => null,
'from' => null,
];
/**
* @var int
*/
public $timeout = 120;
/**
* @var int
*/
public $retries = 1;
/**
* @var \Cake\Mailer\Email
*/
public $Email;
/**
* "Add" the task, not possible for QueueEmailTask
*
* @return void
*/
public function add() {
$this->err('Queue Email Task cannot be added via Console.');
$this->out('Please use createJob() on the QueuedTask Model to create a Proper Email Task.');
$this->out('The Data Array should look something like this:');
$this->out(var_export([
'settings' => [
'to' => 'email@example.com',
'subject' => 'Email Subject',
'from' => 'system@example.com',
'template' => 'sometemplate',
],
'content' => 'hello world',
], true));
$this->out('Alternativly, you can pass the whole EmailLib to directly use it.');
}
/**
* @param array $data The array passed to QueuedJobsTable::createJob()
* @param int $jobId The id of the QueuedJob entity
* @return void
* @throws \Exception
* @throws \Throwable
*/
public function run(array $data, $jobId) {
if (!isset($data['settings'])) {
throw new RuntimeException('Queue Email task called without settings data.');
}
/** @var \Cake\Mailer\Email|null $email */
$email = $data['settings'];
if (is_object($email) && $email instanceof Email) {
$this->Email = $email;
$result = null;
try {
if (!empty($data['transport'])) {
$email->setTransport($data['transport']);
}
$content = isset($data['content']) ? $data['content'] : null;
$result = $email->send($content);
} catch (Throwable $e) {
$error = $e->getMessage();
$error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL . $e->getTraceAsString();
Log::write('error', $error);
throw $e;
} catch (Exception $e) {
$error = $e->getMessage();
$error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL . $e->getTraceAsString();
Log::write('error', $error);
throw $e;
}
if (!$result) {
throw new RuntimeException('Could not send email.');
}
return;
}
$this->Email = $this->_getMailer();
$settings = array_merge($this->defaults, $data['settings']);
foreach ($settings as $method => $setting) {
call_user_func_array([$this->Email, $method], (array)$setting);
}
$message = null;
if (isset($data['content'])) {
$message = $data['content'];
}
if (!empty($data['vars'])) {
$this->Email->setViewVars($data['vars']);
}
if (!empty($data['headers'])) {
if (!is_array($data['headers'])) {
throw new Exception('please provide headers as array');
}
$this->Email->setHeaders($data['headers']);
}
if (!$this->Email->send($message)) {
throw new RuntimeException('Could not send email.');
}
}
/**
* Check if Mail class exists and create instance
*
* @return \Cake\Mailer\Email
* @throws \Exception
*/
protected function _getMailer() {
$class = Configure::read('Queue.mailerClass');
if (!$class) {
$class = 'Tools\Mailer\Email';
if (!class_exists($class)) {
$class = 'Cake\Mailer\Email';
}
}
if (!class_exists($class)) {
throw new Exception(sprintf('Configured mailer class `%s` in `%s` not found.', $class, get_class($this)));
}
return new $class();
}
/**
* Log message
*
* @param array $contents log-data
* @param mixed $log int for loglevel, array for merge with log-data
* @return void
*/
protected function _log($contents, $log) {
$config = [
'level' => LOG_DEBUG,
'scope' => 'email',
];
if ($log !== true) {
if (!is_array($log)) {
$log = ['level' => $log];
}
$config = array_merge($config, $log);
}
Log::write(
$config['level'],
PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message'],
$config['scope']
);
}
}